Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

51 řádky
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;
}