Avoid unnecessary allocation from realloc

This commit is contained in:
theodus
2019-02-02 14:47:06 -05:00
parent c0ceb6e08f
commit 85cf5dd097
2 changed files with 6 additions and 5 deletions

View File

@@ -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);

View File

@@ -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<Start>(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;