diff --git a/src/mem/slab.h b/src/mem/slab.h index 6387dc636..67f9712d9 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 5e3bf1f55..218191e58 100644 --- a/src/override/malloc.cc +++ b/src/override/malloc.cc @@ -80,12 +80,16 @@ extern "C" "Calling realloc on pointer that is not to the start of an allocation"); } #endif + size_t sz = Alloc::alloc_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) + if (p != nullptr) { assert(p == Alloc::external_pointer(p)); - size_t sz = - (std::min)(size, SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr)); + sz = (std::min)(size, sz); memcpy(p, ptr, sz); SNMALLOC_NAME_MANGLE(free)(ptr); }