diff options
| author | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
|---|---|---|
| committer | kartofen <kartofen.mail.0@protonmail.com> | 2025-07-20 01:32:24 +0300 |
| commit | 34357640c0676f33ad13aac1fe28effc6f6e47c7 (patch) | |
| tree | d656ee61da7d7a0b133aa57311266653ef100569 /util/arena.h | |
| parent | 174e9b35ce3b6e99e500907f1bb24c6f31f481bf (diff) | |
start of grammar parsing
Diffstat (limited to 'util/arena.h')
| -rw-r--r-- | util/arena.h | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/util/arena.h b/util/arena.h new file mode 100644 index 0000000..3d82b95 --- /dev/null +++ b/util/arena.h @@ -0,0 +1,30 @@ +#ifndef ARENA_H +#define ARENA_H + +#include <stddef.h> // size_t + +struct arena_ctx { + void *buffer; + size_t size; + size_t offset; +}; + +#define ARENA_CTX_INIT(buffer, sz) (struct arena_ctx){(buffer), (sz), 0} +void *arena_allocate(struct arena_ctx *ctx, size_t sz); +void arena_reset(struct arena_ctx *ctx); + +#ifdef ARENA_IMPLEMENTATION + +void *arena_allocate(struct arena_ctx *ctx, size_t sz) +{ + if(ctx->offset + sz > ctx->size) return NULL; + + void *off = ctx->buffer + ctx->offset; + ctx->offset += sz; + return off; +} + +void arena_reset(struct arena_ctx *ctx) { ctx->offset = 0; } + +#endif +#endif |
