ck_cc: use __builtin_offsetof for CK_CC_CONTAINER on gcc-ish compilers

Less undefined behaviour is always good.  In practice, compilers seem
to handle either form correctly, but some static analysis tools have
trouble with (size_t)&((T *)0)->M).
master
Paul Khuong 5 years ago
parent 91343eaa2c
commit 9a76e490ae

@ -50,6 +50,7 @@
* Container function.
* This relies on (compiler) implementation-defined behavior.
*/
#ifndef CK_CC_CONTAINER
#define CK_CC_CONTAINER(F, T, M, N) \
CK_CC_INLINE static T * \
N(F *p) \
@ -57,6 +58,7 @@
F *n = p; \
return (T *)(void *)(((char *)n) - ((size_t)&((T *)0)->M)); \
}
#endif
#define CK_CC_PAD(x) union { char pad[x]; }

@ -39,6 +39,15 @@
#define CK_CC_UNUSED __attribute__((unused))
#define CK_CC_USED __attribute__((used))
#define CK_CC_IMM "i"
#define CK_CC_CONTAINER(F, T, M, N) \
CK_CC_INLINE static T * \
N(F *p) \
{ \
\
return (T *)(void *)((char *)p - __builtin_offsetof(T, M)); \
}
#if defined(__x86_64__) || defined(__x86__)
#define CK_CC_IMM_U32 "Z"
#define CK_CC_IMM_S32 "e"

Loading…
Cancel
Save