Update allocator interface
Release / release (push) Successful in 4s

This commit is contained in:
2026-09-30 22:24:46 +01:00
parent cd93b2e479
commit df03b48319
8 changed files with 75 additions and 32 deletions
+24
View File
@@ -56,6 +56,26 @@ void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size) {
allocator->free(ptr, size, allocator->obj);
}
void wpMemAllocatorTempBegin(const WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_TEMP_BEGIN)) {
return;
}
allocator->temp_begin(allocator->obj);
}
void wpMemAllocatorTempEnd(const WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_TEMP_END)) {
return;
}
allocator->temp_end(allocator->obj);
}
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
@@ -70,6 +90,10 @@ b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
return allocator->realloc_aligned != NULL;
case WP_MEM_OP_FREE:
return allocator->free != NULL;
case WP_MEM_OP_TEMP_BEGIN:
return allocator->temp_begin != NULL;
case WP_MEM_OP_TEMP_END:
return allocator->temp_end != NULL;
default:
break;
}
+19 -11
View File
@@ -17,24 +17,30 @@ typedef enum WpMemOp {
WP_MEM_OP_REALLOC,
WP_MEM_OP_REALLOC_ALIGNED,
WP_MEM_OP_FREE,
WP_MEM_OP_TEMP_BEGIN,
WP_MEM_OP_TEMP_END,
COUNT_MEM_OPS
} WpMemOp;
typedef void *(WpMemAllocFunc)(u64 size, void *alloc_obj);
typedef void *(WpMemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
typedef void *(WpMemReallocAlignedFunc)(void *ptr, u64 old_size, u64 new_size, u64 alignment, void *alloc_obj);
typedef void (WpMemFreeFunc)(void **ptr, u64 size, void *alloc_obj);
typedef void *(WpMemAllocFunc)(u64 size, void *alloc_obj);
typedef void *(WpMemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
typedef void *(WpMemReallocAlignedFunc)(void *ptr, u64 old_size, u64 new_size, u64 alignment, void *alloc_obj);
typedef void (WpMemFreeFunc)(void **ptr, u64 size, void *alloc_obj);
typedef void (WpMemTempBeginFunc)(void *alloc_obj);
typedef void (WpMemTempEndFunc)(void *alloc_obj);
typedef struct WpAllocator WpAllocator;
struct WpAllocator {
void *obj;
WpMemAllocFunc *alloc;
WpMemAllocAlignedFunc *alloc_aligned;
WpMemReallocFunc *realloc;
void *obj;
WpMemAllocFunc *alloc;
WpMemAllocAlignedFunc *alloc_aligned;
WpMemReallocFunc *realloc;
WpMemReallocAlignedFunc *realloc_aligned;
WpMemFreeFunc *free;
WpMemFreeFunc *free;
WpMemTempBeginFunc *temp_begin;
WpMemTempEndFunc *temp_end;
};
#ifdef WP_PLATFORM_CPP
@@ -50,8 +56,10 @@ void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size);
void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment);
void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size);
void *wpMemAllocatorReallocAligned(const WpAllocator *allocator, void *ptr, u64 old_size,
u64 new_size, u64 alignment);
u64 new_size, u64 alignment);
void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size);
void wpMemAllocatorTempBegin(const WpAllocator *allocator);
void wpMemAllocatorTempEnd(const WpAllocator *allocator);
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
#ifdef WP_PLATFORM_CPP
+15 -12
View File
@@ -2,7 +2,6 @@
#include "mem_arena_allocator.h"
#include "mem_arena.h"
#include "../../mem/mem_os.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
@@ -11,7 +10,9 @@ wp_intern void *memArenaAlloc(u64 size, void *alloc_obj);
wp_intern void *memArenaAllocAligned(u64 size, u64 alignment, void *alloc_obj);
wp_intern void *memArenaRealloc(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
wp_intern void *memArenaReallocAligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
void *alloc_obj);
void *alloc_obj);
wp_intern void memArenaTempBegin(void *alloc_obj);
wp_intern void memArenaTempEnd(void *alloc_obj);
WpAllocator wpMemArenaAllocatorInitWithBuffer(u8 *buffer, u64 buffer_size) {
WpAllocator allocator = {0};
@@ -37,16 +38,6 @@ WpAllocator wpMemArenaAllocatorInitCustom(u64 base_capacity, WpMemAllocFlags fla
return allocator;
}
void wpMemArenaAllocatorTempBegin(const WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
wpMemArenaTempBegin((WpArena *)(allocator->obj));
}
void wpMemArenaAllocatorTempEnd(const WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
wpMemArenaTempEnd((WpArena *)(allocator->obj));
}
void wpMemArenaAllocatorClear(WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
wpMemArenaClear((WpArena *)(allocator->obj));
@@ -63,6 +54,8 @@ wp_intern void initialiseArenaAllocator(WpAllocator *allocator) {
allocator->alloc_aligned = memArenaAllocAligned;
allocator->realloc = memArenaRealloc;
allocator->realloc_aligned = memArenaReallocAligned;
allocator->temp_begin = memArenaTempBegin;
allocator->temp_end = memArenaTempEnd;
}
wp_intern void *memArenaAlloc(u64 size, void *alloc_obj) {
@@ -85,3 +78,13 @@ wp_intern void *memArenaReallocAligned(void *ptr, u64 old_size, u64 new_size, u6
WpArena *arena = (WpArena *)alloc_obj;
return wpMemArenaReallocAligned(arena, ptr, old_size, new_size, alignment);
}
wp_intern void memArenaTempBegin(void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj;
wpMemArenaTempBegin(arena);
}
wp_intern void memArenaTempEnd(void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj;
wpMemArenaTempEnd(arena);
}
@@ -34,8 +34,6 @@ BEGIN_C_LINKAGE
*/
WpAllocator wpMemArenaAllocatorInitCustom(u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
WpAllocator wpMemArenaAllocatorInitWithBuffer(u8 *buffer, u64 buffer_size);
void wpMemArenaAllocatorTempBegin(const WpAllocator *allocator);
void wpMemArenaAllocatorTempEnd(const WpAllocator *allocator);
void wpMemArenaAllocatorClear(WpAllocator *allocator);
void wpMemArenaAllocatorDestroy(WpAllocator *allocator);