diff --git a/src/utils.h b/src/utils.h index 43927381..60ad46e9 100644 --- a/src/utils.h +++ b/src/utils.h @@ -130,17 +130,24 @@ safe_isnan(double a) { (uint32_t) __to_tmp; \ }) +/* Are two types/vars the same type (ignoring qualifiers)? */ +#define is_same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b)) + /** * container_of - cast a member of a structure out to the containing structure * @ptr: the pointer to the member. * @type: the type of the container struct this is embedded in. * @member: the name of the member within the struct. * + * WARNING: any const qualifier of @ptr is lost. */ #define container_of(ptr, type, member) \ ({ \ - const __typeof__(((type *)0)->member) *__mptr = (ptr); \ - (type *)((char *)__mptr - offsetof(type, member)); \ + void *__mptr = (void *)(ptr); \ + static_assert(is_same_type(*(ptr), ((type *)0)->member) || \ + is_same_type(*(ptr), void), \ + "pointer type mismatch in container_of()"); \ + ((type *)(__mptr - offsetof(type, member))); \ }) /**