From 2b8b4c884ffa5376101f3843153666db53ce0f94 Mon Sep 17 00:00:00 2001 From: theodus Date: Sat, 2 Feb 2019 17:28:02 -0500 Subject: [PATCH] Do not realloc if size is in the same sizeclass --- src/override/malloc.cc | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 7190f99..3a30922 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,14 +80,17 @@ extern "C" "Calling realloc on pointer that is not to the start of an allocation"); } #endif - if (size <= SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)) + size_t sz = SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr); + // Keep the current allocation if the given size is in the same sizeclass. + if (sz == sizeclass_to_size(size_to_sizeclass(size))) return ptr; void* p = SNMALLOC_NAME_MANGLE(malloc)(size); if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - memcpy(p, ptr, size); + sz = (std::min)(size, sz); + memcpy(p, ptr, sz); SNMALLOC_NAME_MANGLE(free)(ptr); } return p;