Organization cleanup, stage arena to Scratch_Arena

This commit is contained in:
Krzosa Karol
2023-01-01 13:15:20 +01:00
parent 39f46900b2
commit c238e5ba46
8 changed files with 143 additions and 137 deletions

135
base.cpp
View File

@@ -158,6 +158,25 @@ struct Allocator {
Deallocate *deallocate;
};
CORE_Static void memory_zero(void *p, size_t size);
CORE_Static void deallocate_stub(Allocator *, void *) {}
#define allocate_array(a, T, size,...) (T *)allocate_size(a, sizeof(T)*(size),##__VA_ARGS__)
#define allocate_struct(a, T, ...) allocate_array(a, T, 1,##__VA_ARGS__)
CORE_Static void *allocate_size(Allocator *allocator, size_t size, bool zero_memory = true) {
void *result = allocator->allocate(allocator, size);
if (zero_memory) {
memory_zero(result, size);
}
return result;
}
CORE_Static void deallocate(Allocator *allocator, void *p) {
assert(p);
allocator->deallocate(allocator, p);
}
//-----------------------------------------------------------------------------
// Utilities
//-----------------------------------------------------------------------------
@@ -406,119 +425,3 @@ operator!=(Intern_String a, Intern_String b){
#define Iter_Named(list, it) for(auto it = iterate(list); should_we_continue(&it); advance(&it))
#define Iter(list) Iter_Named(list, it)
#define allocate_array(a, T, size,...) (T *)allocate_size(a, sizeof(T)*(size),##__VA_ARGS__)
#define allocate_struct(a, T, ...) allocate_array(a, T, 1,##__VA_ARGS__)
CORE_Static void *allocate_size(Allocator *allocator, size_t size, bool zero_memory = true) {
void *result = allocator->allocate(allocator, size);
if (zero_memory) {
memory_zero(result, size);
}
return result;
}
CORE_Static void deallocate(Allocator *allocator, void *p) {
assert(p);
allocator->deallocate(allocator, p);
}
//-----------------------------------------------------------------------------
// Memory OS
//-----------------------------------------------------------------------------
struct OS_Memory{
size_t commit, reserve;
U8 *data;
};
CORE_Static OS_Memory os_reserve(size_t size);
CORE_Static B32 os_commit(OS_Memory *m, size_t size);
CORE_Static void os_release(OS_Memory *m);
CORE_Static B32 os_decommit_pos(OS_Memory *m, size_t pos);
//-----------------------------------------------------------------------------
// Memory arenas
//-----------------------------------------------------------------------------
global const size_t default_reserve_size = gib(4);
global const size_t default_alignment = 8;
global const size_t additional_commit_size = mib(1);
struct Arena : Allocator {
OS_Memory memory;
size_t alignment;
size_t len;
String debug_string;
};
CORE_Static void
arena_pop_pos(Arena *arena, size_t pos){
pos = clamp_top(pos, arena->len);
arena->len = pos;
}
CORE_Static void *
arena_pop(Arena *arena, size_t size){
size = clamp_top(size, arena->len);
arena->len -= size;
return arena->memory.data + arena->len;
}
CORE_Static void
arena_release(Arena *arena){
os_release(&arena->memory);
}
CORE_Static void
arena_clear(Arena *arena){
arena_pop_pos(arena, 0);
}
CORE_Static void
deallocate_stub(Allocator *, void *) {
}
CORE_Static void *
arena_push_size(Arena *a, size_t size){
size_t generous_size = size + a->alignment;
if(a->len+generous_size>a->memory.commit){
assert(a->memory.reserve > 0);
B32 result = os_commit(&a->memory, generous_size+additional_commit_size);
assert(result);
}
a->len = align_up(a->len, a->alignment);
assert(a->memory.reserve > a->len + size);
void *result = (U8*)a->memory.data + a->len;
a->len += size;
return result;
}
CORE_Static Arena
push_arena(Allocator *allocator, size_t size, String debug_name) {
Arena result = {};
result.memory.data = (U8 *)allocate_size(allocator, size);
result.memory.reserve = size;
result.alignment = default_alignment;
result.debug_string = debug_name;
result.allocate = (Allocator::Allocate *)arena_push_size;
result.deallocate = (Allocator::Deallocate *)deallocate_stub;
return result;
}
CORE_Static void
arena_init(Arena *a, String debug_name){
a->memory = os_reserve(default_reserve_size);
a->alignment = default_alignment;
a->debug_string = debug_name;
a->allocate = (Allocator::Allocate *)arena_push_size;
a->deallocate = (Allocator::Deallocate *)deallocate_stub;
}
CORE_Static Arena
arena_sub(Arena *base, size_t size, String debug_name) {
Arena result = {};
result.memory.data = (U8 *)arena_push_size(base, size);
result.memory.commit = size;
result.memory.reserve = size;
result.alignment = default_alignment;
result.len = 0;
return result;
}