| 1 | #include <stdlib.h> |
| 2 | |
| 3 | static void *unicode_scratch_blocks[16]; |
| 4 | static int unicode_scratch_count; |
| 5 | static int unicode_scratch_tracking; |
| 6 | |
| 7 | static 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 | } |
| 13 | static 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 | } |
| 22 | static void unicode_scratch_begin(void) { |
| 23 | unicode_scratch_count = 0; |
| 24 | unicode_scratch_tracking = 1; |
| 25 | } |
| 26 | static int unicode_scratch_end(void) { |
| 27 | unicode_scratch_tracking = 0; |
| 28 | return unicode_scratch_count; |
| 29 | } |
| 30 | static void *unicode_scratch_first(void) { |
| 31 | return unicode_scratch_count ? unicode_scratch_blocks[0] : NULL; |
| 32 | } |
| 33 | static void *unicode_scratch_malloc(size_t n) { |
| 34 | void *p = malloc(n); |
| 35 | unicode_scratch_record(p); |
| 36 | return p; |
| 37 | } |
| 38 | static 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 | } |
| 43 | static 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 | |