Add ability to query allocator usage
Release / release (push) Successful in 17s

This commit is contained in:
abdelrahman committed 2026-11-22 22:34:39 +00:00
1 parent 1a00f82823
commit ae7a0806df
7 files changed
+46 -1

No files matched your search

+6
View File
@@ -7,6 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [3.6.0] - 2026-11-22
### Added
- Functionality to query memory allocator usage
## [3.5.0] - 2026-11-16 ## [3.5.0] - 2026-11-16
### Changed ### Changed
+1 -1
View File
@@ -1 +1 @@
3.5.0 3.6.0
+12
View File
@@ -76,6 +76,16 @@ void wpMemAllocatorTempEnd(const WpAllocator *allocator) {
allocator->temp_end(allocator->obj); allocator->temp_end(allocator->obj);
} }
WpAllocatorUsage wpMemAllocatorQueryUsage(const WpAllocator *allocator) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_QUERY_USAGE)) {
return (WpAllocatorUsage){0};
}
return allocator->query_usage(allocator->obj);
}
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) { b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL"); wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
@@ -94,6 +104,8 @@ b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
return allocator->temp_begin != NULL; return allocator->temp_begin != NULL;
case WP_MEM_OP_TEMP_END: case WP_MEM_OP_TEMP_END:
return allocator->temp_end != NULL; return allocator->temp_end != NULL;
case WP_MEM_OP_QUERY_USAGE:
return allocator->query_usage != NULL;
case COUNT_MEM_OPS: case COUNT_MEM_OPS:
default: default:
break; break;
+9
View File
@@ -19,10 +19,16 @@ typedef enum WpMemOp {
WP_MEM_OP_FREE, WP_MEM_OP_FREE,
WP_MEM_OP_TEMP_BEGIN, WP_MEM_OP_TEMP_BEGIN,
WP_MEM_OP_TEMP_END, WP_MEM_OP_TEMP_END,
WP_MEM_OP_QUERY_USAGE,
COUNT_MEM_OPS COUNT_MEM_OPS
} WpMemOp; } WpMemOp;
typedef struct WpAllocatorUsage {
u64 capacity;
u64 used;
} WpAllocatorUsage;
typedef void *(WpMemAllocFunc)(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 *(WpMemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj); typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
@@ -30,6 +36,7 @@ typedef void *(WpMemReallocAlignedFunc)(void *ptr, u64 old_size, u64 new_size, u
typedef void (WpMemFreeFunc)(void **ptr, u64 size, void *alloc_obj); typedef void (WpMemFreeFunc)(void **ptr, u64 size, void *alloc_obj);
typedef void (WpMemTempBeginFunc)(void *alloc_obj); typedef void (WpMemTempBeginFunc)(void *alloc_obj);
typedef void (WpMemTempEndFunc)(void *alloc_obj); typedef void (WpMemTempEndFunc)(void *alloc_obj);
typedef WpAllocatorUsage (WpMemQueryUsageFunc)(void *alloc_obj);
typedef struct WpAllocator WpAllocator; typedef struct WpAllocator WpAllocator;
struct WpAllocator { struct WpAllocator {
@@ -41,6 +48,7 @@ struct WpAllocator {
WpMemFreeFunc *free; WpMemFreeFunc *free;
WpMemTempBeginFunc *temp_begin; WpMemTempBeginFunc *temp_begin;
WpMemTempEndFunc *temp_end; WpMemTempEndFunc *temp_end;
WpMemQueryUsageFunc *query_usage;
}; };
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
@@ -60,6 +68,7 @@ void *wpMemAllocatorReallocAligned(const WpAllocator *allocator, void *ptr, u64
void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size); void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size);
void wpMemAllocatorTempBegin(const WpAllocator *allocator); void wpMemAllocatorTempBegin(const WpAllocator *allocator);
void wpMemAllocatorTempEnd(const WpAllocator *allocator); void wpMemAllocatorTempEnd(const WpAllocator *allocator);
WpAllocatorUsage wpMemAllocatorQueryUsage(const WpAllocator *allocator);
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op); b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
+9
View File
@@ -172,6 +172,15 @@ void wpMemArenaTempEnd(WpArena *arena) {
arena->prev_offset = NULL; arena->prev_offset = NULL;
} }
WpAllocatorUsage wpMemArenaQueryUsage(WpArena *arena) {
wpDebugAssert(arena != NULL, "`arena` should not be NULL");
return (WpAllocatorUsage){
.capacity = arena->capacity,
.used = arena->offset - arena->buf,
};
}
void wpMemArenaClear(WpArena *arena) { void wpMemArenaClear(WpArena *arena) {
wpDebugAssert(arena != NULL, "`arena` should not be NULL"); wpDebugAssert(arena != NULL, "`arena` should not be NULL");
+2
View File
@@ -6,6 +6,7 @@
#include "../../mem/mem_os.h" #include "../../mem/mem_os.h"
#include "../../../common/aliases/aliases.h" #include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h" #include "../../../common/platform/platform.h"
#include "../../../base/mem/allocator/mem_allocator.h"
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
@@ -35,6 +36,7 @@ void *wpMemArenaRealloc(WpArena *arena, void *ptr, u64 old_size, u64 new_size);
void *wpMemArenaReallocAligned(WpArena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment); void *wpMemArenaReallocAligned(WpArena *arena, void *ptr, u64 old_size, u64 new_size, u64 alignment);
void wpMemArenaTempBegin(WpArena *arena); void wpMemArenaTempBegin(WpArena *arena);
void wpMemArenaTempEnd(WpArena *arena); void wpMemArenaTempEnd(WpArena *arena);
WpAllocatorUsage wpMemArenaQueryUsage(WpArena *arena);
void wpMemArenaClear(WpArena *arena); void wpMemArenaClear(WpArena *arena);
void wpMemArenaDestroy(WpArena **arena); void wpMemArenaDestroy(WpArena **arena);
@@ -13,6 +13,7 @@ wp_intern void *memArenaReallocAligned(void *ptr, u64 old_size, u64 new_size, u6
void *alloc_obj); void *alloc_obj);
wp_intern void memArenaTempBegin(void *alloc_obj); wp_intern void memArenaTempBegin(void *alloc_obj);
wp_intern void memArenaTempEnd(void *alloc_obj); wp_intern void memArenaTempEnd(void *alloc_obj);
wp_intern WpAllocatorUsage memArenaQueryUsage(void *alloc_obj);
WpAllocator wpMemArenaAllocatorInitWithBuffer(u8 *buffer, u64 buffer_size) { WpAllocator wpMemArenaAllocatorInitWithBuffer(u8 *buffer, u64 buffer_size) {
WpAllocator allocator = {0}; WpAllocator allocator = {0};
@@ -56,6 +57,7 @@ wp_intern void initialiseArenaAllocator(WpAllocator *allocator) {
allocator->realloc_aligned = memArenaReallocAligned; allocator->realloc_aligned = memArenaReallocAligned;
allocator->temp_begin = memArenaTempBegin; allocator->temp_begin = memArenaTempBegin;
allocator->temp_end = memArenaTempEnd; allocator->temp_end = memArenaTempEnd;
allocator->query_usage = memArenaQueryUsage;
} }
wp_intern void *memArenaAlloc(u64 size, void *alloc_obj) { wp_intern void *memArenaAlloc(u64 size, void *alloc_obj) {
@@ -88,3 +90,8 @@ wp_intern void memArenaTempEnd(void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj; WpArena *arena = (WpArena *)alloc_obj;
wpMemArenaTempEnd(arena); wpMemArenaTempEnd(arena);
} }
wp_intern WpAllocatorUsage memArenaQueryUsage(void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj;
return wpMemArenaQueryUsage(arena);
}