1
0
Fork 0

Added: arena allocator

Browse Source
This commit is contained in:
rhuibertsjr 2024-04-13 14:15:02 +02:00
parent b9abdc8fce
commit e882e68959
2 changed files with 71 additions and 0 deletions

40
src/arena.c Normal file
View File

@ -0,0 +1,40 @@
internal Arena *
arena_initialize(u64 size)
{
Arena *result = 0;
void *backing_buffer = malloc(size);
if (backing_buffer != NULL)
{
result = (Arena*) backing_buffer;
result->buffer = backing_buffer;
result->offset = sizeof(Arena);
result->size = sizeof(Arena) + size;
}
return result;
}
internal void *
arena_allocate(Arena *arena, u64 size)
{
void *result = 0;
if (arena->offset + size <= arena->size)
{
result = (void*)((u64) arena->buffer + (u64) arena->offset);
arena->offset += size;
memset(result, 0, size);
}
return result;
}
internal void
arena_release(Arena *arena)
{
arena->offset = 0;
}

31
src/arena.h Normal file
View File

@ -0,0 +1,31 @@
#ifndef ARENA_H
#define ARENA_H
typedef struct Arena Arena;
struct Arena {
u64 *buffer;
u64 offset;
u64 size;
};
internal Arena * arena_initialize(u64 size);
internal void * arena_allocate(Arena *arena, u64 size);
internal void arena_release(Arena *arena);
//= rhjr: arena helpers
#define arena_push_array(arena, type, count) \
(type*) arena_allocate((arena), sizeof(type) * (count))
#define arena_push(arena, type) \
(type*) arena_allocate((arena), sizeof(type))
//= rhjr: memory helpers
#define memory_zero(s,z) memset((s), 0, (z))
#define memory_zero_struct(s) memory_zero((s), sizeof(*(s)))
#define memory_copy(dst, src, length) memmove((dst), (src), (length))
#endif // ARENA_H