1
0
Fork 0

Added: growable arenas on allocation

Browse Source
This commit is contained in:
rhuibertsjr 2024-04-26 17:00:32 +02:00
parent 66083b3a95
commit 2f246bdf59
4 changed files with 48 additions and 2 deletions

View File

@ -10,10 +10,11 @@ arena_initialize(u64 size, b8 growable)
result = (Arena*) backing_buffer; result = (Arena*) backing_buffer;
result->backing_buffer = backing_buffer; result->backing_buffer = backing_buffer;
result->current = result;
// rhjr: immutable arena header // rhjr: immutable arena header
result->base_pos = sizeof(Arena); result->base_pos = sizeof(Arena);
result->commit_pos = sizeof(Arena); result->offset = sizeof(Arena);
result->growable = growable; result->growable = growable;
result->size = size; result->size = size;
@ -35,6 +36,43 @@ arena_initialize_default()
internal void * internal void *
arena_allocate(Arena *arena, u64 size) arena_allocate(Arena *arena, u64 size)
{ {
Arena *current = arena->current;
u64 pos_mem = current->offset;
u64 pos_new = current->offset + size;
if (current->size < pos_new && current->growable)
{
Arena *new_memory_bock;
if (size > ARENA_DEFAULT_RESERVE_SIZE)
{
// rhjr: TODO add support for allocations larger then a single page, in a
// single allocation call.
}
else
{
new_memory_bock = arena_initialize_default();
if (new_memory_bock)
{
sll_stack_push(arena->current, new_memory_bock, prev);
current = new_memory_bock;
pos_mem = current->offset;
pos_new = current->offset + size;
}
}
}
void *memory = (void*)((u64) current->backing_buffer + pos_mem);
arena->offset = pos_new;
platform_memory_commit(memory, size);
memset(memory, 0, size);
return memory;
} }
internal void internal void

View File

@ -13,7 +13,7 @@ struct Arena {
struct Arena *prev; struct Arena *prev;
u64 *backing_buffer; u64 *backing_buffer;
u64 base_pos; u64 base_pos;
u64 commit_pos; u64 offset;
u64 size; u64 size;
b8 growable; b8 growable;
}; };

View File

@ -33,6 +33,10 @@ typedef uint8_t b64;
#define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } ) #define ASSERT(c) STATEMENT( if (!(c)){ (*(volatile int*)0 = 0); } )
#define STATIC_ASSERT(c,l) typedef u8 Glue(l,__LINE__) [(c)?1:-1] #define STATIC_ASSERT(c,l) typedef u8 Glue(l,__LINE__) [(c)?1:-1]
//= rhjr: linkedlist helpers
#define sll_stack_push(f,n,next) ((n)->next=(f), (f)=(n))
//= rhjr: memory helpers //= rhjr: memory helpers
#define memory_zero(s,z) memset((s), 0, (z)) #define memory_zero(s,z) memset((s), 0, (z))

View File

@ -9,5 +9,9 @@ int
main(void) main(void)
{ {
Arena *arena = arena_initialize_default(); Arena *arena = arena_initialize_default();
u64* ptr1 = arena_allocate(arena, KB(3));
u64* ptr2 = arena_allocate(arena, KB(2));
arena_release(arena);
return 0; return 0;
} }