mirror of https://github.com/yshui/picom.git
utils: steal updated container_of macro from linux
Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
parent
b9fab89918
commit
4566acee33
11
src/utils.h
11
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))); \
|
||||
})
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue