]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
IMPORT: ebtree: add a definition of offsetof()
authorWilly Tarreau <w@1wt.eu>
Sat, 13 Sep 2025 12:21:54 +0000 (14:21 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 17 Sep 2025 12:30:32 +0000 (14:30 +0200)
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.

include/import/ebtree.h

index 2506e288ba4750b36e11e67adf99d74349237fdb..3cd3ae9be714acb0f1d7408ac2b60c7f3f06e221 100644 (file)
 #ifndef _EBTREE_H
 #define _EBTREE_H
 
+#include <stddef.h>
 #include <stdlib.h>
 #include <import/ebtree-t.h>
 #include <haproxy/api.h>
@@ -365,6 +366,15 @@ static inline unsigned int flsnz64(unsigned long long x)
 #define fls64(x) flsnz64(x)
 #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
  * <type> which has its member <name> stored at address <ptr>.
  */