git: 2adc3f0db187 - main - stddef.h: centralize definition of offsetof()

From: Brooks Davis <brooks_at_FreeBSD.org>
Date: Thu, 19 Feb 2026 15:10:50 UTC
The branch main has been updated by brooks:

URL: https://cgit.FreeBSD.org/src/commit/?id=2adc3f0db1877dbe9801e6dacfa9ae8df8879549

commit 2adc3f0db1877dbe9801e6dacfa9ae8df8879549
Author:     Brooks Davis <brooks@FreeBSD.org>
AuthorDate: 2026-02-19 15:05:22 +0000
Commit:     Brooks Davis <brooks@FreeBSD.org>
CommitDate: 2026-02-19 15:10:32 +0000

    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(-)

diff --git a/include/stddef.h b/include/stddef.h
index 978a34affc0a..9c7f1f147289 100644
--- a/include/stddef.h
+++ b/include/stddef.h
@@ -33,6 +33,7 @@
 #define _STDDEF_H_
 
 #include <sys/_null.h>
+#include <sys/_offsetof.h>
 #include <sys/_types.h>
 #include <sys/_visible.h>
 
@@ -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 000000000000..25982fad774a
--- /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 90186eed7624..911755f71aa8 100644
--- a/sys/sys/stddef.h
+++ b/sys/sys/stddef.h
@@ -31,6 +31,7 @@
 
 #include <sys/cdefs.h>
 #include <sys/_null.h>
+#include <sys/_offsetof.h>
 #include <sys/_types.h>
 #include <sys/_visible.h>
 
@@ -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_ */