Implemented an arena allocator
This commit is contained in:
parent
ad415bcafe
commit
b058cbe288
|
@ -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);
|
||||
|
||||
|
||||
|
||||
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)
|
|
@ -0,0 +1,57 @@
|
|||
#include <utility.h>
|
||||
|
||||
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;
|
||||
}
|
Loading…
Reference in New Issue