50 lines
1.1 KiB
C
50 lines
1.1 KiB
C
global const SizeU default_reserve_size = gib(4);
|
|
global const SizeU default_alignment = 8;
|
|
global const SizeU additional_commit_size = mib(1);
|
|
|
|
function void
|
|
memory_copy(U8 *dst, U8 *src, SizeU size){
|
|
for(SizeU i = 0; i < size; i++){
|
|
dst[i] = src[i];
|
|
}
|
|
}
|
|
|
|
function void
|
|
memory_zero(void *p, SizeU size){
|
|
U8 *pp = p;
|
|
for(SizeU i = 0; i < size; i++)
|
|
pp[i] = 0;
|
|
}
|
|
|
|
function void
|
|
arena_init(Arena *a){
|
|
a->memory = os_reserve(default_reserve_size);
|
|
a->alignment = default_alignment;
|
|
}
|
|
|
|
function void *
|
|
arena_push_size(Arena *a, SizeU size){
|
|
SizeU generous_size = size;
|
|
if(a->memory.commit+generous_size>a->memory.commit){
|
|
if(a->memory.reserve == 0){
|
|
arena_init(a);
|
|
}
|
|
os_commit(&a->memory, generous_size+additional_commit_size);
|
|
}
|
|
|
|
a->len = align_up(a->len, a->alignment);
|
|
void *result = (U8*)a->memory.data + a->len;
|
|
a->len += size;
|
|
|
|
memory_zero(result, size);
|
|
return result;
|
|
}
|
|
|
|
function String
|
|
arena_push_string_copy(Arena *arena, String string){
|
|
U8 *copy = arena_push_array(arena, U8, string.len+1);
|
|
memory_copy(copy, string.str, string.len);
|
|
copy[string.len] = 0;
|
|
return (String){copy, string.len};
|
|
}
|