realloc: Return unchanged if request is within usable size
authorSiddhesh Poyarekar <siddhesh@sourceware.org>
Mon, 28 Nov 2022 17:26:46 +0000 (12:26 -0500)
committerSiddhesh Poyarekar <siddhesh@sourceware.org>
Thu, 8 Dec 2022 16:23:43 +0000 (11:23 -0500)
commitf4f2ca1509288f6f780af50659693a89949e7e46
tree90dd3a1bf876ffc694e0c0da99e836f2749bb37d
parent929ea132b43dbec93e5f4d28f316d37ede91a635
realloc: Return unchanged if request is within usable size

If there is enough space in the chunk to satisfy the new size, return
the old pointer as is, thus avoiding any locks or reallocations.  The
only real place this has a benefit is in large chunks that tend to get
satisfied with mmap, since there is a large enough spare size (up to a
page) for it to matter.  For allocations on heap, the extra size is
typically barely a few bytes (up to 15) and it's unlikely that it would
make much difference in performance.

Also added a smoke test to ensure that the old pointer is returned
unchanged if the new size to realloc is within usable size of the old
pointer.

Signed-off-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
Reviewed-by: DJ Delorie <dj@redhat.com>
malloc/malloc.c
malloc/tst-realloc.c