BlockArena, a bit of allocator rework

This commit is contained in:
Krzosa Karol
2025-11-30 19:54:46 +01:00
parent c19c60fe22
commit c2f4686bc4
15 changed files with 337 additions and 224 deletions

View File

@@ -10,6 +10,7 @@
#define MA_ASAN_UNPOISON_MEMORY_REGION(addr, size) ASAN_UNPOISON_MEMORY_REGION(addr, size)
#endif
#include <stdlib.h>
#if OS_WINDOWS
#ifndef NOMINMAX
@@ -20,44 +21,44 @@
#endif
#include <windows.h>
void *VReserve(size_t size) {
API void *VReserve(size_t size) {
void *result = (uint8_t *)VirtualAlloc(0, size, MEM_RESERVE, PAGE_READWRITE);
return result;
}
bool VCommit(void *p, size_t size) {
API bool VCommit(void *p, size_t size) {
void *result = VirtualAlloc(p, size, MEM_COMMIT, PAGE_READWRITE);
return result ? true : false;
}
bool VRelease(void *p, size_t size) {
API bool VRelease(void *p, size_t size) {
BOOL result = VirtualFree(p, 0, MEM_RELEASE);
return result ? true : false;
}
bool VDecommit(void *p, size_t size) {
API bool VDecommit(void *p, size_t size) {
BOOL result = VirtualFree(p, size, MEM_DECOMMIT);
return result ? true : false;
}
#elif OS_LINUX || OS_MAC
void *VReserve(size_t size) {
API void *VReserve(size_t size) {
void *result = mmap(0, size, PROT_NONE, MAP_PRIVATE | MAP_ANONYMOUS, -1, (off_t)0);
return result == (void *)-1 ? 0 : result;
}
bool VCommit(void *p, size_t size) {
API bool VCommit(void *p, size_t size) {
int result = mprotect(p, size, PROT_READ | PROT_WRITE);
return result == 0;
}
bool VRelease(void *p, size_t size) {
API bool VRelease(void *p, size_t size) {
int result = munmap(p, size);
return result == 0;
}
bool VDecommit(void *p, size_t size) {
API bool VDecommit(void *p, size_t size) {
mprotect(p, size, PROT_NONE);
madvise(p, size, MADV_DONTNEED);
return true;
@@ -65,29 +66,102 @@ bool VDecommit(void *p, size_t size) {
#else
void *VReserve(size_t size) {
API void *VReserve(size_t size) {
InvalidCodepath();
return NULL;
}
bool VCommit(void *p, size_t size) {
API bool VCommit(void *p, size_t size) {
InvalidCodepath();
return false;
}
bool VRelease(void *p, size_t size) {
API bool VRelease(void *p, size_t size) {
InvalidCodepath();
return false;
}
bool VDecommit(void *p, size_t size) {
API bool VDecommit(void *p, size_t size) {
InvalidCodepath();
return false;
}
#endif
void InitArena(Arena *arena, size_t reserve) {
API void *SystemAllocatorProc(void *object, int kind, void *p, size_t size) {
void *result = NULL;
if (kind == AllocatorKind_Allocate) {
result = malloc(size);
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
} else {
InvalidCodepath();
}
return result;
}
API Allocator GetSystemAllocator() {
Allocator result = {SystemAllocatorProc};
return result;
}
API void *_AllocSize_(Allocator alo, size_t size) {
void *result = alo.proc(alo.object, AllocatorKind_Allocate, NULL, size);
memset(result, 0, size);
return result;
}
struct MemoryRecord {
size_t size;
void *addr;
char *file;
int line;
};
thread_local Array<MemoryRecord> MemoryTrackingRecord;
API void *TrackingAllocatorProc(void *object, int kind, void *p, size_t size) {
void *result = NULL;
if (kind == AllocatorKind_Allocate) {
result = malloc(size);
Add(&MemoryTrackingRecord, {size, result, LocationTraceO.file, LocationTraceO.line});
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
bool found = false;
For(IterateInReverse(&MemoryTrackingRecord)) {
if (it.addr == p) {
found = true;
UnorderedRemove(&MemoryTrackingRecord, it);
break;
}
}
Assert(found);
} else {
InvalidCodepath();
}
return result;
}
API void TrackingAllocatorCheck() {
// For (MemoryTrackingRecord) {
// ReportConsolef("%s(%d): error: memory leak");
// }
Assert(MemoryTrackingRecord.len == 0);
}
API Allocator GetTrackingAllocator() {
Allocator result = {TrackingAllocatorProc};
return result;
}
///////////////////////////////
// Virtual Arena
API void InitArena(VirtualArena *arena, size_t reserve) {
reserve = AlignUp(reserve, PAGE_SIZE);
arena->align = DEFAULT_ALIGNMENT;
arena->data = (uint8_t *)VReserve(reserve);
@@ -96,8 +170,8 @@ void InitArena(Arena *arena, size_t reserve) {
}
}
Arena *AllocArena(Allocator allocator, size_t size) {
Arena *result = AllocType(allocator, Arena);
API VirtualArena *AllocArena(Allocator allocator, size_t size) {
VirtualArena *result = AllocType(allocator, VirtualArena);
result->data = (uint8_t *)AllocSize(allocator, size);
result->reserve = size;
result->commit = size;
@@ -105,8 +179,8 @@ Arena *AllocArena(Allocator allocator, size_t size) {
return result;
}
Arena *AllocArena(size_t reserve) {
Arena *result = NULL;
API VirtualArena *AllocArena(size_t reserve) {
VirtualArena *result = NULL;
void *data = VReserve(reserve);
if (!data) return result;
@@ -117,16 +191,16 @@ Arena *AllocArena(size_t reserve) {
return result;
}
result = (Arena *)data;
result = (VirtualArena *)data;
result->data = (uint8_t *)data;
result->reserve = reserve;
result->commit = PAGE_SIZE;
result->len = result->base_len = sizeof(Arena);
result->len = result->base_len = sizeof(VirtualArena);
result->align = DEFAULT_ALIGNMENT;
return result;
}
void *PushSize(Arena *arena, size_t size) {
API void *PushSize(VirtualArena *arena, size_t size) {
// base_len is used for bootstraping arenas, it denotes the
// space occupied by the arena. If len is smaller then base_len then
// we start to overwrite the arena itself - pure barbarism.
@@ -145,7 +219,7 @@ void *PushSize(Arena *arena, size_t size) {
if (to_commit_clamped > 0) {
bool success = VCommit(arena->data + arena->commit, to_commit_clamped);
if (success) {
MA_ASAN_UNPOISON_MEMORY_REGION(arena->data + arena->commit, to_commit_clamped);
MA_ASAN_POISON_MEMORY_REGION(arena->data + arena->commit, to_commit_clamped);
arena->commit += to_commit_clamped;
}
}
@@ -159,14 +233,14 @@ void *PushSize(Arena *arena, size_t size) {
return (void *)result;
}
void Release(Arena *arena) {
API void Release(VirtualArena *arena) {
if (arena == NULL || arena->data == NULL) return;
bool zero_memory = (uint8_t *)arena != arena->data;
VRelease(arena->data, arena->reserve);
if (zero_memory) MemoryZero(arena, sizeof(Arena));
if (zero_memory) MemoryZero(arena, sizeof(VirtualArena));
}
void PopToPos(Arena *arena, size_t pos) {
API void PopToPos(VirtualArena *arena, size_t pos) {
// base_len is used for bootstraping arenas, it denotes the
// space occupied by the arena. If len is smaller then base_len then
// we start to overwrite the arena itself - pure barbarism.
@@ -178,9 +252,9 @@ void PopToPos(Arena *arena, size_t pos) {
MA_ASAN_POISON_MEMORY_REGION(arena->data + arena->len, size);
}
void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
API void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
if (kind == AllocatorKind_Allocate) {
return PushSize((Arena *)object, size);
return PushSize((VirtualArena *)object, size);
} else if (AllocatorKind_Deallocate) {
} else {
Assert(!"invalid codepath");
@@ -188,106 +262,123 @@ void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
return NULL;
}
thread_local Arena *ScratchArenaPool[4];
///////////////////////////////
// Block Arena
#if OS_WASM
void InitScratch() {
Allocator sys_allocator = GetSystemAllocator();
ScratchArenaPool[0] = AllocArena(sys_allocator, MiB(16));
ScratchArenaPool[1] = AllocArena(sys_allocator, MiB(8));
ScratchArenaPool[3] = AllocArena(sys_allocator, MiB(2));
ScratchArenaPool[3] = AllocArena(sys_allocator, MiB(1));
}
#else
void InitScratch() {
for (int i = 0; i < Lengthof(ScratchArenaPool); i += 1) {
ScratchArenaPool[i] = AllocArena();
}
}
#endif
TempArena GetScratchEx(Arena **conflicts, int conflict_count) {
Arena *unoccupied = 0;
for (int i = 0; i < Lengthof(ScratchArenaPool); i += 1) {
Arena *from_pool = ScratchArenaPool[i];
unoccupied = from_pool;
for (int conflict_i = 0; conflict_i < conflict_count; conflict_i += 1) {
Arena *from_conflict = conflicts[conflict_i];
if (from_pool == from_conflict) {
unoccupied = 0;
break;
}
API void *PushSize(BlockArena *arena, size_t size) {
if (size > (size_t)(arena->end - arena->start)) {
size_t block_size = MiB(1);
if (size > block_size) {
block_size = size;
}
if (arena->allocator.proc == NULL) {
arena->allocator = GetSystemAllocator();
}
BlockArenaNode *new_block = (BlockArenaNode *)AllocSize(arena->allocator, block_size + sizeof(BlockArenaNode));
Assert(GetAlignOffset((size_t)new_block->start, DEFAULT_ALIGNMENT) == 0);
arena->start = new_block->start;
new_block->end = arena->end = new_block->start + block_size;
SLL_STACK_ADD(arena->blocks, new_block);
}
U8 *result = arena->start;
Assert(GetAlignOffset((size_t)result, DEFAULT_ALIGNMENT) == 0);
arena->start = (U8 *)AlignUp((size_t)(arena->start + size), DEFAULT_ALIGNMENT);
return result;
}
if (unoccupied) {
API void Release(BlockArena *arena) {
for (BlockArenaNode *it = arena->blocks, *next = NULL; it; it = next) {
next = it->next;
Dealloc(arena->allocator, it);
}
MemoryZero(arena, sizeof(BlockArena));
}
API void Unwind(BlockArena *arena, U8 *pos) {
bool contains = false;
for (BlockArenaNode *it = arena->blocks, *next = NULL; it; it = next) {
next = it->next;
if ((pos >= it->start) && (pos < it->end)) {
contains = true;
break;
} else {
arena->blocks = arena->blocks->next;
Dealloc(arena->allocator, it);
}
}
// Failed to get free scratch memory, this is a fatal error, this shouldnt happen
Assert(unoccupied);
TempArena result = BeginTemp(unoccupied);
return result;
Assert(contains || pos == NULL);
arena->start = pos;
}
#include <stdlib.h>
void *SystemAllocator_Alloc(void *object, int kind, void *p, size_t size) {
void *result = NULL;
API void *BlockArenaAllocatorProc(void *object, int kind, void *p, size_t size) {
BlockArena *arena = (BlockArena *)object;
if (kind == AllocatorKind_Allocate) {
result = malloc(size);
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
return PushSize(arena, size);
} else if (AllocatorKind_Deallocate) {
} else {
InvalidCodepath();
Assert(!"invalid codepath");
}
return result;
return NULL;
}
Allocator GetSystemAllocator() {
Allocator result = {SystemAllocator_Alloc};
return result;
}
struct MemoryRecord {
size_t size;
void *addr;
bool deallocated;
};
Array<MemoryRecord> MemoryTrackingRecord;
void *TrackingAllocatorProc(void *object, int kind, void *p, size_t size) {
void *result = NULL;
if (kind == AllocatorKind_Allocate) {
result = malloc(size);
Add(&MemoryTrackingRecord, {size, result});
Assert(result);
} else if (kind == AllocatorKind_Deallocate) {
free(p);
bool found = false;
For(MemoryTrackingRecord){
if (it.addr == p) {
it.deallocated = true;
found = true;
void TestArena() {
Allocator memory_tracking_allocator = GetTrackingAllocator();
{
BlockArena arena = {};
arena.allocator = memory_tracking_allocator;
for (int i = 0; i < 10000; i += 1) {
int *vals = (int *)PushSize(&arena, sizeof(int)*i);
for (int j = 0; j < i; j += 1) {
vals[j] = j;
}
}
Assert(found);
} else {
InvalidCodepath();
Release(&arena);
TrackingAllocatorCheck();
}
return result;
}
void TrackingAllocatorCheck() {
For (MemoryTrackingRecord) {
Assert(it.deallocated);
{
BlockArena arena = {};
U8 *start = arena.start;
arena.allocator = memory_tracking_allocator;
int *vals = (int *)PushSize(&arena, sizeof(int) * 32);
for (int i = 0; i < 32; i += 1) vals[i] = i;
Unwind(&arena, (U8 *)vals);
Assert(arena.blocks);
Assert(arena.blocks->next == NULL);
Assert(arena.start == (U8 *)vals);
Assert(arena.blocks[0].start == (U8 *)vals);
Unwind(&arena, NULL);
Dealloc(arena.allocator, arena.blocks);
TrackingAllocatorCheck();
}
{
BlockArena arena = {};
arena.allocator = memory_tracking_allocator;
int *vals = (int *)PushSize(&arena, sizeof(int) * 32);
for (int i = 0; i < 32; i += 1) vals[i] = i;
U8 *p = arena.start;
U8 *a = (U8 *)PushSize(&arena, KiB(32));
U8 *b = (U8 *)PushSize(&arena, KiB(1000));
Assert(arena.blocks);
Assert(arena.blocks->next);
Assert(arena.blocks->next->next == NULL);
Unwind(&arena, a);
Assert(arena.blocks);
Assert(arena.blocks->next == NULL);
Assert(arena.start == p);
Release(&arena);
TrackingAllocatorCheck();
}
{
BlockArena arena = {};
arena.allocator = memory_tracking_allocator;
U8 *a = (U8 *)PushSize(&arena, KiB(2000));
Assert((size_t)(arena.blocks[0].end - arena.blocks[0].start) == KiB(2000));
Release(&arena);
TrackingAllocatorCheck();
}
}
Allocator GetTrackingAllocator() {
Allocator result = {TrackingAllocatorProc};
return result;
}

View File

@@ -1,5 +1,17 @@
#pragma once
struct LocationTrace {
char *file;
int line;
};
#if DEBUG_BUILD
thread_local LocationTrace LocationTraceO;
#define LOCATION_TRACE (LocationTraceO.file = __FILE__, LocationTraceO.line = __LINE__)
#else
#define LOCATION_TRACE
#endif
const int AllocatorKind_Allocate = 1;
const int AllocatorKind_Deallocate = 2;
@@ -10,29 +22,47 @@ struct Allocator {
#define AllocType(alo, Type) (Type *)AllocSize(alo, sizeof(Type))
#define AllocArray(alo, Type, count) (Type *)AllocSize(alo, sizeof(Type) * (count))
inline void *AllocSize(Allocator alo, size_t size) {
void *result = alo.proc(alo.object, AllocatorKind_Allocate, NULL, size);
memset(result, 0, size);
return result;
}
#define AllocSize(alo, size) (LOCATION_TRACE, _AllocSize_(alo, size))
API void *_AllocSize_(Allocator alo, size_t size);
inline void Dealloc(Allocator alo, void *p) { if (p) (alo).proc((alo).object, AllocatorKind_Deallocate, (p), 0); }
template <class T>
void Dealloc(Allocator alo, T **p) {
if (*p == NULL) return;
alo.proc(alo.object, AllocatorKind_Deallocate, *p, 0);
*p = NULL;
}
#define DeallocEx(alo, p) (alo).proc((alo).object, AllocatorKind_Deallocate, (p), 0);
API Allocator GetSystemAllocator();
API void TrackingAllocatorCheck();
API Allocator GetTrackingAllocator();
Allocator GetSystemAllocator();
#define MemoryZero(x, size) memset(x, 0, size)
#define MemoryCopy(dst, src, size) memcpy(dst, src, size)
#define MemoryMove(dst, src, size) memmove(dst, src, size)
const int PAGE_SIZE = 4096;
const int DEFAULT_ALIGNMENT = sizeof(void *);
///////////////////
// Block Arena
///////////////////
API void *BlockArenaAllocatorProc(void *object, int kind, void *p, size_t size);
struct Arena {
struct BlockArenaNode {
BlockArenaNode *next;
U8 *end;
U8 start[0];
};
struct BlockArena {
U8 *start;
U8 *end;
BlockArenaNode *blocks;
Allocator allocator;
operator Allocator() { return {BlockArenaAllocatorProc, this}; }
};
API void *PushSize(BlockArena *arena, size_t size);
API void Release(BlockArena *arena);
API void Unwind(BlockArena *arena, U8 *pos);
///////////////////
// Virtual Arena
///////////////////
struct VirtualArena {
uint8_t *data;
size_t len;
size_t base_len; // to prevent self deleting the arena
@@ -46,50 +76,40 @@ struct Arena {
}
};
struct TempArena {
Arena *arena;
size_t len;
};
inline void SetLen(Arena *arena, size_t len) { arena->len = Clamp(len, arena->base_len, arena->len); }
inline void Pop(Arena *arena, size_t size) { SetLen(arena, arena->len - size); }
inline TempArena BeginTemp(Arena *arena) { return {arena, arena->len}; }
inline void EndTemp(TempArena temp) { SetLen(temp.arena, temp.len); }
inline void Clear(Arena *arena) { SetLen(arena, 0); }
inline void SetLen(VirtualArena *arena, size_t len) { arena->len = Clamp(len, arena->base_len, arena->len); }
inline void Pop(VirtualArena *arena, size_t size) { SetLen(arena, arena->len - size); }
inline void Clear(VirtualArena *arena) { SetLen(arena, 0); }
void *VReserve(size_t size);
bool VCommit(void *p, size_t size);
bool VRelease(void *p, size_t size);
bool VDecommit(void *p, size_t size);
API void *VReserve(size_t size);
API bool VCommit(void *p, size_t size);
API bool VRelease(void *p, size_t size);
API bool VDecommit(void *p, size_t size);
void InitArena(Arena *arena, size_t reserve = MiB(256));
Arena *AllocArena(size_t reserve = MiB(256));
Arena *AllocArena(Allocator allocator, size_t size);
void *PushSize(Arena *arena, size_t size);
void Release(Arena *arena);
void InitScratch();
TempArena GetScratchEx(Arena **conflicts, int conflict_count);
API void InitArena(VirtualArena *arena, size_t reserve = MiB(256));
API VirtualArena *AllocArena(size_t reserve = MiB(256));
API VirtualArena *AllocArena(Allocator allocator, size_t size);
API void *PushSize(VirtualArena *arena, size_t size);
API void Release(VirtualArena *arena);
inline TempArena GetScratch(Arena *c1 = NULL, Arena *c2 = NULL) {
int count = c1 ? 1 : 0;
count += c2 ? 1 : 0;
Arena *conflicts[] = {c1, c2};
return GetScratchEx(conflicts, count);
}
///////////////////////////////
// Scratch
struct Scratch {
TempArena checkpoint;
Scratch() { this->checkpoint = GetScratch(); }
Scratch(Arena *conflict) { this->checkpoint = GetScratch(conflict); }
Scratch(Arena *c1, Arena *c2) { this->checkpoint = GetScratch(c1, c2); }
Scratch(Allocator conflict) { this->checkpoint = GetScratch((Arena *)conflict.object); }
Scratch(Allocator c1, Allocator c2) { this->checkpoint = GetScratch((Arena *)c1.object, (Arena *)c2.object); }
~Scratch() { EndTemp(checkpoint); }
operator Arena *() { return checkpoint.arena; }
operator Allocator() { return *checkpoint.arena; }
BlockArena arena = {};
Scratch() {}
Scratch(BlockArena *conflict) {}
Scratch(BlockArena *c1, BlockArena *c2) {}
Scratch(Allocator conflict) {}
Scratch(Allocator c1, Allocator c2) {}
~Scratch() { Release(&arena); }
operator BlockArena *() { return &arena; }
operator Allocator() { return arena; }
private: // @Note: Disable copy constructors, cause its error prone
Scratch(Scratch &arena);
Scratch(Scratch &arena, Scratch &a2);
};
};
const int PAGE_SIZE = 4096;
const int DEFAULT_ALIGNMENT = sizeof(void *);

View File

@@ -214,7 +214,7 @@ void Reserve(Array<T> *arr, int64_t size) {
T *new_data = AllocArray(arr->allocator, T, size);
Assert(new_data);
memcpy(new_data, arr->data, arr->len * sizeof(T));
Dealloc(arr->allocator, &arr->data);
Dealloc(arr->allocator, arr->data);
arr->data = new_data;
arr->cap = size;
@@ -393,7 +393,10 @@ T Get(Array<T> &arr, int64_t i, T default_value = {}) {
template <class T>
void Dealloc(Array<T> *arr) {
if (arr->data) Dealloc(arr->allocator, &arr->data);
if (arr->data) {
Dealloc(arr->allocator, arr->data);
arr->data = NULL;
}
arr->len = arr->cap = 0;
}

View File

@@ -442,7 +442,7 @@ API String ReadFile(Allocator arena, String path) {
}
if (!success) {
Dealloc(arena, &result.data);
Dealloc(arena, result.data);
result = {};
}
@@ -566,7 +566,7 @@ API String GetExePath(Allocator allocator) {
}
API String GetExeDir(Allocator allocator) {
Scratch scratch((Arena *)allocator.object);
Scratch scratch(allocator);
String path = GetExePath(scratch);
path = ChopLastSlash(path);
path = Copy(allocator, path);
@@ -852,7 +852,7 @@ API String PollStdout(Allocator allocator, Process *process, bool force_read) {
bool read_error = ReadFile(p->child_stdout_read, buffer, (DWORD)buffer_size, &bytes_read, 0) == 0;
if (read_error) {
Win32ReportError("Failed to read the stdout of child process", "ReadFile");
Dealloc(allocator, &buffer);
Dealloc(allocator, buffer);
Win32CloseProcess(process);
return {};
}

View File

@@ -51,7 +51,9 @@ struct HashTable {
Assert(!(old_values == 0 && len != 0));
if (len == 0) {
if (old_values) Dealloc(allocator, &old_values);
if (old_values) {
Dealloc(allocator, old_values);
}
return;
}
@@ -62,7 +64,7 @@ struct HashTable {
insert(it->key, it->value);
}
}
Dealloc(allocator, &old_values);
Dealloc(allocator, old_values);
}
Entry *get_table_entry(uint64_t key) {
@@ -177,7 +179,8 @@ struct HashTable {
}
void dealloc() {
Dealloc(allocator, &values);
Dealloc(allocator, values);
values = NULL;
len = 0;
cap = 0;
}