add memory tracking

This commit is contained in:
silverweed 2024-07-12 11:58:48 +02:00
parent edd376b4ba
commit c2531be115
2 changed files with 18 additions and 20 deletions

View file

@ -39,6 +39,7 @@ Arena *arena_alloc_sized(u64 init_res, u64 init_cmt)
arena->res = res;
arena->align = 8;
arena->grow = true;
arena->mem_used = arena->mem_peak_used = ARENA_HEADER_SIZE;
}
return arena;
@ -59,16 +60,6 @@ void arena_release(Arena *arena)
}
}
internal u64
arena_mem_used(Arena *arena)
{
u64 tot = 0;
for (Arena *node = arena->cur, *prev = 0; node; node = prev) {
tot += node->pos - node->base_pos;
}
return tot;
}
internal
u64 arena_huge_push_threshold()
{
@ -117,8 +108,11 @@ void *arena_push_impl(Arena *arena, u64 size)
void *mem = 0;
if (cur->cmt >= pos_new) {
mem = (u8*)cur + pos_mem;
u64 added_size = pos_new - cur->pos;
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, size);
asan_unpoison_memory_region(mem, added_size);
}
return mem;
@ -158,15 +152,15 @@ T *arena_push_array(Arena *arena, u64 count)
return ary;
}
// internal
// u8 *arena_push_contiguous(Arena *arena, u64 size)
// {
// b32 restore = arena->grow;
// arena->grow = 0;
// void *mem = arena_push_impl(arena, size);
// arena->grow = restore;
// return (u8 *)mem;
// }
internal
u8 *arena_push_contiguous(Arena *arena, u64 size)
{
b32 restore = arena->grow;
arena->grow = 0;
void *mem = arena_push_impl(arena, size);
arena->grow = restore;
return (u8 *)mem;
}
internal
u64 arena_pos(Arena *arena)
@ -196,6 +190,8 @@ void arena_pop_to(Arena *arena, u64 big_pos_unclamped)
// poison popped memory block
asan_poison_memory_region((u8*)current + new_pos, (current->pos - new_pos));
arena->mem_used -= (current->pos - new_pos);
// update position
current->pos = new_pos;

View file

@ -10,6 +10,8 @@ struct Arena {
u64 cmt;
u64 res;
u64 align;
u64 mem_used;
u64 mem_peak_used;
b8 grow;
};