vile / scheme / testdata / unicode_scratch / allocations.h
49 lines · 47 sloc · 1.35 KB · 76646891b77a3d5c6af4a1e42b122cd61dc1654b
Raw
1#include <stdlib.h>
2
3static void *unicode_scratch_blocks[16];
4static int unicode_scratch_count;
5static int unicode_scratch_tracking;
6
7static void unicode_scratch_record(void *p) {
8 if (p && unicode_scratch_tracking) {
9 if (unicode_scratch_count == 16) abort();
10 unicode_scratch_blocks[unicode_scratch_count++] = p;
11 }
12}
13static void unicode_scratch_forget(void *p) {
14 if (!unicode_scratch_tracking || !p) return;
15 for (int i = 0; i < unicode_scratch_count; i++) {
16 if (unicode_scratch_blocks[i] == p) {
17 unicode_scratch_blocks[i] = unicode_scratch_blocks[--unicode_scratch_count];
18 return;
19 }
20 }
21}
22static void unicode_scratch_begin(void) {
23 unicode_scratch_count = 0;
24 unicode_scratch_tracking = 1;
25}
26static int unicode_scratch_end(void) {
27 unicode_scratch_tracking = 0;
28 return unicode_scratch_count;
29}
30static void *unicode_scratch_first(void) {
31 return unicode_scratch_count ? unicode_scratch_blocks[0] : NULL;
32}
33static void *unicode_scratch_malloc(size_t n) {
34 void *p = malloc(n);
35 unicode_scratch_record(p);
36 return p;
37}
38static void *unicode_scratch_calloc(size_t n, size_t size) {
39 void *p = calloc(n, size);
40 unicode_scratch_record(p);
41 return p;
42}
43static void unicode_scratch_free(void *p) {
44 unicode_scratch_forget(p);
45 free(p);
46}
47#define malloc(n) unicode_scratch_malloc(n)
48#define calloc(n, size) unicode_scratch_calloc(n, size)
49#define free(p) unicode_scratch_free(p)
50