diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-01-31 08:02:09 +0100 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-01-31 08:02:09 +0100 |
| commit | 8a9e6b1bd3be1041792311a86d8aac68b659e891 (patch) | |
| tree | 2a62729da17b2e6669c3d71a162525ff37e42c6a /memory.c | |
feat: First version, failing json parse string
Diffstat (limited to 'memory.c')
| -rw-r--r-- | memory.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/memory.c b/memory.c new file mode 100644 index 0000000..cfb5dd4 --- /dev/null +++ b/memory.c @@ -0,0 +1,27 @@ +#include <assert.h> +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "memory.h" + +Arena Arena_new(ptrdiff_t cap) { + Arena a = {0}; + a.cap = cap; + a.beg = malloc(cap); + a.end = a.beg ? a.beg + cap : 0; + return a; +} + +// Magic to allow optimisation __attribute((malloc, alloc_size(2, 4), +// alloc_align(3))) +void *alloc(Arena *a, ptrdiff_t count, int size, int align) { + int padding = -(uintptr_t)a->beg & (align - 1); + assert(count < (a->end - a->beg - padding) / size); + void *cursor = a->beg + padding; + a->beg += padding + count * size; + return memset(cursor, 0, count * size); +} + +int Arena_get_used(Arena a) { return a.cap - (a.end - a.beg); } |
