Skip to content

Commit

Permalink
Avoid unnecessary allocation from realloc
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus committed Feb 2, 2019
1 parent c0ceb6e commit 85cf5dd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/mem/slab.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 5 additions & 4 deletions src/override/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 85cf5dd

Please sign in to comment.