aboutsummaryrefslogtreecommitdiff
path: root/include/alloc.h
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-04-16 02:03:12 +0200
committerThomas Voss <mail@thomasvoss.com> 2024-04-16 02:03:12 +0200
commit43b04a1bfaecfabe8b3460575db7be5d592f896b (patch)
tree86c337e90f4b4238ab73dd698f470cf3482a0de8 /include/alloc.h
parentbbbeab43e067a7f009b74df72ed9b083f3ecef58 (diff)
implement arena allocators
Diffstat (limited to 'include/alloc.h')
-rw-r--r--include/alloc.h31
1 files changed, 31 insertions, 0 deletions
diff --git a/include/alloc.h b/include/alloc.h
index 6eb2a30..0855e01 100644
--- a/include/alloc.h
+++ b/include/alloc.h
@@ -3,7 +3,38 @@
#include <stddef.h>
+#include "_attrs.h"
+
+#ifndef MLIB_ARENA_BLKSIZE
+# define MLIB_ARENA_BLKSIZE (8 * 1024)
+#endif
+
+struct _region {
+ size_t len, cap;
+ void *data;
+ struct _region *next;
+};
+
+typedef struct {
+ struct _region *_head;
+ size_t _init;
+} arena;
+
[[gnu::__returns_nonnull__]] void *bufalloc(void *, size_t, size_t);
void *bufalloc_noterm(void *, size_t, size_t);
+[[_mlib_pure, _mlib_inline]]
+static inline arena
+mkarena(size_t n)
+{
+ return (arena){._init = n ? n : MLIB_ARENA_BLKSIZE};
+}
+
+[[gnu::__malloc__, gnu::__alloc_size__(2, 3), gnu::__alloc_align__(4)]]
+void *arena_alloc(arena *, size_t, size_t, size_t);
+void arena_zero(arena *);
+void arena_free(arena *);
+
+#define arena_new(a, T, n) ((T *)arena_alloc((a), sizeof(T), (n), alignof(T)))
+
#endif /* !MLIB_ALLOC_H */