summaryrefslogtreecommitdiff
path: root/include/obe/arena.h
diff options
context:
space:
mode:
authorJohnny Richard <johnny@johnnyrichard.com>2024-10-30 22:58:03 +0100
committerJohnny Richard <johnny@johnnyrichard.com>2025-12-14 09:53:52 +0100
commit10bb8a05088f1d3bb24f7167f609b5f6fb0ba026 (patch)
tree7a4b3f69a461301c45204ed856b61f92a7d42233 /include/obe/arena.h
bootstrap projectHEADmaster
Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
Diffstat (limited to 'include/obe/arena.h')
-rw-r--r--include/obe/arena.h77
1 files changed, 77 insertions, 0 deletions
diff --git a/include/obe/arena.h b/include/obe/arena.h
new file mode 100644
index 0000000..5b3490a
--- /dev/null
+++ b/include/obe/arena.h
@@ -0,0 +1,77 @@
+/*
+ * Copyright (C) 2025 Johnny Richard <johnny@johnnyrichard.com>
+ *
+ * SPDX-License-Identifier: LGPL-3.0-or-later
+ *
+ * This file is part of obe.
+ *
+ * obe is free software: you can redistribute it and/or modify it under the
+ * terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation, either version 3 of the License, or (at your option)
+ * any later version.
+ *
+ * obe is distributed in the hope that it will be useful, but WITHOUT ANY
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with obe. If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef OBE_ARENA_H
+#define OBE_ARENA_H
+
+#include <stddef.h>
+#include <stdint.h>
+
+#define OBE_ARENA_ALIGMENT_BYTES (16)
+#define OBE_ARENA_ALIGMENT_MASK (OBE_ARENA_ALIGMENT_BYTES - 1)
+#define OBE_ARENA_REGION_DEFAULT_CAPACITY (8 * 1024)
+#define OBE_ARENA_PADDING(bytes) \
+ ((uint8_t)((OBE_ARENA_ALIGMENT_BYTES - bytes) & OBE_ARENA_ALIGMENT_MASK))
+
+typedef struct obe_arena_region obe_arena_region_t;
+
+struct obe_arena_region
+{
+ obe_arena_region_t* next;
+ size_t offset;
+ size_t capacity;
+ uint8_t* data;
+};
+
+typedef struct obe_arena
+{
+ obe_arena_region_t* begin;
+ obe_arena_region_t* end;
+} obe_arena_t;
+
+/* arena */
+
+void*
+obe_arena_alloc(obe_arena_t* arena, size_t size);
+
+void*
+obe_arena_realloc(obe_arena_t* arena,
+ void* old_ptr,
+ size_t old_size,
+ size_t new_size);
+
+void
+obe_arena_release(obe_arena_t* arena);
+
+void
+obe_arena_free(obe_arena_t* arena);
+
+/* region */
+
+obe_arena_region_t*
+obe_arena_region_new(size_t capacity);
+
+void
+obe_arena_region_free(obe_arena_region_t* r);
+
+char*
+obe_arena_strdup(obe_arena_t* arena, char* s);
+
+#endif /* OBE_ARENA_H */