Skip to content

Commit 61afa77

Browse files
Theodusmjp41
authored andcommitted
Fix sizeclass rounding error
1 parent 333190a commit 61afa77

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

src/mem/sizeclass.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -188,10 +188,14 @@ namespace snmalloc
188188

189189
SNMALLOC_FAST_PATH static size_t round_size(size_t size)
190190
{
191-
if (size > size_to_sizeclass(NUM_SIZECLASSES - 1))
191+
if (size > sizeclass_to_size(NUM_SIZECLASSES - 1))
192192
{
193193
return bits::next_pow2(size);
194194
}
195+
if (size == 0)
196+
{
197+
size = 1;
198+
}
195199
return sizeclass_to_size(size_to_sizeclass(size));
196200
}
197201
} // namespace snmalloc

src/test/func/malloc/malloc.cc

+37-20
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,39 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
1515
{
1616
if (p != nullptr)
1717
abort();
18-
}
19-
else
20-
{
21-
auto asize = our_malloc_usable_size(p);
22-
if (asize < size)
23-
{
24-
printf(
25-
"Usable size is %zu, but required to be at least %zu.\n", asize, size);
26-
abort();
27-
}
28-
29-
if (static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0)
30-
abort();
3118

3219
our_free(p);
20+
return;
21+
}
22+
23+
const auto alloc_size = our_malloc_usable_size(p);
24+
const auto expected_size = round_size(size);
25+
if ((align == 1) && (alloc_size != expected_size))
26+
{
27+
printf(
28+
"Usable size is %zu, but required to be %zu.\n",
29+
alloc_size,
30+
expected_size);
31+
abort();
32+
}
33+
if ((align != 1) && (alloc_size < expected_size))
34+
{
35+
printf(
36+
"Usable size is %zu, but required to be at least %zu.\n",
37+
alloc_size,
38+
expected_size);
39+
abort();
3340
}
41+
if (static_cast<size_t>(reinterpret_cast<uintptr_t>(p) % align) != 0)
42+
{
43+
printf(
44+
"Address is 0x%zx, but required to be aligned to 0x%zx.\n",
45+
reinterpret_cast<uintptr_t>(p),
46+
align);
47+
abort();
48+
}
49+
50+
our_free(p);
3451
}
3552

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

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

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

103120
test_calloc(0, 0, SUCCESS, false);
104121

105-
for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
122+
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
106123
{
107124
const size_t size = sizeclass_to_size(sc);
108125

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

121-
for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
138+
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
122139
{
123140
const size_t size = sizeclass_to_size(sc);
124141
test_realloc(our_malloc(size), size, SUCCESS, false);
125142
test_realloc(our_malloc(size), 0, SUCCESS, true);
126143
test_realloc(nullptr, size, SUCCESS, false);
127144
test_realloc(our_malloc(size), (size_t)-1, ENOMEM, true);
128-
for (snmalloc::sizeclass_t sc2 = 0; sc2 < NUM_SIZECLASSES; sc2++)
145+
for (sizeclass_t sc2 = 0; sc2 < NUM_SIZECLASSES; sc2++)
129146
{
130147
const size_t size2 = sizeclass_to_size(sc2);
131148
test_realloc(our_malloc(size), size2, SUCCESS, false);
132149
test_realloc(our_malloc(size + 1), size2, SUCCESS, false);
133150
}
134151
}
135152

136-
for (snmalloc::sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
153+
for (sizeclass_t sc = 0; sc < (SUPERSLAB_BITS + 4); sc++)
137154
{
138155
const size_t size = 1ULL << sc;
139156
test_realloc(our_malloc(size), size, SUCCESS, false);
140157
test_realloc(our_malloc(size), 0, SUCCESS, true);
141158
test_realloc(nullptr, size, SUCCESS, false);
142159
test_realloc(our_malloc(size), (size_t)-1, ENOMEM, true);
143-
for (snmalloc::sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++)
160+
for (sizeclass_t sc2 = 0; sc2 < (SUPERSLAB_BITS + 4); sc2++)
144161
{
145162
const size_t size2 = 1ULL << sc2;
146163
printf("size1: %zu, size2:%zu\n", size, size2);
@@ -156,7 +173,7 @@ int main(int argc, char** argv)
156173
for (size_t align = sizeof(uintptr_t); align <= SUPERSLAB_SIZE * 8;
157174
align <<= 1)
158175
{
159-
for (snmalloc::sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
176+
for (sizeclass_t sc = 0; sc < NUM_SIZECLASSES; sc++)
160177
{
161178
const size_t size = sizeclass_to_size(sc);
162179
test_posix_memalign(size, align, SUCCESS, false);

0 commit comments

Comments
 (0)