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

Florian Weimer fweimer@redhat.com
Tue Mar 25 09:11:43 GMT 2025


* Wilco Dijkstra:

> Hi Florian,
>
>>> 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.
>>
>> I think the “Since mmap chunks are larger than the largest tcache
>> chunk” part is not correct.  We can fall back to performing mmap for a
>> smallish allocation.  But I think it's still fine to put such chunks
>> into tcache.  Maybe add a comment to this effect?
>
> Well assuming minimum page size is 4KB it should not currently be possible
> for the smallest mmap chunk to get into tcache.

I think 2K alignment will produce a 2K chunk.  Still outside the range
of tcache, though.  I thought it reached further.

> However the available size of an mmap chunk is different, so it would
> go wrong unless we make the chunk layouts compatible (that would be a
> simplification that removes the special cases in memsize and musable).

Oh, I had forgotten about that.

In this case, we probably want some sort of assert that this doesn't go
wrong if someone changes TCACHE_MAX_BINS.  And the comment needs
updating that the number is no longer arbitrary.

Thanks,
Florian



More information about the Libc-alpha mailing list