mirror of
https://github.com/darkk/redsocks.git
synced 2025-08-29 13:15:30 +00:00
Rewrite container_of macro without using statement expression.
Statement expression is GNU C extension, not present in ISO standard. Type checking works now via implicit conversion performed for compound literal (compound literals are available since C99). typeof is only used on GNUC compilers like gcc or clang.
This commit is contained in:
parent
2e3f648809
commit
fce288f657
14
utils.h
14
utils.h
@ -20,6 +20,12 @@ struct sockaddr_in;
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
#ifdef __GNUC__
|
||||||
|
#define member_type(type, member) __typeof(((type *)0)->member)
|
||||||
|
#else
|
||||||
|
#define member_type(type, member) const void
|
||||||
|
#endif
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* container_of - cast a member of a structure out to the containing structure
|
* container_of - cast a member of a structure out to the containing structure
|
||||||
* @ptr: the pointer to the member.
|
* @ptr: the pointer to the member.
|
||||||
@ -27,9 +33,11 @@ struct sockaddr_in;
|
|||||||
* @member: the name of the member within the struct.
|
* @member: the name of the member within the struct.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
#define container_of(ptr, type, member) ({ \
|
#define container_of(ptr, type, member) \
|
||||||
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
|
((type *)( \
|
||||||
(type *)( (char *)__mptr - offsetof(type,member) );})
|
(char *)(member_type(type, member) *){ ptr } - offsetof(type, member) \
|
||||||
|
))
|
||||||
|
|
||||||
|
|
||||||
#define clamp_value(value, min_val, max_val) do { \
|
#define clamp_value(value, min_val, max_val) do { \
|
||||||
if (value < min_val) \
|
if (value < min_val) \
|
||||||
|
Loading…
Reference in New Issue
Block a user