aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjoern A. Zeeb <bz@FreeBSD.org>2026-04-14 15:06:56 +0000
committerBjoern A. Zeeb <bz@FreeBSD.org>2026-04-18 01:12:12 +0000
commit524df650a92f648e19ba27d6727bdc79c8efdbbb (patch)
tree197639356c810088a27d1ded0cbb8e5abb9f12f1
parent35b90c21f48056e85b70dbbe2209b6c3a4927315 (diff)
LinuxKPI: conditionally add __flex_counter()
__flex_counter() is used by overflow.h and needed for "flex allocations". It is either a void * typed 0 (NULL) (like this for _Generic checks), or uses __builtin_counted_by_ref. The latter was added to gcc and llvm fairly recently and while for gcc the __has_builtin() check suffices, clang had parts broken until recently so needs an extra check for the next major version. The fixed hash is currently not part of any tag to use, so we play it save (and hope 23 will have it). It will be a while until we will see the builting to be used but at least we will be prepared for it. See inline comments for the commit hashes and versions which added the feature. Sponsored by: The FreeBSD Foundation MFC after: 3 days Reviewed by: dumbbell Differential Revision: https://reviews.freebsd.org/D56393
-rw-r--r--sys/compat/linuxkpi/common/include/linux/compiler_types.h13
1 files changed, 13 insertions, 0 deletions
diff --git a/sys/compat/linuxkpi/common/include/linux/compiler_types.h b/sys/compat/linuxkpi/common/include/linux/compiler_types.h
index 7151c03de690..25e69f5afd8e 100644
--- a/sys/compat/linuxkpi/common/include/linux/compiler_types.h
+++ b/sys/compat/linuxkpi/common/include/linux/compiler_types.h
@@ -42,4 +42,17 @@
#define __same_type(a, b) __builtin_types_compatible_p(typeof(a), typeof(b))
+/*
+ * __builtin_counted_by_ref was introduced to
+ * - gcc in e7380688fa59 with a first release tag of gcc-15.1.0,
+ * - llvm in 7475156d49406 with a first release tag of llvmorg-20.1.0-rc1
+ * but cannot be used before 23 (22.x.y possibly) (see 09a3d830a888).
+ */
+#if (__has_builtin(__builtin_counted_by_ref)) && \
+ (!defined(__clang__) || (__clang_major__ >= 23))
+#define __flex_counter(_field) __builtin_counted_by_ref(_field)
+#else
+#define __flex_counter(_field) ((void *)NULL)
+#endif
+
#endif