2 Commits
Author SHA1 Message Date
noselasd b096434964 Add lazy commit option 2026-09-23 18:33:29 +02:00
noselasd 25dc1cf8f5 Simplify 2026-09-21 20:45:26 +02:00
7 changed files with 48 additions and 15 deletions
+3 -4
View File
@@ -10,7 +10,7 @@ static MemoryBlock *memoryblock_allocate(U32 size, U32 commit_size, U32 page_siz
MemoryBlock *block = memory_reserve(size);
Assert(block != NULL);
memory_commit(block, commit_size);
memory_commit(block, commit_size, false);
ASAN_POISON_MEMORY_REGION(block, size);
ASAN_UNPOISON_MEMORY_REGION(block, MemoryBlock_Header_Size);
@@ -38,7 +38,7 @@ void arena_init(Arena *arena, const ArenaOptions *opts)
MemoryBlock *block = memoryblock_allocate(aligned_size, aligned_commit_size, page_size);
arena->flags = opts->flags;
arena->temp_level = 0;
arena->temp_level = 0;
arena->current = block;
arena->block_size = aligned_size;
arena->commit_size = aligned_commit_size;
@@ -61,7 +61,6 @@ void *arena_push(Arena *arena, U32 size, U32 align)
{
Assert(IsPow2(align));
MemoryBlock *block = arena->current;
U32 start_pos = AlignUp(block->first_free, align); // might align past block->size, handled in below check
U32 available = block->size - start_pos;
@@ -97,7 +96,7 @@ void *arena_push(Arena *arena, U32 size, U32 align)
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);
memory_commit(&block->base[block->committed_end], aligned_size, false);
block->committed_end += aligned_size;
Assert(block->committed_end <= block->size);
}
+1 -1
View File
@@ -89,7 +89,7 @@ static INLINE void *arena_allocator_func(Allocator *allocator, AllocOperation op
return NULL;
}
Assert(false);
ShouldNotHappen;
}
static INLINE Allocator arena_allocator(Arena *arena)
+2 -2
View File
@@ -423,7 +423,7 @@ static INLINE void allocator_free_all(Allocator *allocator)
*/
/**
* Initialize a list
* Initialize a list, can also just be zero initialized
*
* @param list struct pointer with a *first and a **last member to an entry struct
*/
@@ -484,7 +484,7 @@ do { \
*/
/**
* Initialize a list
* Initialize a list, can also just be zero initialized
*
* @param list struct pointer with a *first and a *last member to an entry struct
*/
+5 -3
View File
@@ -47,7 +47,7 @@ static inline U32 lbh_offset(LAlloc *allocator, LBlockHeader *block)
return NULL_BLOCK;
}
return (U32)((uintptr_t)block - (uintptr_t)allocator->base);
return (U32)((UMM)block - (uintptr_t)allocator->base);
}
static void lmaybe_split_block(LAlloc *allocator, LBlockHeader *block, U32 size)
@@ -98,7 +98,9 @@ static void lmerge_with_next(LAlloc *allocator, LBlockHeader *block)
lblock_set_size(block, new_size);
block->next = next->next;
LBlockHeader *nextnext = lblock_from_offset(allocator, next->next);
if (nextnext) nextnext->prev = lbh_offset(allocator, block);
if (nextnext != NULL) {
nextnext->prev = lbh_offset(allocator, block);
}
// note - caller have to adjust next_alloc on return
}
@@ -131,7 +133,7 @@ void *lalloc(LAlloc *allocator, U32 size)
}
size = AlignUp(size, BLOCK_ALIGNMENT);
LBlockHeader *start = allocator->next_alloc ? allocator->next_alloc : (LBlockHeader *)allocator->base;
LBlockHeader *start = allocator->next_alloc;
LBlockHeader *curr = start;
do {
+6 -2
View File
@@ -91,7 +91,7 @@ void *memory_reserve(size_t size)
return mem;
}
void memory_commit(void *mem, size_t size)
void memory_commit(void *mem, size_t size, bool lazy)
{
Assert(IsPageAligned((UMM)mem));
Assert(IsPageAligned(size));
@@ -100,7 +100,11 @@ void memory_commit(void *mem, size_t size)
int rc;
rc = mprotect(mem, size, PROT_READ | PROT_WRITE);
Assert(rc == 0);
rc = madvise(mem, size, MADV_WILLNEED);
if (lazy) {
rc = madvise(mem, size, MADV_POPULATE_WRITE);
} else {
rc = madvise(mem, size, MADV_WILLNEED);
}
Assert(rc == 0);
}
+2 -1
View File
@@ -11,6 +11,7 @@ U32 platform_page_size(void);
// Memory functions expect mem and size to be page aligned
void *memory_allocate_and_commit(size_t size);
void *memory_reserve(size_t size);
void memory_commit(void *mem, size_t size);
void memory_commit(void *mem, size_t size, bool lazy);
void memory_decommit(void *mem, size_t size);
void memory_free(void *mem, size_t size);
+29 -2
View File
@@ -8,10 +8,14 @@
#include <stdio.h>
#include <stdlib.h>
#define ARENA_DEBUG 1
void arena_dump(const Arena *arena)
{
return;
if (!ARENA_DEBUG) {
return;
}
MemoryBlock *block = arena->current;
puts("@@@@@@@@@@");
while (block) {
@@ -19,7 +23,7 @@ void arena_dump(const Arena *arena)
printf("base: %p\n", block->base);
printf("size: %u\n", block->size);
printf("first_free: %u\n", block->first_free);
printf("total_used: %lu\n",block->arena_offset);
printf("arena_offset: %lu\n",block->arena_offset);
printf("prev: %p\n", block->prev);
puts("-----Arena end-");
block = block->prev;
@@ -166,6 +170,28 @@ static void arena_test_4(void)
arena_free(&arena);
}
static void arena_test_bigger_push(void)
{
TRACEF("\n\n");
ArenaOptions arena_params = {};
U32 arena_size = KB(256) + MemoryBlock_Header_Size;
U32 commit_size = KB(8);
arena_params.size = arena_size;
arena_params.commit_size = commit_size;
Arena arena;
arena_init(&arena, &arena_params);
arena_dump(&arena);
void *ptr = arena_push(&arena, KB(256), 1);
MemoryZero(ptr, 512);
printf("Allocated ptr %p\n", ptr);
arena_dump(&arena);
ptr = arena_push(&arena, MB(4), 4);
MemoryZero(ptr, MB(4));
printf("Allocated ptr %p\n", ptr);
arena_dump(&arena);
arena_free(&arena);
}
static void arena_test_allocator_interface(void)
{
TRACEF("\n\n");
@@ -741,6 +767,7 @@ int main(int argc, char *argv[])
arena_test_4();
arena_test_decommit();
arena_test_allocator_interface();
arena_test_bigger_push();
miniarena_test1();
apool_test();
apool_test_autogrow();