Base/platform/src-c/alloc/arena.c

42 lines
932 B
C
Raw Normal View History

#include <alloc/arena.h>
rr_arena_s rr_new_arena(usz_t capacity)
{
rr_arena_s arena;
arena.capacity = capacity;
arena.offset = 0;
arena.allocation = malloc(capacity);
return arena;
}
void rr_delete_arena(rr_arena_s *arena)
{
if(arena->allocation == NULL) return;
free(arena->allocation);
arena->allocation = NULL;
}
void * rr_arena_alloc(rr_arena_s *arena, usz_t length)
{
if((arena->offset + length) > arena->capacity)
return NULL;
void *block = &arena->allocation[arena->offset];
arena->offset += length;
return block;
}
char * rr_arena_clone_string(rr_arena_s *arena, char *string)
{
usz_t len_string = rr_measure_string(string);
char *cloned_string = rr_arena_alloc(arena, len_string+1);
if(cloned_string == NULL)
return NULL;
rr_memcopy(cloned_string, string, len_string);
cloned_string[len_string] = 0x00;
return cloned_string;
}