You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
983 B
51 lines
983 B
|
|
|
|
|
|
typedef struct Memory_Arena |
|
{ |
|
void *memory; |
|
size_t size; |
|
size_t allocation_offset; |
|
} Memory_Arena; |
|
|
|
_Static_assert(sizeof(size_t) == sizeof(void*), |
|
"The Memory_Arena implementation assumes that size_t and pointers are of the same size."); |
|
|
|
void *memory_arena_allocate( |
|
Memory_Arena *arena, |
|
size_t size, |
|
size_t alignment) |
|
{ |
|
#if 0 |
|
printf( |
|
"%s: arena: {.memory=%p, .size=%zu, .allocation_offset=%zu}\n" |
|
"size: %zu, alignment: %zu\n" |
|
, __func__ |
|
, arena->memory, arena->size, arena->allocation_offset |
|
, size, alignment |
|
); |
|
#endif |
|
size_t allocation_offset = arena->allocation_offset; |
|
allocation_offset = ((allocation_offset + alignment - 1) / alignment) * alignment; |
|
if(allocation_offset + size > arena->size) |
|
{ |
|
return NULL; |
|
} |
|
void *allocation = ((char*)arena->memory) + allocation_offset; |
|
arena->allocation_offset = allocation_offset + size; |
|
return allocation; |
|
} |
|
|
|
void memory_arena_reset( |
|
Memory_Arena *arena) |
|
{ |
|
arena->allocation_offset = 0; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|