utils: steal updated container_of macro from linux

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2024-04-16 08:11:46 +01:00
parent b9fab89918
commit 4566acee33
No known key found for this signature in database
GPG Key ID: D3A4405BE6CC17F4
1 changed files with 9 additions and 2 deletions

View File

@ -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))); \
})
/**