Gears-C/src/allocators.h
2025-04-14 16:37:47 +10:00

86 lines
1.8 KiB
C

#pragma once
// ::Allocator::Util::Header::
usize CalcPaddingWithHeader(uintptr ptr, uintptr alignment, usize header_size);
// ::Allocator::Arena::Header::
#define ARENA_HEADER_SIZE 64
typedef struct Arena
{
u8 *buffer;
usize length;
usize pos;
u32 init_line_no;
} Arena;
typedef struct TempArena
{
Arena *arena;
u64 pos;
} TempArena;
static Arena *CreateArena(rawptr buffer, usize length);
static rawptr ArenaAllocAlign(Arena *arena, usize size, usize align);
static rawptr ArenaAlloc(Arena *arena, usize size);
static void ArenaFree(Arena *arena);
static void ArenaFreeZeroed(Arena *arena);
static void DeallocArena(Arena *arena);
static Arena *CreateArenaDebug(rawptr buffer, usize length, u32 init_line_no);
// ::Allocator::GlobalAlloc::Header::
typedef struct FreeListBuffer
{
rawptr buf;
u32 size;
u32 free_size;
} FreeListBuffer;
typedef struct Allocator
{
RBNode *head;
RBNode *nil;
FreeListBuffer *buffers;
usize grow_size;
u8 buf_len;
} Allocator;
static void InitAllocator(usize init_size, usize grow_size);
static void DeinitAlloc();
static void AllocGrow();
static rawptr Alloc(usize size);
static void Free(rawptr ptr);
// ::Allocator::FreeList::Header::
typedef struct FLAllocHeader
{
usize block_size;
usize padding;
} FLAllocHeader;
typedef struct FLNode FLNode;
typedef struct FLNode
{
FLNode *next;
usize size;
} FLNode;
typedef struct FLAlloc
{
rawptr data;
FLNode *head;
FLNode *nil;
usize size;
usize used;
} FLAlloc;
static void FLAllocInit(FLAlloc *alloc, usize size);
static void FLAllocFreeAll(FLAlloc *alloc);
static FLNode *FreeListSearch(FLAlloc *alloc, usize size, u32 alignment, u32 *out_padding, FLNode **prev_node);
static rawptr FreeListAlloc(FLAlloc *alloc, usize size, u32 alignment);