aboutsummaryrefslogtreecommitdiff
path: root/util/arena.h
diff options
context:
space:
mode:
Diffstat (limited to 'util/arena.h')
-rw-r--r--util/arena.h30
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