Skip to contentSkip to conversation

libbpf bpf_helpers: Use __builtin_offsetof for offsetof

PATCH Applied Ian Rogers 3 participants 3 messages Jul 20, 2020 – Jul 21, 2020

Subsystems (4)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH v2 1/1 libbpf bpf_helpers: Use __builtin_offsetof for offsetof

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin that is available since llvm 3.7 (the first with a BPF backend).

---
 tools/lib/bpf/bpf_helpers.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf_helpers.h +1 -1
index a510d8ed716f..bc14db706b88 100644
@@ -40,7 +40,7 @@
  * Helper macro to manipulate data structures
  */
 #ifndef offsetof
-#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
+#define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
 #endif
 #ifndef container_of
 #define container_of(ptr, type, member)				\
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
Acked-by on v2 1/1
#1 lore raw
Quoted 10 lines · Ian Rogers

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin that is available since llvm 3.7 (the first with a BPF backend).

---

Quoted 19 lines
 tools/lib/bpf/bpf_helpers.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/lib/bpf/bpf_helpers.h +1 -2
index a510d8ed716f..bc14db706b88 100644
@@ -40,7 +40,7 @@
  * Helper macro to manipulate data structures
  */
 #ifndef offsetof
-#define offsetof(TYPE, MEMBER)  ((size_t)&((TYPE *)0)->MEMBER)
+#define offsetof(TYPE, MEMBER)  __builtin_offsetof(TYPE, MEMBER)
 #endif
 #ifndef container_of
 #define container_of(ptr, type, member)                                \
--

2.28.0.rc0.105.gf9edc3c819-goog

#2 lore raw
Quoted 10 lines · Andrii Nakryiko
Quoted 8 lines · Ian Rogers

The non-builtin route for offsetof has a dependency on size_t from stdlib.h/stdint.h that is undeclared and may break targets. The offsetof macro in bpf_helpers may disable the same macro in other headers that have a #ifdef offsetof guard. Rather than add additional dependencies improve the offsetof macro declared here to use the builtin that is available since llvm 3.7 (the first with a BPF backend).

Quoted 4 lines
Quoted 2 lines

---

Applied. Thanks