diff --git a/utils.h b/utils.h index c2277e9..991af9c 100644 --- a/utils.h +++ b/utils.h @@ -20,6 +20,12 @@ struct sockaddr_in; #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 * @ptr: the pointer to the member. @@ -27,9 +33,11 @@ struct sockaddr_in; * @member: the name of the member within the struct. * */ -#define container_of(ptr, type, member) ({ \ - const typeof( ((type *)0)->member ) *__mptr = (ptr); \ - (type *)( (char *)__mptr - offsetof(type,member) );}) +#define container_of(ptr, type, member) \ + ((type *)( \ + (char *)(member_type(type, member) *){ ptr } - offsetof(type, member) \ + )) + #define clamp_value(value, min_val, max_val) do { \ if (value < min_val) \