diff mbox series

[v2] libbpf bpf_helpers: Use __builtin_offsetof for offsetof

Message ID 20200720061741.1514673-1-irogers@google.com
State Accepted
Delegated to: BPF Maintainers
Headers show
Series [v2] libbpf bpf_helpers: Use __builtin_offsetof for offsetof | expand

Commit Message

Ian Rogers July 20, 2020, 6:17 a.m. UTC
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).

Signed-off-by: Ian Rogers <irogers@google.com>
---
 tools/lib/bpf/bpf_helpers.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Comments

Andrii Nakryiko July 20, 2020, 6:21 a.m. UTC | #1
On Sun, Jul 19, 2020 at 11:18 PM Ian Rogers <irogers@google.com> wrote:
>
> 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).
>
> Signed-off-by: Ian Rogers <irogers@google.com>
> ---

Acked-by: Andrii Nakryiko <andriin@fb.com>

>  tools/lib/bpf/bpf_helpers.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
> index a510d8ed716f..bc14db706b88 100644
> --- a/tools/lib/bpf/bpf_helpers.h
> +++ b/tools/lib/bpf/bpf_helpers.h
> @@ -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
>
Alexei Starovoitov July 21, 2020, 7:26 p.m. UTC | #2
On Sun, Jul 19, 2020 at 11:21 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Sun, Jul 19, 2020 at 11:18 PM Ian Rogers <irogers@google.com> wrote:
> >
> > 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).
> >
> > Signed-off-by: Ian Rogers <irogers@google.com>
> > ---
>
> Acked-by: Andrii Nakryiko <andriin@fb.com>

Applied. Thanks
diff mbox series

Patch

diff --git a/tools/lib/bpf/bpf_helpers.h b/tools/lib/bpf/bpf_helpers.h
index a510d8ed716f..bc14db706b88 100644
--- a/tools/lib/bpf/bpf_helpers.h
+++ b/tools/lib/bpf/bpf_helpers.h
@@ -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)				\