[PATCH 5/5] newlib: use empty reentrancy context arguments with TLS
H. Peter Anvin
hpa@zytor.com
Thu Dec 3 22:01:41 GMT 2026
If the compiler supports it, define __reent_ptr_t as an empty
structure when thread-local reentrancy is enabled
(--enable-newlib-reent-thread-local.) This avoids the overhead of
passing a pointer argument around which is always going to be NULL.
Use a private zero-length array member in C++ to match the zero-sized C
argument ABI without exposing representation details to callers.
Construct an empty __NULL_REENT_PTR value instead of passing a null
pointer.
This also applies to single-threaded TLS builds. Non-TLS single-threaded
builds still use explicit reentrancy contexts and must retain pointers,
since single-threaded operation does not make those contexts redundant.
Signed-off-by: H. Peter Anvin <hpa@zytor.com>
---
newlib/libc/include/sys/_types.h | 20 ++++++++++++++++++++
newlib/libc/include/sys/reent.h | 8 ++++++++
2 files changed, 28 insertions(+)
diff --git a/newlib/libc/include/sys/_types.h b/newlib/libc/include/sys/_types.h
index f8891f9cde99..29cae9c9f2a5 100644
--- a/newlib/libc/include/sys/_types.h
+++ b/newlib/libc/include/sys/_types.h
@@ -27,7 +27,27 @@
#include <machine/_types.h>
struct _reent;
+
+#if !defined (_REENT_THREAD_LOCAL) || !defined (_HAVE_EMPTY_STRUCT)
+/* Need an actual reent pointer. */
typedef struct _reent *__reent_ptr_t;
+#else
+/* Create a completely empty data type using gcc/clang extensions */
+#ifdef __cplusplus
+/* Empty C++ class: this representation is ABI-compatible, but is not C. */
+extern "C++" /* Suppress any C-compatibility warning */
+{
+ __extension__ struct __reent_ptr_t
+ {
+ private:
+ char __empty[0];
+ };
+}
+#else
+/* Empty C structure */
+__extension__ typedef struct __reent_ptr_t { } __reent_ptr_t;
+#endif
+#endif
#ifndef __machine_blkcnt_t_defined
typedef long __blkcnt_t;
diff --git a/newlib/libc/include/sys/reent.h b/newlib/libc/include/sys/reent.h
index bbe953c5a993..10d0c3837008 100644
--- a/newlib/libc/include/sys/reent.h
+++ b/newlib/libc/include/sys/reent.h
@@ -43,7 +43,15 @@ typedef __uint32_t __ULong;
struct _reent;
+#if defined (_REENT_THREAD_LOCAL) && defined (_HAVE_EMPTY_STRUCT)
+#ifdef __cplusplus
+#define __NULL_REENT_PTR (__reent_ptr_t ())
+#else
+#define __NULL_REENT_PTR (__extension__ (__reent_ptr_t) {})
+#endif
+#else
#define __NULL_REENT_PTR NULL
+#endif
struct __locale_t;
--
2.56.1
More information about the Newlib
mailing list