feat and fix: Made arena_init use malloc and added arena_destroy
Realized that if i want to use malloc and free i either have to make some sort of interface that lets you use a custom allocator and deallocator or just use them by default. Maybe later it would be cool to change it to that but for now is out of the scope of my project. # Tipos: # feat, fix, refactor, docs, style, test, chore
This commit is contained in:
17
src/arena.c
17
src/arena.c
@@ -2,11 +2,13 @@
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
ArenaResult arena_init(void *buffer, size_t size) {
|
||||
ArenaResult arena_init(size_t size) {
|
||||
|
||||
void *buffer = malloc(size);
|
||||
if (buffer == NULL) {
|
||||
ArenaResult err = {
|
||||
.is_valid = false,
|
||||
.err = ARENA_NULL_ARG,
|
||||
.err = ARENA_BAD_ALLOC,
|
||||
};
|
||||
return err;
|
||||
}
|
||||
@@ -24,7 +26,6 @@ ArenaResult arena_init(void *buffer, size_t size) {
|
||||
.size = size,
|
||||
.offset = 0,
|
||||
};
|
||||
|
||||
ArenaResult val = {
|
||||
.is_valid = true,
|
||||
.arena = new_arena,
|
||||
@@ -32,3 +33,13 @@ ArenaResult arena_init(void *buffer, size_t size) {
|
||||
|
||||
return val;
|
||||
}
|
||||
|
||||
void arena_destroy(Arena *arena) {
|
||||
if (arena == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
arena->offset = 0;
|
||||
arena->size = 0;
|
||||
free(arena->data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user