@@ -15,22 +15,39 @@ void check_result(size_t size, size_t align, void* p, int err, bool null)
15
15
{
16
16
if (p != nullptr )
17
17
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 ();
31
18
32
19
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 ();
33
40
}
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);
34
51
}
35
52
36
53
void test_calloc (size_t nmemb, size_t size, int err, bool null)
@@ -92,7 +109,7 @@ int main(int argc, char** argv)
92
109
93
110
test_realloc (our_malloc (64 ), 4194304 , SUCCESS, false );
94
111
95
- for (snmalloc:: sizeclass_t sc = 0 ; sc < (SUPERSLAB_BITS + 4 ); sc++)
112
+ for (sizeclass_t sc = 0 ; sc < (SUPERSLAB_BITS + 4 ); sc++)
96
113
{
97
114
const size_t size = 1ULL << sc;
98
115
printf (" malloc: %zu\n " , size);
@@ -102,7 +119,7 @@ int main(int argc, char** argv)
102
119
103
120
test_calloc (0 , 0 , SUCCESS, false );
104
121
105
- for (snmalloc:: sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
122
+ for (sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
106
123
{
107
124
const size_t size = sizeclass_to_size (sc);
108
125
@@ -118,29 +135,29 @@ int main(int argc, char** argv)
118
135
test_calloc (0 , size, SUCCESS, false );
119
136
}
120
137
121
- for (snmalloc:: sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
138
+ for (sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
122
139
{
123
140
const size_t size = sizeclass_to_size (sc);
124
141
test_realloc (our_malloc (size), size, SUCCESS, false );
125
142
test_realloc (our_malloc (size), 0 , SUCCESS, true );
126
143
test_realloc (nullptr , size, SUCCESS, false );
127
144
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++)
129
146
{
130
147
const size_t size2 = sizeclass_to_size (sc2);
131
148
test_realloc (our_malloc (size), size2, SUCCESS, false );
132
149
test_realloc (our_malloc (size + 1 ), size2, SUCCESS, false );
133
150
}
134
151
}
135
152
136
- for (snmalloc:: sizeclass_t sc = 0 ; sc < (SUPERSLAB_BITS + 4 ); sc++)
153
+ for (sizeclass_t sc = 0 ; sc < (SUPERSLAB_BITS + 4 ); sc++)
137
154
{
138
155
const size_t size = 1ULL << sc;
139
156
test_realloc (our_malloc (size), size, SUCCESS, false );
140
157
test_realloc (our_malloc (size), 0 , SUCCESS, true );
141
158
test_realloc (nullptr , size, SUCCESS, false );
142
159
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++)
144
161
{
145
162
const size_t size2 = 1ULL << sc2;
146
163
printf (" size1: %zu, size2:%zu\n " , size, size2);
@@ -156,7 +173,7 @@ int main(int argc, char** argv)
156
173
for (size_t align = sizeof (uintptr_t ); align <= SUPERSLAB_SIZE * 8 ;
157
174
align <<= 1 )
158
175
{
159
- for (snmalloc:: sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
176
+ for (sizeclass_t sc = 0 ; sc < NUM_SIZECLASSES; sc++)
160
177
{
161
178
const size_t size = sizeclass_to_size (sc);
162
179
test_posix_memalign (size, align, SUCCESS, false );
0 commit comments