[PATCH] malloc: Move mmap code out of __libc_free hotpath

DJ Delorie dj@redhat.com
Mon Mar 24 22:16:27 GMT 2025


Wilco Dijkstra <Wilco.Dijkstra@arm.com> writes:
> Currently __libc_free checks for a freed mmap chunk in the fast path.
> Also errno is always saved and restored to preserve it.  Since mmap chunks
> are larger than the largest tcache chunk, it is safe to delay this and
> handle tcache, smallbin and medium bin blocks first.  Move saving of errno
> to cases that actually need it.

> +  /* Preserve errno when called from free() - _int_malloc may corrupt it.  */
> +  int err = errno;

We're in tcache_init so the performance hit here is negligible.

>  
> -
>    if (ar_ptr != NULL)
>      __libc_lock_unlock (ar_ptr->mutex);
>  
> +  __set_errno (err);
> +

Ok.

> @@ -3446,37 +3450,15 @@ __libc_free (void *mem)
>    if (__glibc_unlikely (mtag_enabled))
>      *(volatile char *)mem;
>  
> -  int err = errno;
> -
>    p = mem2chunk (mem);
>  
> -  if (chunk_is_mmapped (p))                       /* release mmapped memory. */
> -    {
> -      /* See if the dynamic brk/mmap threshold needs adjusting.
> -	 Dumped fake mmapped chunks do not affect the threshold.  */
> -      if (!mp_.no_dyn_threshold
> -          && chunksize_nomask (p) > mp_.mmap_threshold
> -          && chunksize_nomask (p) <= DEFAULT_MMAP_THRESHOLD_MAX)
> -        {

... the rest of this looks ok, but it leaves _libc_free and _int_free as
being very small functions.  Do we need them?  Or are we relying on the
inlining to make them efficient?

LGTM
Reviewed-by: DJ Delorie <dj@redhat.com>



More information about the Libc-alpha mailing list