[PATCH] malloc: Improve realloc

Wilco Dijkstra Wilco.Dijkstra@arm.com
Thu Nov 19 18:52:42 GMT 2026


Hi Paul,

> A little bikeshedding here: in the typical REALLOC_ZERO_BYTES_FREES case, it would
> help performance slightly if we combined this test with the previous one, to save a 
> conditional branch. Something like this, perhaps:
>
>   #if REALLOC_ZERO_BYTES_FREES
>     if (! (0 < bytes && bytes <= PTRDIFF_MAX))
>       {
>         if (bytes == 0)
>           __libc_free (oldmem);
>         else
>           __set_errno (ENOMEM);
>         return NULL;
>       }
>   #else
>     if (bytes > PTRDIFF_MAX)
>       {
>         __set_errno (ENOMEM);
>         return NULL;
>       }
>   #endif
>
> GCC optimizes (! (0 < bytes && bytes <= PTRDIFF_MAX)) into ((ptrdiff_t) bytes <= 0).

Good point, we can just always do if (bytes == 0 || bytes > PTRDIFF_MAX).

See https://sourceware.org/pipermail/libc-alpha/2026-November/182267.html

Cheers,
Wilco


More information about the Libc-alpha mailing list