Start refactor, restructure basic

This commit is contained in:
Krzosa Karol
2025-11-27 09:00:10 +01:00
parent 38b9b7df10
commit 781c2dc53c
30 changed files with 5716 additions and 3490 deletions

95
src/basic/basic_alloc.h Normal file
View File

@@ -0,0 +1,95 @@
#pragma once
const int AllocatorKind_Allocate = 1;
const int AllocatorKind_Deallocate = 2;
struct Allocator {
void *(*proc)(void *object, int kind, void *p, size_t size);
void *object;
};
#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;
}
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);
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 *);
struct Arena {
uint8_t *data;
size_t len;
size_t base_len; // to prevent self deleting the arena
size_t reserve;
size_t commit;
size_t align;
operator Allocator() {
void *ArenaAllocatorProc(void *object, int kind, void *p, size_t size);
return {ArenaAllocatorProc, this};
}
};
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); }
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);
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);
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);
}
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; }
private: // @Note: Disable copy constructors, cause its error prone
Scratch(Scratch &arena);
Scratch(Scratch &arena, Scratch &a2);
};