[PATCH 3/6] malloc: Put MALLOC_ALIGNMENT depends in one header
Muhammad Kamran
muhammad.kamran@arm.com
Thu Sep 24 14:35:06 GMT 2026
From: Muhammad Kamran <Muhammad.Kamran@arm.com>
The 'malloc-alignment.h' header defines macro MALLOC_ALIGNMENT that
depends on SIZE_SZ which in turn depends on INTERNAL_SIZE_T.
These dependencies used to be defined in 'malloc-size.h'. We should
either merge these two headers or move the dependencies or change the
'malloc-alignment.h' header to make sure it is self-consistent.
This commit does the latter. In addition we fix the tst-mallocalign1.c
test that uses the MALLOC_ALIGN_MASK internal to malloc. Since it's
been moved to 'malloc-alignment.h' it makes sense to include this
header into the test source.
Co-authored-by: Yury Khrustalev <yury.khrustalev@arm.com>
---
malloc/tst-mallocalign1.c | 2 +-
sysdeps/generic/malloc-alignment.h | 43 +++++++++++++++++++++++++++++-
sysdeps/generic/malloc-size.h | 42 +----------------------------
sysdeps/i386/malloc-alignment.h | 14 ++++++++++
4 files changed, 58 insertions(+), 43 deletions(-)
diff --git a/malloc/tst-mallocalign1.c b/malloc/tst-mallocalign1.c
index 0ef0674b46..1ba7e69d2c 100644
--- a/malloc/tst-mallocalign1.c
+++ b/malloc/tst-mallocalign1.c
@@ -19,7 +19,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
-#include <malloc-size.h>
+#include <malloc-alignment.h>
#include <support/check.h>
static void *
diff --git a/sysdeps/generic/malloc-alignment.h b/sysdeps/generic/malloc-alignment.h
index 28c50abe1f..8684b9efd1 100644
--- a/sysdeps/generic/malloc-alignment.h
+++ b/sysdeps/generic/malloc-alignment.h
@@ -1,4 +1,5 @@
-/* Define MALLOC_ALIGNMENT for malloc. Generic version.
+/* Define INTERNAL_SIZE_T and MALLOC_ALIGNMENT for malloc.
+ Generic version.
Copyright (C) 2017-2026 Free Software Foundation, Inc.
This file is part of the GNU C Library.
@@ -19,6 +20,44 @@
#ifndef _GENERIC_MALLOC_ALIGNMENT_H
#define _GENERIC_MALLOC_ALIGNMENT_H
+#include <stddef.h>
+
+/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
+ chunk sizes.
+
+ The default version is the same as size_t.
+
+ While not strictly necessary, it is best to define this as an
+ unsigned type, even if size_t is a signed type. This may avoid some
+ artificial size limitations on some systems.
+
+ On a 64-bit machine, you may be able to reduce malloc overhead by
+ defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
+ expense of not being able to handle more than 2^32 of malloced
+ space. If this limitation is acceptable, you are encouraged to set
+ this unless you are on a platform requiring 16byte alignments. In
+ this case the alignment requirements turn out to negate any
+ potential advantages of decreasing size_t word size.
+
+ Implementors: Beware of the possible combinations of:
+ - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
+ and might be the same width as int or as long
+ - size_t might have different width and signedness as INTERNAL_SIZE_T
+ - int and long might be 32 or 64 bits, and might be the same width
+
+ To deal with this, most comparisons and difference computations
+ among INTERNAL_SIZE_Ts should cast them to unsigned long, being
+ aware of the fact that casting an unsigned int to a wider long does
+ not sign-extend. (This also makes checking for negative numbers
+ awkward.) Some of these casts result in harmless compiler warnings
+ on some systems. */
+#ifndef INTERNAL_SIZE_T
+# define INTERNAL_SIZE_T size_t
+#endif
+
+/* The corresponding word size. */
+#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
+
/* MALLOC_ALIGNMENT is the minimum alignment for malloc'ed chunks. It
must be a power of two at least 2 * SIZE_SZ, even on machines for
which smaller alignments would suffice. It may be defined as larger
@@ -27,5 +66,7 @@
#define MALLOC_ALIGNMENT (2 * SIZE_SZ < __alignof__ (long double) \
? __alignof__ (long double) : 2 * SIZE_SZ)
+/* The corresponding bit mask value. */
+#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
#endif /* !defined(_GENERIC_MALLOC_ALIGNMENT_H) */
diff --git a/sysdeps/generic/malloc-size.h b/sysdeps/generic/malloc-size.h
index adaf01619d..5b12bc9005 100644
--- a/sysdeps/generic/malloc-size.h
+++ b/sysdeps/generic/malloc-size.h
@@ -1,5 +1,4 @@
-/* Define INTERNAL_SIZE_T, SIZE_SZ, MALLOC_ALIGNMENT and MALLOC_ALIGN_MASK
- for malloc.
+/* Size-related definitions for malloc: generic version.
Copyright (C) 2021-2026 Free Software Foundation, Inc.
This file is part of the GNU C Library.
@@ -20,45 +19,6 @@
#ifndef _GENERIC_MALLOC_SIZE_H
#define _GENERIC_MALLOC_SIZE_H
-/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
- chunk sizes.
-
- The default version is the same as size_t.
-
- While not strictly necessary, it is best to define this as an
- unsigned type, even if size_t is a signed type. This may avoid some
- artificial size limitations on some systems.
-
- On a 64-bit machine, you may be able to reduce malloc overhead by
- defining INTERNAL_SIZE_T to be a 32 bit `unsigned int' at the
- expense of not being able to handle more than 2^32 of malloced
- space. If this limitation is acceptable, you are encouraged to set
- this unless you are on a platform requiring 16byte alignments. In
- this case the alignment requirements turn out to negate any
- potential advantages of decreasing size_t word size.
-
- Implementors: Beware of the possible combinations of:
- - INTERNAL_SIZE_T might be signed or unsigned, might be 32 or 64 bits,
- and might be the same width as int or as long
- - size_t might have different width and signedness as INTERNAL_SIZE_T
- - int and long might be 32 or 64 bits, and might be the same width
-
- To deal with this, most comparisons and difference computations
- among INTERNAL_SIZE_Ts should cast them to unsigned long, being
- aware of the fact that casting an unsigned int to a wider long does
- not sign-extend. (This also makes checking for negative numbers
- awkward.) Some of these casts result in harmless compiler warnings
- on some systems. */
-#ifndef INTERNAL_SIZE_T
-# define INTERNAL_SIZE_T size_t
-#endif
-
-/* The corresponding word size. */
-#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
-
#include <malloc-alignment.h>
-/* The corresponding bit mask value. */
-#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
-
#endif /* _GENERIC_MALLOC_SIZE_H */
diff --git a/sysdeps/i386/malloc-alignment.h b/sysdeps/i386/malloc-alignment.h
index d1e60948c9..ff870c2f73 100644
--- a/sysdeps/i386/malloc-alignment.h
+++ b/sysdeps/i386/malloc-alignment.h
@@ -19,6 +19,20 @@
#ifndef _I386_MALLOC_ALIGNMENT_H
#define _I386_MALLOC_ALIGNMENT_H
+#include <stddef.h>
+
+/* INTERNAL_SIZE_T is the word-size used for internal bookkeeping of
+ chunk sizes. See sysdeps/generic/malloc-alignment.h for details. */
+#ifndef INTERNAL_SIZE_T
+# define INTERNAL_SIZE_T size_t
+#endif
+
+/* The corresponding word size. */
+#define SIZE_SZ (sizeof (INTERNAL_SIZE_T))
+
#define MALLOC_ALIGNMENT 16
+/* The corresponding bit mask value. */
+#define MALLOC_ALIGN_MASK (MALLOC_ALIGNMENT - 1)
+
#endif /* !defined(_I386_MALLOC_ALIGNMENT_H) */
--
2.43.0
More information about the Libc-alpha
mailing list