forked from thi-ng/tinyalloc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.c
More file actions
236 lines (195 loc) · 5.85 KB
/
test.c
File metadata and controls
236 lines (195 loc) · 5.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* tinyalloc */
#include "tinyalloc.h"
static char heap[256 * 1024 * 1024];
static const ta_cfg_t cfg = {
.base = heap,
.limit = &heap[sizeof(heap)],
.max_blocks = 256 * 1024,
.split_thresh = 16,
.alignment = 16,
};
#define malloc(size) ta_alloc(&cfg, size)
#define calloc(n, size) ta_calloc(&cfg, n, size)
#define free(p) ta_free(&cfg, p)
#define realloc(p, size) ta_realloc(&cfg, p, size)
#define MAX_TEST_BLOCKS (100000)
#define NUM_RANDOM_ALLOCS (100000)
#define REQUIRED_ALIGN (16)
typedef struct {
uint8_t *mem;
size_t size;
uint8_t fill;
} TestBlock;
static TestBlock test_blocks[MAX_TEST_BLOCKS];
static size_t num_blocks;
static size_t total_bytes;
static size_t num_success;
static size_t num_failed;
static size_t smallest_fail = SIZE_MAX;
static bool each_equal(uint8_t *mem, uint8_t val, size_t size) {
for (size_t i = 0; i < size; i++) {
if (mem[i] != val) {
return false;
}
}
return true;
}
static size_t rand_bias_low(void) {
int bits = rand() % 32;
size_t mask = ((size_t)1 << bits) * 2 - 1;
return (size_t)rand() & mask;
}
static bool test_malloc(size_t idx, size_t size, uint8_t fill) {
assert(test_blocks[idx].mem == NULL);
uint8_t *mem = malloc(size);
if (mem == NULL) {
size_t fail_size = total_bytes + size;
if (fail_size > total_bytes /* check overflow */ &&
fail_size < smallest_fail) {
smallest_fail = fail_size;
}
num_failed++;
return false;
}
assert((uintptr_t)mem % REQUIRED_ALIGN == 0);
memset(mem, fill, size);
test_blocks[idx].mem = mem;
test_blocks[idx].size = size;
test_blocks[idx].fill = fill;
num_blocks++;
total_bytes += size;
num_success++;
return true;
}
static bool test_calloc(size_t idx, size_t num, size_t size, uint8_t fill) {
assert(test_blocks[idx].mem == NULL);
size_t num_x_size = num * size;
uint8_t *mem = calloc(num, size);
if (mem == NULL) {
size_t fail_size = total_bytes + num_x_size;
if ((size == 0 || num_x_size / size == num) /* check overflow */ &&
fail_size > total_bytes /* check overflow */ &&
fail_size < smallest_fail) {
smallest_fail = fail_size;
}
num_failed++;
return false;
}
assert((uintptr_t)mem % REQUIRED_ALIGN == 0);
assert(each_equal(mem, 0, num_x_size));
memset(mem, fill, num_x_size);
test_blocks[idx].mem = mem;
test_blocks[idx].size = num_x_size;
test_blocks[idx].fill = fill;
num_blocks++;
total_bytes += num_x_size;
num_success++;
return true;
}
static bool test_realloc(size_t idx, size_t size, uint8_t fill) {
uint8_t *oldmem = test_blocks[idx].mem;
size_t oldsize = (oldmem == NULL) ? 0 : test_blocks[idx].size;
uint8_t oldfill = test_blocks[idx].fill;
if (oldmem != NULL) {
assert(each_equal(oldmem, oldfill, oldsize));
}
uint8_t *mem = realloc(oldmem, size);
if (oldmem != NULL && size == 0) {
assert(mem == NULL);
test_blocks[idx].mem = NULL;
num_blocks--;
total_bytes -= oldsize;
return true;
}
if (mem == NULL) {
size_t fail_size = total_bytes - oldsize + size;
if (fail_size > total_bytes - oldsize /* check overflow */ &&
fail_size < smallest_fail) {
smallest_fail = fail_size;
}
num_failed++;
return false;
}
assert((uintptr_t)mem % REQUIRED_ALIGN == 0);
size_t minsize = (size < oldsize) ? size : oldsize;
assert(each_equal(mem, oldfill, minsize));
memset(mem, fill, size);
test_blocks[idx].mem = mem;
test_blocks[idx].size = size;
test_blocks[idx].fill = fill;
if (oldmem == NULL) {
num_blocks++;
}
total_bytes = total_bytes - oldsize + size;
num_success++;
return true;
}
static void test_free(size_t idx) {
uint8_t *mem = test_blocks[idx].mem;
size_t size = (mem == NULL) ? 0 : test_blocks[idx].size;
assert(each_equal(mem, test_blocks[idx].fill, size));
free(mem);
test_blocks[idx].mem = NULL;
if (mem != NULL) {
num_blocks--;
}
total_bytes -= size;
}
static void print_stats(void) {
printf(
"%zu blocks, %zu total bytes, %zu success, %zu failed, "
"smallest_fail = %zu\033[0K\r",
num_blocks, total_bytes, num_success, num_failed, smallest_fail);
fflush(stdout);
}
int main(void) {
srand(0x77777777);
/* tinyalloc */
ta_init(&cfg);
assert(ta_check(&cfg));
/* test 1-byte allocations */
for (size_t idx = 0; idx < MAX_TEST_BLOCKS; idx++) {
if (!test_malloc(idx, 1, rand())) {
break;
}
print_stats();
}
assert(ta_check(&cfg));
/* random free/malloc */
for (size_t n = 0; n < NUM_RANDOM_ALLOCS; n++) {
size_t idx = (size_t)rand() % MAX_TEST_BLOCKS;
test_free(idx);
test_malloc(idx, rand_bias_low(), rand());
print_stats();
}
assert(ta_check(&cfg));
/* random free/calloc */
for (size_t n = 0; n < NUM_RANDOM_ALLOCS; n++) {
size_t idx = (size_t)rand() % MAX_TEST_BLOCKS;
test_free(idx);
test_calloc(idx, rand_bias_low(), rand_bias_low(), rand());
print_stats();
}
assert(ta_check(&cfg));
/* random realloc */
for (size_t n = 0; n < NUM_RANDOM_ALLOCS; n++) {
size_t idx = (size_t)rand() % MAX_TEST_BLOCKS;
test_realloc(idx, rand_bias_low(), rand());
print_stats();
}
assert(ta_check(&cfg));
/* free remaining */
for (size_t idx = 0; idx < MAX_TEST_BLOCKS; idx++) {
test_free(idx);
print_stats();
}
assert(ta_check(&cfg));
printf("\n");
return 0;
}