Add allocator interface

This commit is contained in:
2026-06-21 23:41:27 +02:00
parent 1010541db7
commit e52051b859
7 changed files with 218 additions and 82 deletions
+40
View File
@@ -344,6 +344,46 @@ static INLINE U64 SaturatingMult64(U64 a, U64 b)
return SaturatingMult(a, b, U64, U64_Max);
}
// Allocators
typedef enum AllocOperation AllocOperation;
enum AllocOperation {
Alloc_Op_Aligned = 1,
Alloc_Op_Free,
Alloc_Op_Free_All,
};
typedef struct Allocator Allocator;
typedef void *(*AllocOpFunc)(Allocator *allocator, AllocOperation operation, void *memory, U32 size, U32 align);
struct Allocator {
AllocOpFunc op_func_ptr;
void *data;
};
static INLINE void *allocator_alloc_aligned(Allocator *allocator, U32 size, U32 align)
{
return allocator->op_func_ptr(allocator, Alloc_Op_Aligned, NULL, size, align);
}
static INLINE void allocator_free(Allocator *allocator, void *memory)
{
allocator->op_func_ptr(allocator, Alloc_Op_Free, memory, 0, 0);
}
static INLINE void allocator_free_all(Allocator *allocator)
{
allocator->op_func_ptr(allocator, Alloc_Op_Free_All, NULL, 0, 0);
}
#define allocator_push_type(allocator, type_)\
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, sizeof(type_), _Alignof(type_))
#define allocator_push_array(allocator, type_, nr_elements)\
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, sizeof(type_) * (nr_elements), _Alignof(type_) )
#define allocator_push(allocator, size, align)\
(allocator)->op_func_ptr((allocator), Alloc_Op_Aligned, NULL, size, align)
// address-sanitizer helpers (-fsanitize=address)
#ifdef USE_ASAN
#include <sanitizer/asan_interface.h>