From 85cf5dd097226465334821274596b7c7b7ed80a4 Mon Sep 17 00:00:00 2001 From: theodus Date: Sat, 2 Feb 2019 14:47:06 -0500 Subject: [PATCH] Avoid unnecessary allocation from realloc --- src/mem/slab.h | 2 +- src/override/malloc.cc | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/mem/slab.h b/src/mem/slab.h index 6387dc6..67f9712 100644 --- a/src/mem/slab.h +++ b/src/mem/slab.h @@ -71,7 +71,7 @@ namespace snmalloc meta.debug_slab_invariant(is_short(), this); - if (zero_mem == YesZero) + if constexpr (zero_mem == YesZero) { if (rsize < PAGE_ALIGNED_SIZE) memory_provider.zero(p, rsize); diff --git a/src/override/malloc.cc b/src/override/malloc.cc index 5e3bf1f..7190f99 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,13 +80,14 @@ 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)) + return ptr; + void* p = SNMALLOC_NAME_MANGLE(malloc)(size); - if (p) + if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - size_t sz = - (std::min)(size, SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)); - memcpy(p, ptr, sz); + memcpy(p, ptr, size); SNMALLOC_NAME_MANGLE(free)(ptr); } return p;