-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
372 lines (278 loc) · 8.29 KB
/
main.c
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <assert.h>
#include "allocators/scratch.h"
#include "maps/fixed_set.h"
#include "maps/fixed_map.h"
#include "lists/simple_linked_list.h"
#include "allocators/arena.h"
#include "maps/growing_map.h"
#include "lists/simple_dynarray.h"
#include "lists/dynarray.h"
#include "maps/intern.h"
#include "lists/ring_buffer.h"
#include "lists/stack.h"
#include "allocators/pool.h"
void test_scratch(ScratchAlloc *scr) {
printf("=== SCRATCH TEST ===\n");
int *array = scratch_alloc(scr, sizeof(int) * 10);
printf("allocating a little space, new head is %llu\n", scr->current);
printf("filling our new array full of data!\n");
for (int i = 0; i < 10; i++) {
array[0] = i;
}
printf("Resetting our scratch\n");
scratch_clear(scr);
printf("\n");
}
void test_fixed_set(ScratchAlloc *scr) {
printf("=== FIXED SET TEST ===\n");
FixedSet s = fixed_set_init(scr, 64);
fixed_set_insert(&s, "Hello");
fixed_set_insert(&s, "Goodbye");
if (fixed_set_contains(&s, "Hello")) {
printf("Looked for 'Hello' and got it\n");
}
if (fixed_set_contains(&s, "Goodbye")) {
printf("Looked for 'Goodbye' and got it\n");
}
bool ok = fixed_set_contains(&s, "Hamburger");
if (!ok) {
printf("Looked for 'Hamburger' couldn't find it\n");
}
printf("Clearing out all our set entries\n");
fixed_set_clear(&s);
if (!fixed_set_contains(&s, "Goodbye")) {
printf("Looked for 'Goodbye' couldn't find it\n");
}
printf("Cleaning up after ourselves\n");
scratch_clear(scr);
printf("\n");
}
void test_fixed_map(ScratchAlloc *scr) {
printf("=== FIXED MAP TEST ===\n");
FixedMap m = fixed_map_init(scr, 64);
fixed_map_insert(&m, 'b', 1000);
fixed_map_insert(&m, '~', 5);
uint64_t val = 0;
bool ok = fixed_map_get(&m, 'b', &val);
printf("Looked for 'b' and got %llu\n", val);
ok = fixed_map_get(&m, '~', &val);
printf("Looked for '~' and got %llu\n", val);
ok = fixed_map_get(&m, 9, &val);
if (!ok) {
printf("Looked for 9 and couldn't find it!\n");
} else {
printf("Looked for 9 and got %llu\n", val);
}
printf("Clearing out all our map entries\n");
fixed_map_clear(&m);
ok = fixed_map_get(&m, '~', &val);
if (!ok) {
printf("Looked for '~' and couldn't find it!\n");
} else {
printf("Looked for '~' and got %llu\n", val);
}
printf("Cleaning up after ourselves\n");
scratch_clear(scr);
printf("\n");
}
void test_simple_linked_list(ScratchAlloc *scr) {
printf("=== SIMPLE LINKED LIST TEST ===\n");
printf("creating 0\n");
Node *list_head = simple_linked_list_init(scr, 0);
for (int i = 1; i < 4; i++) {
printf("appending %d\n", i);
simple_linked_list_append(scr, &list_head, i);
}
simple_linked_list_print_list(list_head);
printf("Cleaning up after ourselves\n");
scratch_clear(scr);
printf("\n");
}
void test_arena(Arena *a) {
printf("=== ARENA V1 TEST ===\n");
printf("current arena size: 0x%08llx\n", a->total_size);
printf("testing a small allocation\n");
char *buffer = arena_alloc(a, 50);
snprintf(buffer, 50, "Hello Arena Test!\n");
printf("> %s\n", buffer);
printf("current arena size: 0x%08llx\n", a->total_size);
printf("testing a big allocation\n");
int *array = arena_alloc(a, sizeof(int) * 10000);
array[9999] = 36;
array[9998] = 72;
printf("current arena size: 0x%08llx\n", a->total_size);
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("current arena size: 0x%08llx\n", a->total_size);
printf("\n");
}
void test_simple_dynarray(Arena *a) {
printf("=== SIMPLE DYNAMIC ARRAY TEST ===\n");
SimpleDynArray arr = simple_dyn_init(a, 32);
printf("Trying to append data\n");
simple_dyn_append(&arr, 99);
simple_dyn_append(&arr, 200);
simple_dyn_append(&arr, 1);
simple_dyn_append(&arr, 0);
printf("Checking that data is in there\n");
for (int i = 0; i < arr.size; i++) {
printf("arr[%d] = %llu\n", i, simple_dyn_get(&arr, i));
}
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
void test_dynarray(Arena *a) {
printf("=== DYNAMIC ARRAY TEST ===\n");
DynArray(int) arr;
dyn_init(a, &arr, 32);
printf("Trying to append data\n");
dyn_append(&arr, 99);
dyn_append(&arr, 200);
dyn_append(&arr, 1);
dyn_append(&arr, 0);
printf("Checking that data is in there\n");
for (int i = 0; i < arr.size; i++) {
printf("arr[%d] = %d\n", i, dyn_get(&arr, i));
}
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
void test_growing_map(Arena *a) {
printf("=== GROWING MAP TEST ===\n");
Map m = map_init(a, 0);
for (int i = 0; i < 20; i += 1) {
printf("inserting map['%c'] = %d\n", 'a'+i, i);
map_insert(&m, 'a'+i, i);
}
uint64_t val = 0;
for (int i = 0; i < 20; i += 1) {
uint64_t key = 'a'+i;
bool ok = map_get(&m, key, &val);
assert(ok);
printf("Looked for '%c' and got %llu\n", (char)key, val);
}
bool ok = map_get(&m, 9, &val);
assert(!ok);
printf("Clearing out all our map entries\n");
map_clear(&m);
ok = map_get(&m, 'a', &val);
assert(!ok);
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
void test_intern(Arena *a, Arena *string_block) {
printf("=== STRING INTERNING TEST ===\n");
Intern in = intern_init(a, string_block, 0);
for (int i = 0; i < 10; i++) {
char buf[2] = {};
buf[0] = 'a' + i;
intern_get(&in, buf);
}
char *test_string = "Apple";
char *interned_string = intern_get(&in, test_string);
printf("orig string: %s, new string: %s || orig ptr: %p, new ptr: %p\n",
test_string, interned_string, test_string, interned_string);
printf("Cleaning up after ourselves\n");
arena_clear(a);
arena_clear(string_block);
printf("\n");
}
void test_ring_buffer(Arena *a) {
printf("=== RING BUFFER TEST ===\n");
RingBuffer ring = ring_init(a, 8);
for (int64_t i = 1; i < 10; i++) {
printf("Pushing %lld\n", i);
ring_push(&ring, (void *)i);
}
for (int64_t i = 1; i < 10; i++) {
int64_t v = (int64_t)ring_pop(&ring);
printf("Popped %lld\n", v);
}
printf("Arena bytes used before: %lld\n", a->total_size);
for (int64_t i = 0; i < 10; i++) {
ring_push(&ring, (void *)i);
ring_pop(&ring);
}
printf("Arena bytes used after: %lld\n", a->total_size);
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
void test_stack(Arena *a) {
printf("=== STACK TEST ===\n");
Stack stack = stack_init(a, 8);
for (int64_t i = 1; i < 10; i++) {
printf("Pushing %lld\n", i);
stack_push(&stack, (void *)i);
}
for (int64_t i = 1; i < 10; i++) {
int64_t v = (int64_t)stack_pop(&stack);
printf("Popped %lld\n", v);
}
printf("Arena bytes used before: %lld\n", a->total_size);
for (int64_t i = 0; i < 10; i++) {
stack_push(&stack, (void *)i);
stack_pop(&stack);
}
printf("Arena bytes used after: %lld\n", a->total_size);
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
void test_pool(Arena *a) {
printf("=== POOL TEST ===\n");
Pool *p = pool_init(a, 4096, sizeof(uint64_t));
uint64_t *slot_1 = pool_get(p);
printf("Got a slot 1 -- %p\n", slot_1);
uint64_t *slot_2 = pool_get(p);
printf("Got a slot 2 -- %p\n", slot_2);
uint64_t *slot_3 = pool_get(p);
printf("Got a slot 3 -- %p\n", slot_3);
pool_free(p, slot_1);
printf("Freed slot 1 -- %p\n", slot_1);
uint64_t *slot_4 = pool_get(p);
printf("Got a slot 4 -- %p\n", slot_4);
pool_free(p, slot_2);
printf("Freed slot 2 -- %p\n", slot_2);
uint64_t *slot_5 = pool_get(p);
printf("Got a slot 5 -- %p\n", slot_5);
pool_free(p, slot_3);
printf("Freed slot 3 -- %p\n", slot_3);
pool_free(p, slot_4);
printf("Freed slot 4 -- %p\n", slot_4);
pool_free(p, slot_5);
printf("Freed slot 5 -- %p\n", slot_5);
printf("Cleaning up after ourselves\n");
arena_clear(a);
printf("\n");
}
int main(int argc, char **argv) {
ScratchAlloc scr = scratch_init(8 * 1024);
printf("~~ Hello Memory, it's me the ScratchAllocator! ~~\n\n");
test_scratch(&scr);
test_fixed_set(&scr);
test_fixed_map(&scr);
test_simple_linked_list(&scr);
printf("~~ Ugh, that's a lot of work... Time for retirement! ~~ \n\n");
scratch_free(&scr);
Arena *a = arena_init();
printf("~~ WELCOME TO THE THUNDERDOME! ~~\n\n");
test_arena(a);
test_simple_dynarray(a);
test_dynarray(a);
test_growing_map(a);
printf("~~ Now We Have Two Arenas ~~\n\n");
Arena *string_block = arena_init();
test_intern(a, string_block);
arena_free(string_block);
printf("~~ Back To Just One ~~\n\n");
test_ring_buffer(a);
test_stack(a);
test_pool(a);
}