[Bug malloc/14581] memalign allocations are often not reused after free
henri at henrimenke dot de
sourceware-bugzilla@sourceware.org
Thu Jul 23 16:22:13 GMT 2026
https://sourceware.org/bugzilla/show_bug.cgi?id=14581
Henri Menke <henri at henrimenke dot de> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |henri at henrimenke dot de
--- Comment #26 from Henri Menke <henri at henrimenke dot de> ---
>From playing around a little bit and having a look at the code this is my take:
Very small allocations go into the tcache and are always perfectly reused,
because when aligned memory is requested, the tcache is scanned linearly until
a matching chunk is found. Allocations larger than the mmap threshold are
never reused, but that is by design. For intermediate allocations the logic
fails.
When an aligned allocation is requested, the allocation is padded by MINSIZE
and rounded up to the alignment. Then the excess is split into a separate
chunk that is freed immediately and the aligned chunk is returned. Any
subsequent *identical* aligned allocation will not be able to reuse the
previous chunk, because any information about alignment has been trimmed and
the chunk looks just like any other (in particular it's missing MINSIZE and
alignment).
This would be fixable by scanning all chunks for one with matching size and
alignment, but in contrast to the tcache this is unbounded. Another possible
fix is to keep the information that the chunk is aligned in the header and also
not trim the alignment, but this leads to an internal fragmentation of up to
alignment + MINSIZE per live allocation, which is fine for small alignment but
disastrous for, e.g. alignment at page boundary.
For the latter case of keeping a shadow header, I have included a rather crude
patch that explores this direction. This could (and should) be improved to
only keep the shadow header when the ratio of wasted space to total allocation
size is small and probably also be hidden behind some kind of tunable. Since I
am not at all familiar with developing memory allocators, I would be very happy
if the experts coul share their opinion on this.
Kind regards,
Henri
--
You are receiving this mail because:
You are on the CC list for the bug.
More information about the Glibc-bugs
mailing list