aboutsummaryrefslogtreecommitdiff
path: root/malloc/malloc.c
diff options
context:
space:
mode:
Diffstat (limited to 'malloc/malloc.c')
-rw-r--r--malloc/malloc.c10
1 files changed, 10 insertions, 0 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 2a61c8b..ef8c794 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1100,6 +1100,8 @@ static void munmap_chunk(mchunkptr p);
static mchunkptr mremap_chunk(mchunkptr p, size_t new_size);
#endif
+static size_t musable (void *mem);
+
/* ------------------ MMAP support ------------------ */
@@ -3396,6 +3398,14 @@ __libc_realloc (void *oldmem, size_t bytes)
if (__glibc_unlikely (mtag_enabled))
*(volatile char*) oldmem;
+ /* Return the chunk as is whenever possible, i.e. there's enough usable space
+ but not so much that we end up fragmenting the block. We use the trim
+ threshold as the heuristic to decide the latter. */
+ size_t usable = musable (oldmem);
+ if (bytes <= usable
+ && (unsigned long) (usable - bytes) <= mp_.trim_threshold)
+ return oldmem;
+
/* chunk corresponding to oldmem */
const mchunkptr oldp = mem2chunk (oldmem);
/* its size */