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->res = res;
arena->align = 8;
arena->grow = true;
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
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_new = pos_mem + size;
if (cur->res < pos_new && arena->grow) {
Arena *new_block;
if (size < arena_huge_push_threshold()) {
new_block = arena_alloc();
} else {
u64 new_block_size = size + ARENA_HEADER_SIZE;
new_block = arena_alloc_sized(new_block_size, new_block_size);
if (cur->res < pos_new) {
u64 res_size = cur->res;
u64 cmt_size = cur->cmt;
if (size > res_size) {
res_size = size + ARENA_HEADER_SIZE;
cmt_size = size + ARENA_HEADER_SIZE;
}
if (new_block) {
Arena *new_block = arena_alloc_sized(res_size, cmt_size);
new_block->base_pos = cur->base_pos + cur->res;
new_block->prev = arena->cur;
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_new = pos_mem + size;
}
}
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_size = cmt_new_clamped - cur->cmt;
b32x is_cmt_ok = os_commit((u8*)cur + cur->cmt, cmt_new_size);
if (is_cmt_ok)
os_commit((u8*)cur + cur->cmt, cmt_new_size);
cur->cmt = cmt_new_clamped;
}
@ -151,10 +137,16 @@ void *arena_push_impl(Arena *arena, u64 size)
if (cur->cmt >= pos_new) {
mem = (u8*)cur + pos_mem;
u64 added_size = pos_new - cur->pos;
assert(added_size >= size);
arena->mem_used += added_size;
arena->mem_peak_used = max(arena->mem_peak_used, arena->mem_used);
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;

View file

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