Perform proper memory commit. Many bug fizes

This commit is contained in:
Nils O. Selåsdal
2026-06-09 20:15:12 +02:00
parent 6b9b058859
commit 2b4660f7d5
7 changed files with 234 additions and 143 deletions
+87 -46
View File
@@ -2,101 +2,142 @@
#include "base_core.h"
#include "platform.h"
MemoryBlock *memoryblock_allocate(size_t size)
typedef struct MemoryBlockOptions MemoryBlockOptions;
struct MemoryBlockOptions {
U32 size;
U32 commit_size;
U32 commit_now_size;
U32 page_size;
};
static MemoryBlock *memoryblock_allocate(const MemoryBlockOptions *opts)
{
void *base = memory_allocate(size);
Assert(base != NULL);
U32 aligned_size = AlignUpPow2(opts->size, opts->page_size);
U32 aligned_commit_size = AlignUpPow2(opts->commit_size, opts->page_size);
U32 aligned_commit_now_size = AlignUpPow2(opts->commit_now_size, opts->page_size);
MemoryBlock *block = (MemoryBlock *)base;
block->base = (U8 *)base;
block->size = size;
block->pos = MemoryBlock_Header_Size;
block->base_pos = 0;
block->prev = NULL;
MemoryBlock *block = memory_reserve(aligned_size);
Assert(block != NULL);
memory_commit(block, aligned_commit_now_size);
block->base = (U8 *)block;
block->size = aligned_size;
block->first_free = MemoryBlock_Header_Size;
block->arena_offset = 0;
block->prev = NULL;
block->committed_end = aligned_commit_now_size;
ASAN_POISON_MEMORY_REGION(block->base, block->size);
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
return block;
}
void arena_init(Arena *arena, const ArenaParams *params)
void arena_init(Arena *arena, const ArenaOptions *opts)
{
Assert(params->size > MemoryBlock_Header_Size);
Assert(params->size < (U32_Max - MemoryBlock_Header_Size - 1));
U32 page_size = platform_page_size();
Assert(opts->size > MemoryBlock_Header_Size);
Assert(opts->size < (U32_Max - page_size)); // Reserving last piece so our math doesn't overflow
Assert(opts->commit_size > 0);
Assert(opts->commit_size <= opts->size);
MemoryBlock *block = memoryblock_allocate(params->size);
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
MemoryBlockOptions mb_opts;
mb_opts.page_size = page_size;
mb_opts.commit_size = opts->commit_size;
mb_opts.size = opts->size;
mb_opts.commit_now_size = opts->commit_size;
MemoryBlock *block = memoryblock_allocate(&mb_opts);
arena->flags = params->flags;
arena->flags = opts->flags;
arena->checkpoint_level = 0;
arena->current = block;
arena->requested_size = params->size;
arena->block_size = opts->size;
arena->commit_size = opts->commit_size;
arena->page_size = page_size;
ASAN_POISON_MEMORY_REGION(&block->base[block->pos], memoryblock_available(block));
}
void arena_free(Arena *arena)
{
MemoryBlock *block = arena->current;
Assert(arena->checkpoint_level == 0);
while (block != NULL) {
MemoryBlock *prev = block->prev;
memory_free(block->base, block->size);
block = prev;
}
arena->current = NULL;
}
void *arena_push(Arena *arena, U32 size, U32 align)
{
Assert(size < (U32_Max - align - MemoryBlock_Header_Size - 1));
Assert(size < (U32_Max - platform_page_size()));
Assert(IsPow2(align));
MemoryBlock *block = arena->current;
U32 start_pos = AlignUpPow2(block->pos, align);
U32 available = memoryblock_available(block);
U32 start_pos = AlignUpPow2(block->first_free, align); // might align past block->size, handled in below check
U32 available = block->size - start_pos;
if (available < size) {
U32 new_size = arena->requested_size; // original user requeted size
if (new_size < size) {
new_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
if (start_pos > block->size || available < size) {
U32 block_size = arena->block_size; // original user requested size
U32 new_commit_size = arena->commit_size;
if (size > block_size - MemoryBlock_Header_Size) {
block_size = AlignUpPow2(size + MemoryBlock_Header_Size,align);
new_commit_size = block_size;
}
U32 commit_now_size = Max(new_commit_size, size);
MemoryBlock *new_block = memoryblock_allocate(new_size);
new_block->base_pos = block->base_pos + block->size;
MemoryBlockOptions mb_opts;
mb_opts.page_size = arena->page_size;
mb_opts.commit_size = new_commit_size;
mb_opts.size = block_size;
mb_opts.commit_now_size = commit_now_size;
MemoryBlock *new_block = memoryblock_allocate(&mb_opts);
// arena_offset tracks the virtual absolute position within an arena
// This is not a contiguous space, we use it to free blocks when popping the arena
new_block->arena_offset = block->arena_offset + block->size;
new_block->prev = block;
arena->current = new_block;
block = new_block;
start_pos = AlignUpPow2(block->pos, align);
start_pos = AlignUpPow2(block->first_free, align);
}
U32 end_pos = start_pos + size;
Assert(end_pos <= block->size);
U8 *mem = &block->base[start_pos];
block->pos = end_pos;
U8 *mem = &block->base[start_pos];
if (block->committed_end < end_pos) {
// Commit more memory
U32 aligned_end_pos = AlignUpPow2(start_pos + size, arena->page_size);
U32 aligned_size = RoundUpToMultiple(aligned_end_pos - block->committed_end, arena->commit_size);
aligned_size = Min(aligned_size, block->size - block->committed_end);
memory_commit(&block->base[block->committed_end], aligned_size);
block->committed_end += aligned_size;
Assert(block->committed_end <= block->size);
}
block->first_free = end_pos;
ASAN_UNPOISON_MEMORY_REGION(mem, size);
return mem;
}
void arena_pop_to(Arena *arena, U32 abs_pos)
void arena_reset_to(Arena *arena, U64 total_offset)
{
MemoryBlock *block = arena->current;
abs_pos = Max(MemoryBlock_Header_Size, abs_pos);
total_offset = Max(MemoryBlock_Header_Size, total_offset);
while (block) {
MemoryBlock *prev = block->prev;
if (block->base_pos >= abs_pos) {
if (block->arena_offset >= total_offset) {
ASAN_UNPOISON_MEMORY_REGION(block->base, block->size);
memory_free(block->base, block->size);
arena->current = prev;
} else {
U32 new_pos = abs_pos - block->base_pos;
U64 new_pos = total_offset - block->arena_offset;
new_pos = Max(MemoryBlock_Header_Size, new_pos);
// Note, when doing commit/release, new_pos <= current->pos
// is the right thing . shouldn't be able to pop to larger than what's already commited
Assert(new_pos <= block->size);
block->pos = new_pos;
ASAN_POISON_MEMORY_REGION(&block->base[block->pos], memoryblock_available(block));
// Disallow arbitrary pops to positions that previously were not commited.
Assert(new_pos <= block->committed_end);
block->first_free = new_pos;
ASAN_POISON_MEMORY_REGION(&block->base[block->first_free], memoryblock_available(block));
break;
}
@@ -104,25 +145,25 @@ void arena_pop_to(Arena *arena, U32 abs_pos)
}
}
ArenaCheckPoint arena_temp_begin(Arena *arena)
ArenaCheckpoint arena_temp_begin(Arena *arena)
{
ArenaCheckPoint checkpoint = {};
ArenaCheckpoint checkpoint = {};
checkpoint.arena = arena;
checkpoint.absolute_pos = arena_used(arena);
checkpoint.total_offset = arena_used(arena);
arena->checkpoint_level++;
return checkpoint;
}
void arena_temp_end(ArenaCheckPoint *checkpoint)
void arena_temp_end(ArenaCheckpoint *checkpoint)
{
Arena *arena = checkpoint->arena;
Assert(arena->checkpoint_level > 0);
arena_pop_to(arena, checkpoint->absolute_pos);
arena_reset_to(arena, checkpoint->total_offset);
arena->checkpoint_level--;
}
void arena_reset(Arena *arena)
{
arena_pop_to(arena, 0);
arena_reset_to(arena, 0);
}