cgen: add the __offsetof_ptr C macro

pull/9233/head
Delyan Angelov 2021-03-11 13:08:45 +02:00
parent 5a231326d7
commit 144546543c
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
1 changed files with 6 additions and 1 deletions

View File

@ -103,9 +103,14 @@ static inline void __sort_ptr(uintptr_t a[], bool b[], int l)
#endif #endif
#endif #endif
// Use __offsetof_ptr instead of __offset_of, when you *do* have a valid pointer, to avoid UB:
#ifndef __offsetof_ptr
#define __offsetof_ptr(ptr,PTYPE,FIELDNAME) ((size_t)((byte *)&((PTYPE *)ptr)->FIELDNAME - (byte *)ptr))
#endif
// for __offset_of // for __offset_of
#ifndef __offsetof #ifndef __offsetof
#define __offsetof(s,memb) ((size_t)((char *)&((s *)0)->memb - (char *)0)) #define __offsetof(PTYPE,FIELDNAME) ((size_t)((char *)&((PTYPE *)0)->FIELDNAME - (char *)0))
#endif #endif
#define OPTION_CAST(x) (x) #define OPTION_CAST(x) (x)