Allocator memes

This commit is contained in:
krzosa
2025-12-30 10:09:46 +01:00
parent 187bbcc5fc
commit a57ebb49be
8 changed files with 122 additions and 50 deletions
+30 -10
View File
@@ -51,6 +51,7 @@ struct BlockArena {
U8 *end;
BlockArenaNode *blocks;
Allocator allocator;
bool when_unwinding_make_sure_to_keep_last_block_alive;
operator Allocator() { return {BlockArenaAllocatorProc, this}; }
};
@@ -95,17 +96,36 @@ API void Release(VirtualArena *arena);
///////////////////////////////
// Scratch
extern thread_local VirtualArena ScratchArenas[4];
API VirtualArena *GetScratchEx(VirtualArena **conflicts, int conflict_count);
struct Scratch {
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; }
VirtualArena *arena;
U64 p;
Scratch() {arena = &ScratchArenas[0]; p = arena->len;}
Scratch(VirtualArena *conflict) {
VirtualArena *conf[] = {conflict};
arena = GetScratchEx(conf, Lengthof(conf));
p = arena->len;
}
Scratch(VirtualArena *c1, VirtualArena *c2) {
VirtualArena *conf[] = {c1, c2};
arena = GetScratchEx(conf, Lengthof(conf));
p = arena->len;
}
Scratch(Allocator conflict) {
VirtualArena *conf[] = {(VirtualArena *)conflict.object};
arena = GetScratchEx(conf, Lengthof(conf));
p = arena->len;
}
Scratch(Allocator c1, Allocator c2) {
VirtualArena *conf[] = {(VirtualArena *)c1.object, (VirtualArena *)c2.object};
arena = GetScratchEx(conf, Lengthof(conf));
p = arena->len;
}
~Scratch() { SetLen(arena, p); }
operator VirtualArena *() { return arena; }
operator Allocator() { return *arena; }
private: // @Note: Disable copy constructors, cause its error prone
Scratch(Scratch &arena);
@@ -113,4 +133,4 @@ struct Scratch {
};
const int PAGE_SIZE = 4096;
const int DEFAULT_ALIGNMENT = sizeof(void *);
const int DEFAULT_ALIGNMENT = sizeof(void *);