Added: arena allocator
This commit is contained in:
parent
b9abdc8fce
commit
e882e68959
|
@ -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;
|
||||
}
|
|
@ -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
|
Loading…
Reference in New Issue
Block a user