[PATCH] malloc: Add realloc_zero tunable
Sam James
sam@gentoo.org
Thu Nov 19 18:52:34 GMT 2026
Wilco Dijkstra <Wilco.Dijkstra@arm.com> writes:
> Add a realloc_zero tunable to change realloc (p, 0) behaviour to either free (p)
> and return NULL or free (p) and return malloc (0). Set the default to
> REALLOC_ZERO_BYTES_FREES. This is done as part of an exceptional path so it only
> adds negligible overhead if realloc (p, 0) is used. This allows testing applications with
> both realloc behaviours. Documentation TBD.
>
> Passes regress, OK for commit?
Alex, I think this is of interest to you? We did discuss this
possibliity a few times in the various threads about this though I
couldn't find the exact reference to it.
>
> ---
>
> diff --git a/elf/dl-tunables.list b/elf/dl-tunables.list
> index 111649f14555f07cb63b44e21f6e0cd605c941ff..0b53639deb96dead64bc793e674056e27218f5c4 100644
> --- a/elf/dl-tunables.list
> +++ b/elf/dl-tunables.list
> @@ -87,6 +87,11 @@ glibc {
> type: SIZE_T
> minval: 0
> }
> + realloc_zero {
> + type: INT_32
> + minval: 0
> + maxval: 1
> + }
> }
>
> rtld {
> diff --git a/malloc/arena.c b/malloc/arena.c
> index d0f1003ad4b7fc72a39a8779faa01a7e9f786d86..5332a261427cb4420c37ad90d0f3e11eedbf4063 100644
> --- a/malloc/arena.c
> +++ b/malloc/arena.c
> @@ -242,6 +242,7 @@ TUNABLE_CALLBACK_FNDECL (set_tcache_max, size_t)
> TUNABLE_CALLBACK_FNDECL (set_tcache_count, size_t)
> #endif
> TUNABLE_CALLBACK_FNDECL (set_hugetlb, size_t)
> +TUNABLE_CALLBACK_FNDECL (set_realloc_zero, int32_t)
>
> #if USE_TCACHE
> static void tcache_key_initialize (void);
> @@ -282,6 +283,7 @@ __ptmalloc_init (void)
> TUNABLE_GET (tcache_count, size_t, TUNABLE_CALLBACK (set_tcache_count));
> # endif
> TUNABLE_GET (hugetlb, size_t, TUNABLE_CALLBACK (set_hugetlb));
> + TUNABLE_GET (realloc_zero, int32_t, TUNABLE_CALLBACK (set_realloc_zero));
>
> if (mp_.hp_pagesize > 0 && mp_.hp_pagesize <= heap_max_size ())
> {
> diff --git a/malloc/malloc.c b/malloc/malloc.c
> index 4993631f94d94bc15a7ee19c3c2782907b3a2559..f9a125c1b0076e0fb381cb900085ba39bb292b7b 100644
> --- a/malloc/malloc.c
> +++ b/malloc/malloc.c
> @@ -1524,6 +1524,7 @@ struct malloc_par
> /* Maximum number of chunks in each bucket. */
> size_t tcache_count;
> #endif
> + int realloc_zero;
> };
>
> /* There are several instances of this struct ("arenas") in this
> @@ -1548,13 +1549,13 @@ static struct malloc_par mp_ =
> .mmap_threshold = DEFAULT_MMAP_THRESHOLD,
> .trim_threshold = DEFAULT_TRIM_THRESHOLD,
> .arena_test = sizeof (long) == 4 ? 2 : 8,
> - .thp_mode = thp_mode_unknown
> + .thp_mode = thp_mode_unknown,
> #if USE_TCACHE
> - ,
> .tcache_count = TCACHE_FILL_COUNT,
> .tcache_small_bins = TCACHE_SMALL_BINS,
> .tcache_max_bytes = MAX_TCACHE_SMALL_SIZE + 1,
> #endif
> + .realloc_zero = REALLOC_ZERO_BYTES_FREES,
> };
>
> /*
> @@ -3092,15 +3093,14 @@ __libc_realloc (void *oldmem, size_t bytes)
> if (oldmem == NULL)
> return __libc_malloc (bytes);
>
> -#if REALLOC_ZERO_BYTES_FREES
> - if (bytes == 0)
> + if (bytes > PTRDIFF_MAX || bytes == 0)
> {
> - __libc_free (oldmem); return NULL;
> - }
> -#endif
> + if (bytes == 0)
> + {
> + __libc_free (oldmem);
> + return mp_.realloc_zero ? NULL : __libc_malloc (0);
> + }
>
> - if (bytes > PTRDIFF_MAX)
> - {
> __set_errno (ENOMEM);
> return NULL;
> }
> @@ -4777,6 +4777,13 @@ do_set_hugetlb (size_t value)
> return 0;
> }
>
> +static __always_inline int
> +do_set_realloc_zero (int32_t value)
> +{
> + mp_.realloc_zero = value;
> + return 0;
> +}
> +
> int
> __libc_mallopt (int param_number, int value)
> {
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 418 bytes
Desc: not available
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20261119/6d2081b3/attachment.sig>
More information about the Libc-alpha
mailing list