Skip to content

Commit

Permalink
Fix sizeclass rounding error
Browse files Browse the repository at this point in the history
  • Loading branch information
Theodus authored and mjp41 committed Jun 8, 2020
1 parent 333190a commit 61afa77
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 21 deletions.
6 changes: 5 additions & 1 deletion src/mem/sizeclass.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,10 +188,14 @@ namespace snmalloc

SNMALLOC_FAST_PATH static size_t round_size(size_t size)
{
if (size > size_to_sizeclass(NUM_SIZECLASSES - 1))
if (size > sizeclass_to_size(NUM_SIZECLASSES - 1))
{
return bits::next_pow2(size);
}
if (size == 0)
{
size = 1;
}
return sizeclass_to_size(size_to_sizeclass(size));
}
} // namespace snmalloc
57 changes: 37 additions & 20 deletions src/test/func/malloc/malloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,39 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
{
if (p != nullptr)
abort();
}
else
{
auto asize = our_malloc_usable_size(p);
if (asize < size)
{
printf(
"Usable size is %zu, but required to be at least %zu.\n", asize, size);
abort();
}

if (static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0)
abort();

our_free(p);
return;
}

const auto alloc_size = our_malloc_usable_size(p);
const auto expected_size = round_size(size);
if ((align == 1) && (alloc_size != expected_size))
{
printf(
"Usable size is %zu, but required to be %zu.\n",
alloc_size,
expected_size);
abort();
}
if ((align != 1) && (alloc_size < expected_size))
{
printf(
"Usable size is %zu, but required to be at least %zu.\n",
alloc_size,
expected_size);
abort();
}
if (static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0)
{
printf(
"Address is 0x%zx, but required to be aligned to 0x%zx.\n",
reinterpret_cast<uintptr_t>(p),
align);
abort();
}

our_free(p);
}

void test_calloc(size_t nmemb, size_t size, int err, bool null)
Expand Down Expand Up @@ -92,7 +109,7 @@ int main(int argc, char** argv)

test_realloc(our_malloc(64), 4194304, SUCCESS, false);

for (snmalloc::sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
for (sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
{
const size_t size = 1ULL << sc;
printf("malloc: %zu\n", size);
Expand All @@ -102,7 +119,7 @@ int main(int argc, char** argv)

test_calloc(0, 0, SUCCESS, false);

for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);

Expand All @@ -118,29 +135,29 @@ int main(int argc, char** argv)
test_calloc(0, size, SUCCESS, false);
}

for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);
test_realloc(our_malloc(size), size, SUCCESS, false);
test_realloc(our_malloc(size), 0, SUCCESS, true);
test_realloc(nullptr, size, SUCCESS, false);
test_realloc(our_malloc(size), (size_t)-1, ENOMEM, true);
for (snmalloc::sizeclass_t sc2 = 0; sc2 < NUM_SIZECLASSES; sc2++)
for (sizeclass_t sc2 = 0; sc2 < NUM_SIZECLASSES; sc2++)
{
const size_t size2 = sizeclass_to_size(sc2);
test_realloc(our_malloc(size), size2, SUCCESS, false);
test_realloc(our_malloc(size + 1), size2, SUCCESS, false);
}
}

for (snmalloc::sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
for (sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
{
const size_t size = 1ULL << sc;
test_realloc(our_malloc(size), size, SUCCESS, false);
test_realloc(our_malloc(size), 0, SUCCESS, true);
test_realloc(nullptr, size, SUCCESS, false);
test_realloc(our_malloc(size), (size_t)-1, ENOMEM, true);
for (snmalloc::sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++)
for (sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++)
{
const size_t size2 = 1ULL << sc2;
printf("size1: %zu, size2:%zu\n", size, size2);
Expand All @@ -156,7 +173,7 @@ int main(int argc, char** argv)
for (size_t align = sizeof(uintptr_t); align <= SUPERSLAB_SIZE * 8;
align <<= 1)
{
for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
{
const size_t size = sizeclass_to_size(sc);
test_posix_memalign(size, align, SUCCESS, false);
Expand Down

0 comments on commit 61afa77

Please sign in to comment.