Skip to content

Commit fc24b8b

Browse files
Merge pull request #12 from Theodus/override
Avoid unnecessary allocation from realloc
2 parents c0ceb6e + ae3f9f6 commit fc24b8b

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/mem/slab.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ namespace snmalloc
7171

7272
meta.debug_slab_invariant(is_short(), this);
7373

74-
if (zero_mem == YesZero)
74+
if constexpr (zero_mem == YesZero)
7575
{
7676
if (rsize < PAGE_ALIGNED_SIZE)
7777
memory_provider.zero(p, rsize);

src/override/malloc.cc

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,16 @@ extern "C"
8080
"Calling realloc on pointer that is not to the start of an allocation");
8181
}
8282
#endif
83+
size_t sz = Alloc::alloc_size(ptr);
84+
// Keep the current allocation if the given size is in the same sizeclass.
85+
if (sz == sizeclass_to_size(size_to_sizeclass(size)))
86+
return ptr;
87+
8388
void* p = SNMALLOC_NAME_MANGLE(malloc)(size);
84-
if (p)
89+
if (p != nullptr)
8590
{
8691
assert(p == Alloc::external_pointer<Start>(p));
87-
size_t sz =
88-
(std::min)(size, SNMALLOC_NAME_MANGLE(malloc_usable_size)(ptr));
92+
sz = (std::min)(size, sz);
8993
memcpy(p, ptr, sz);
9094
SNMALLOC_NAME_MANGLE(free)(ptr);
9195
}

0 commit comments

Comments
 (0)