diff --git a/code/inc/utility.h b/code/inc/utility.h index 6c0a804..bee9745 100644 --- a/code/inc/utility.h +++ b/code/inc/utility.h @@ -47,4 +47,23 @@ void pac_memory_copy (void *destination, void *source, usz_t void pac_memory_fill (void *region, usz_t len_region, u8_t byte); void pac_memory_zero (void *region, usz_t len_region); -#endif // Include Guard (TN_UTIL_TYPES_H) \ No newline at end of file + + +typedef struct pac_arena +{ + usz_t len_allocation; + usz_t offset; + void *allocation; + pac_arena_s *continuation; + +} pac_arena_s; + +pac_arena_s pac_create_arena (usz_t size); +void pac_delete_arena (pac_arena_s arena); + +void pac_free_arena (pac_arena_s *arena); +pac_arena_s * pac_new_arena (usz_t size); + +void * pac_arena_alloc (pac_arena_s *arena, usz_t length); + +#endif // Include Guard (TN_UTIL_TYPES_H) diff --git a/code/src/allocator.c b/code/src/allocator.c new file mode 100644 index 0000000..0ded8bc --- /dev/null +++ b/code/src/allocator.c @@ -0,0 +1,57 @@ +#include + +pac_arena_s pac_create_arena(usz_t size) +{ + pac_arena_s arena; + arena.len_allocation = size; + arena.offset = 0; + arena.allocation = malloc(size); + arena.continuation = NULL; + return arena; +} + +void pac_delete_arena(pac_arena_s arena) +{ + if(arena.continuation != NULL) + { + pac_free_arena(arena.continuation); + } + free(arena.allocation); +} + + + +pac_arena_s * pac_new_arena(usz_t size) +{ + pac_arena_s *arena = malloc(sizeof(pac_arena_s) + size); + arena->len_allocation = size; + arena->offset = 0; + arena->allocation = arena + sizeof(pac_arena_s); + arena->continuation = NULL; + + return arena; +} + +void pac_free_arena(pac_arena_s *arena) +{ + if(arena->continuation != NULL) + { + pac_free_arena(arena->continuation); + } + free(arena); +} + + + +void * pac_arena_alloc(pac_arena_s *arena, usz_t length) +{ + if((arena->offset + length) > arena->len_allocation) + { + if(arena->continuation == NULL) + arena->continuation = pac_new_arena(length*2); + return pac_arena_alloc(arena->continuation, length); + } + void *block = arena->allocation + arena->offset; + arena->offset += length; + return block; +}