IMPORT: ebtree: add a definition of offsetof()

We'll use this to improve the definition of container_of(). Let's define
it if it does not exist. We can rely on __builtin_offsetof() on recent
enough compilers.

This is ebtree commit 1ea273e60832b98f552b9dbd013e6c2b32113aa5.
Signed-off-by: Willy Tarreau <w@1wt.eu>
This is ebtree commit 69b2ef57a8ce321e8de84486182012c954380401.
This commit is contained in:
Willy Tarreau 2025-09-13 14:21:54 +02:00
parent ddbff4e235
commit a31da78685

View File

@ -246,6 +246,7 @@
#ifndef _EBTREE_H #ifndef _EBTREE_H
#define _EBTREE_H #define _EBTREE_H
#include <stddef.h>
#include <stdlib.h> #include <stdlib.h>
#include <import/ebtree-t.h> #include <import/ebtree-t.h>
#include <haproxy/api.h> #include <haproxy/api.h>
@ -365,6 +366,15 @@ static inline unsigned int flsnz64(unsigned long long x)
#define fls64(x) flsnz64(x) #define fls64(x) flsnz64(x)
#define fls_auto(x) ((x) ? flsnz(x) : 0) #define fls_auto(x) ((x) ? flsnz(x) : 0)
/* offsetof() is provided as a builtin starting with gcc-4.1 */
#ifndef offsetof
# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 1))
# define offsetof(type, field) __builtin_offsetof(type, field)
# else
# define offsetof(type, field) ((__size_t)(__uintptr_t)((const volatile void *)&((type *)0)->field))
# endif
#endif
/* Linux-like "container_of". It returns a pointer to the structure of type /* Linux-like "container_of". It returns a pointer to the structure of type
* <type> which has its member <name> stored at address <ptr>. * <type> which has its member <name> stored at address <ptr>.
*/ */