update arena

This commit is contained in:
silverweed 2024-08-09 16:13:44 +02:00
parent ef5ad47f61
commit f8c7c961ce
2 changed files with 23 additions and 32 deletions

View file

@ -80,7 +80,6 @@ Arena *arena_alloc_sized(u64 init_res, u64 init_cmt)
arena->cmt = cmt; arena->cmt = cmt;
arena->res = res; arena->res = res;
arena->align = 8; arena->align = 8;
arena->grow = true;
arena->mem_used = arena->mem_peak_used = ARENA_HEADER_SIZE; arena->mem_used = arena->mem_peak_used = ARENA_HEADER_SIZE;
} }
@ -102,14 +101,6 @@ void arena_release(Arena *arena)
} }
} }
internal
u64 arena_huge_push_threshold()
{
u64 res = ARENA_RESERVE_SIZE;
u64 threshold = (res - ARENA_HEADER_SIZE) / 2 + 1;
return threshold;
}
internal internal
void *arena_push_impl(Arena *arena, u64 size) void *arena_push_impl(Arena *arena, u64 size)
{ {
@ -117,16 +108,14 @@ void *arena_push_impl(Arena *arena, u64 size)
u64 pos_mem = align_pow2(cur->pos, arena->align); u64 pos_mem = align_pow2(cur->pos, arena->align);
u64 pos_new = pos_mem + size; u64 pos_new = pos_mem + size;
if (cur->res < pos_new && arena->grow) { if (cur->res < pos_new) {
Arena *new_block; u64 res_size = cur->res;
if (size < arena_huge_push_threshold()) { u64 cmt_size = cur->cmt;
new_block = arena_alloc(); if (size > res_size) {
} else { res_size = size + ARENA_HEADER_SIZE;
u64 new_block_size = size + ARENA_HEADER_SIZE; cmt_size = size + ARENA_HEADER_SIZE;
new_block = arena_alloc_sized(new_block_size, new_block_size);
} }
Arena *new_block = arena_alloc_sized(res_size, cmt_size);
if (new_block) {
new_block->base_pos = cur->base_pos + cur->res; new_block->base_pos = cur->base_pos + cur->res;
new_block->prev = arena->cur; new_block->prev = arena->cur;
arena->cur = new_block; arena->cur = new_block;
@ -135,15 +124,12 @@ void *arena_push_impl(Arena *arena, u64 size)
pos_mem = align_pow2(cur->pos, cur->align); pos_mem = align_pow2(cur->pos, cur->align);
pos_new = pos_mem + size; pos_new = pos_mem + size;
} }
}
if (cur->cmt < pos_new) { if (cur->cmt < pos_new) {
u64 cmt_new_aligned = align_pow2(pos_new, ARENA_COMMIT_SIZE); u64 cmt_new_aligned = align_pow2(pos_new, cur->cmt);
u64 cmt_new_clamped = min(cmt_new_aligned, cur->res); u64 cmt_new_clamped = min(cmt_new_aligned, cur->res);
u64 cmt_new_size = cmt_new_clamped - cur->cmt; u64 cmt_new_size = cmt_new_clamped - cur->cmt;
b32x is_cmt_ok = os_commit((u8*)cur + cur->cmt, cmt_new_size); os_commit((u8*)cur + cur->cmt, cmt_new_size);
if (is_cmt_ok)
cur->cmt = cmt_new_clamped; cur->cmt = cmt_new_clamped;
} }
@ -151,10 +137,16 @@ void *arena_push_impl(Arena *arena, u64 size)
if (cur->cmt >= pos_new) { if (cur->cmt >= pos_new) {
mem = (u8*)cur + pos_mem; mem = (u8*)cur + pos_mem;
u64 added_size = pos_new - cur->pos; u64 added_size = pos_new - cur->pos;
assert(added_size >= size);
arena->mem_used += added_size; arena->mem_used += added_size;
arena->mem_peak_used = max(arena->mem_peak_used, arena->mem_used); arena->mem_peak_used = max(arena->mem_peak_used, arena->mem_used);
cur->pos = pos_new; cur->pos = pos_new;
asan_unpoison_memory_region(mem, added_size); asan_unpoison_memory_region(mem, size);
}
if (UNLIKELY(!mem)) {
fprintf(stderr, "Failed to grow arena.\n");
exit(1);
} }
return mem; return mem;

View file

@ -12,7 +12,6 @@ struct Arena {
u64 align; u64 align;
u64 mem_used; u64 mem_used;
u64 mem_peak_used; u64 mem_peak_used;
b8 grow;
}; };
struct Temp { struct Temp {