[PATCH] realloc: Return unchanged if request is within usable size

Wilco Dijkstra Wilco.Dijkstra@arm.com
Tue Dec 6 12:45:34 GMT 2022


Hi Siddhesh,

>> We do need to handle the case where the size is smaller than the usable size since
>> we'd still want to free the unused portion if it is a significant fraction of the total size.
>
> I'll make this a function of trim_threshold so that the reuse happens 
> only if the difference between the request and usable size is less than 
> the trim_threshold.

That's better, but that really only helps the mmap case, and all other reallocs will
never release memory due to the 128KB default value of trim_threshold.
That's something we should explicitly decide/document and remove any redundant
code from realloc as a result.

>> Note the mmap code always tries to remap before checking the new size is smaller
>> than usable size - so will end up remapping even if you downsize by one page.
>
> I don't see that; I've put the musable check before the 
> chunk_is_mmapped() check.  What am I missing?

I'm talking about the committed version, basically if there is any change in number of
pages it will do a system call. With your change there must be at least 128KB worth of
pages to be freed. We should do something similar for growing mmaps - large blocks
should never grow/shrink a single page at a time.

Cheers,
Wilco


More information about the Libc-alpha mailing list