summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlbert Cervin <albert@acervin.com>2023-01-27 09:57:23 +0100
committerAlbert Cervin <albert@acervin.com>2023-01-27 09:57:23 +0100
commit94278ec39b08b4085fa920f870261eb7639baa6b (patch)
tree82a0e896bba539b2a1f640c0695cd9cb77261928
parente65158a0326108d1fc724ee683b7fa900ef2671a (diff)
downloaddged-94278ec39b08b4085fa920f870261eb7639baa6b.tar.gz
dged-94278ec39b08b4085fa920f870261eb7639baa6b.tar.xz
dged-94278ec39b08b4085fa920f870261eb7639baa6b.zip
Split out the frame allocator
Used to be a part of main
-rw-r--r--common.mk2
-rw-r--r--src/allocator.c24
-rw-r--r--src/allocator.h25
-rw-r--r--src/main.c1
4 files changed, 36 insertions, 16 deletions
diff --git a/common.mk b/common.mk
index eeb7e0a..c20a1a0 100644
--- a/common.mk
+++ b/common.mk
@@ -5,7 +5,7 @@ default: dged
SOURCES = src/binding.c src/buffer.c src/command.c src/display.c \
src/keyboard.c src/minibuffer.c src/text.c \
- src/utf8.c src/buffers.c src/window.c
+ src/utf8.c src/buffers.c src/window.c src/allocator.c
DGED_SOURCES = $(SOURCES) src/main.c
TEST_SOURCES = test/assert.c test/buffer.c test/text.c test/utf8.c test/main.c \
diff --git a/src/allocator.c b/src/allocator.c
new file mode 100644
index 0000000..308b97c
--- /dev/null
+++ b/src/allocator.c
@@ -0,0 +1,24 @@
+#include "allocator.h"
+
+struct frame_allocator frame_allocator_create(size_t capacity) {
+ return (struct frame_allocator){
+ .capacity = capacity, .offset = 0, .buf = (uint8_t *)malloc(capacity)};
+}
+
+void frame_allocator_destroy(struct frame_allocator *alloc) {
+ free(alloc->buf);
+ alloc->buf = NULL;
+}
+
+void *frame_allocator_alloc(struct frame_allocator *alloc, size_t sz) {
+ if (alloc->offset + sz > alloc->capacity) {
+ return NULL;
+ }
+
+ void *mem = alloc->buf + alloc->offset;
+ alloc->offset += sz;
+
+ return mem;
+}
+
+void frame_allocator_clear(struct frame_allocator *alloc) { alloc->offset = 0; }
diff --git a/src/allocator.h b/src/allocator.h
index d89bd29..49e3aec 100644
--- a/src/allocator.h
+++ b/src/allocator.h
@@ -18,10 +18,14 @@ struct frame_allocator {
* @param capacity The capacity in bytes of the frame allocator
* @returns The frame allocator
*/
-struct frame_allocator frame_allocator_create(size_t capacity) {
- return (struct frame_allocator){
- .capacity = capacity, .offset = 0, .buf = (uint8_t *)malloc(capacity)};
-}
+struct frame_allocator frame_allocator_create(size_t capacity);
+
+/**
+ * Destroy a frame allocator.
+ *
+ * @param alloc The @ref frame_allocator "frame allocator" to destroy.
+ */
+void frame_allocator_destroy(struct frame_allocator *alloc);
/**
* Allocate memory in this @ref frame_allocator "frame allocator"
@@ -31,16 +35,7 @@ struct frame_allocator frame_allocator_create(size_t capacity) {
* @returns void* representing the start of the allocated region on success,
* NULL on failure.
*/
-void *frame_allocator_alloc(struct frame_allocator *alloc, size_t sz) {
- if (alloc->offset + sz > alloc->capacity) {
- return NULL;
- }
-
- void *mem = alloc->buf + alloc->offset;
- alloc->offset += sz;
-
- return mem;
-}
+void *frame_allocator_alloc(struct frame_allocator *alloc, size_t sz);
/**
* Clear this @ref frame_allocator "frame allocator".
@@ -48,4 +43,4 @@ void *frame_allocator_alloc(struct frame_allocator *alloc, size_t sz) {
* This does not free any memory, but simply resets the offset to 0.
* @param alloc The frame allocator to clear
*/
-void frame_allocator_clear(struct frame_allocator *alloc) { alloc->offset = 0; }
+void frame_allocator_clear(struct frame_allocator *alloc);
diff --git a/src/main.c b/src/main.c
index d67ee94..0125110 100644
--- a/src/main.c
+++ b/src/main.c
@@ -306,6 +306,7 @@ int main(int argc, char *argv[]) {
display_destroy(&display);
keymap_destroy(&global_keymap);
command_registry_destroy(&commands);
+ frame_allocator_destroy(&frame_allocator);
return 0;
}