From 2adc3f0db1877dbe9801e6dacfa9ae8df8879549 Mon Sep 17 00:00:00 2001 From: Brooks Davis Date: Thu, 19 Feb 2026 15:05:22 +0000 Subject: [PATCH] stddef.h: centralize definition of offsetof() Move to sys/_offsetof.h and use __builtin_offsetof() instead of __offsetof to avoid reintroducing sys/cdefs.h polution in stddef.h. This has the side effect of allowing sys/stddef.h to be included after stddef.h which can happen in compatability headers. Effort: CHERI upstreaming Sponsored by: DARPA, AFRL Reviewed by: imp, kib Differential Revision: https://reviews.freebsd.org/D55307 --- include/stddef.h | 5 +---- sys/sys/_offsetof.h | 19 +++++++++++++++++++ sys/sys/stddef.h | 3 +-- 3 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 sys/sys/_offsetof.h diff --git a/include/stddef.h b/include/stddef.h index 978a34affc0..9c7f1f14728 100644 --- a/include/stddef.h +++ b/include/stddef.h @@ -33,6 +33,7 @@ #define _STDDEF_H_ #include +#include #include #include @@ -68,10 +69,6 @@ typedef __max_align_t max_align_t; #endif #endif -#ifndef offsetof -#define offsetof(type, field) __builtin_offsetof(type, field) -#endif - #if __EXT1_VISIBLE /* ISO/IEC 9899:2011 K.3.3.2 */ #ifndef _RSIZE_T_DEFINED diff --git a/sys/sys/_offsetof.h b/sys/sys/_offsetof.h new file mode 100644 index 00000000000..25982fad774 --- /dev/null +++ b/sys/sys/_offsetof.h @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2026 Capabilities Limited + * + * SPDX-License-Identifier: BSD-2-Clause + * + * This software was developed by SRI International, the University of + * Cambridge Computer Laboratory (Department of Computer Science and + * Technology), and Capabilities Limited under Defense Advanced Research + * Projects Agency / Air Force Research Laboratory (DARPA/AFRL) Contract + * No. FA8750-24-C-B047 ("DEC"). + */ +#ifndef _SYS__OFFSETOF_H_ +#define _SYS__OFFSETOF_H_ + +#ifndef offsetof +#define offsetof(type, field) __builtin_offsetof(type, field) +#endif + +#endif /* _SYS__OFFSETOF_H_ */ diff --git a/sys/sys/stddef.h b/sys/sys/stddef.h index 90186eed762..911755f71aa 100644 --- a/sys/sys/stddef.h +++ b/sys/sys/stddef.h @@ -31,6 +31,7 @@ #include #include +#include #include #include @@ -46,6 +47,4 @@ typedef __ptrdiff_t ptrdiff_t; #define _PTRDIFF_T_DECLARED #endif -#define offsetof(type, field) __offsetof(type, field) - #endif /* !_SYS_STDDEF_H_ */