diff options
| author | Albert Cervin <albert@acervin.com> | 2023-01-27 09:57:23 +0100 |
|---|---|---|
| committer | Albert Cervin <albert@acervin.com> | 2023-01-27 09:57:23 +0100 |
| commit | 94278ec39b08b4085fa920f870261eb7639baa6b (patch) | |
| tree | 82a0e896bba539b2a1f640c0695cd9cb77261928 /src/allocator.c | |
| parent | e65158a0326108d1fc724ee683b7fa900ef2671a (diff) | |
| download | dged-94278ec39b08b4085fa920f870261eb7639baa6b.tar.gz dged-94278ec39b08b4085fa920f870261eb7639baa6b.tar.xz dged-94278ec39b08b4085fa920f870261eb7639baa6b.zip | |
Split out the frame allocator
Used to be a part of main
Diffstat (limited to 'src/allocator.c')
| -rw-r--r-- | src/allocator.c | 24 |
1 files changed, 24 insertions, 0 deletions
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; } |
