[PATCH] malloc: Improve realloc

Paul Eggert eggert@cs.ucla.edu
Thu Nov 19 17:20:21 GMT 2026


On 2026-11-19 08:37, Wilco Dijkstra wrote:
> +  if (bytes > PTRDIFF_MAX)
> +    {
> +      __set_errno (ENOMEM);
> +      return NULL;
> +    }

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).


More information about the Libc-alpha mailing list