[PATCH] malloc: Cleanup libc_realloc

DJ Delorie dj@redhat.com
Fri Jun 6 20:49:38 GMT 2025


Wilco Dijkstra <Wilco.Dijkstra@arm.com> writes:
> +  /* realloc of null is supposed to be same as malloc */
> +  if (oldmem == NULL)
> +    return __libc_malloc (bytes);
> +
>  #if REALLOC_ZERO_BYTES_FREES
> -  if (bytes == 0 && oldmem != NULL)
> +  if (bytes == 0)
>      {
>        __libc_free (oldmem); return NULL;
>      }
>  #endif
>  
> -  /* realloc of null is supposed to be same as malloc */
> -  if (oldmem == NULL)
> -    return __libc_malloc (bytes);
> -

Ok.

>    if (bytes <= usable)
>      {
>        size_t difference = usable - bytes;
> -      if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T)
> -	  || (chunk_is_mmapped (oldp) && difference <= GLRO (dl_pagesize)))
> +      if ((unsigned long) difference < 2 * sizeof (INTERNAL_SIZE_T))
>  	return oldmem;
>      }

Why is this unneeded?  Growing an mmap'd chunk into the rest of its page
seems a useful optimization here.

>    /* its size */
>    const INTERNAL_SIZE_T oldsize = chunksize (oldp);
>  
> -  if (chunk_is_mmapped (oldp))
> -    ar_ptr = NULL;
> -  else
> -    ar_ptr = arena_for_chunk (oldp);
> -
>    /* Little security check which won't hurt performance: the allocator
>       never wraps around at the end of the address space.  Therefore
>       we can exclude some size values which might appear here by
> @@ -3551,6 +3545,8 @@ __libc_realloc (void *oldmem, size_t bytes)
>        return newmem;
>      }
>  
> +  ar_ptr = arena_for_chunk (oldp);
> +
>    if (SINGLE_THREAD_P)
>      {
>        newp = _int_realloc (ar_ptr, oldp, oldsize, nb);

Ok.



More information about the Libc-alpha mailing list