aboutsummaryrefslogtreecommitdiff
path: root/include
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
parentbbbeab43e067a7f009b74df72ed9b083f3ecef58 (diff)
implement arena allocators
Diffstat (limited to 'include')
-rw-r--r--include/_attrs.h7
-rw-r--r--include/alloc.h31
2 files changed, 38 insertions, 0 deletions
diff --git a/include/_attrs.h b/include/_attrs.h
new file mode 100644
index 0000000..513a5c7
--- /dev/null
+++ b/include/_attrs.h
@@ -0,0 +1,7 @@
+#ifndef MLIB__ATTRS_H
+#define MLIB__ATTRS_H
+
+#define _mlib_pure __nodiscard__, __unsequenced__
+#define _mlib_inline gnu::__always_inline__, clang::__always_inline__
+
+#endif /* !MLIB__ATTRS_H */
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 */