Compare commits

..
3 Commits
Author SHA1 Message Date
abdelrahman ae7a0806df Add ability to query allocator usage
Release / release (push) Successful in 17s
2026-11-22 22:34:39 +00:00
abdelrahman 1a00f82823 Reformat
Release / release (push) Successful in 10s
2026-11-21 20:16:52 +00:00
abdelrahman fc4f3f49d2 Updated WpUuid API
Release / release (push) Successful in 12s
2026-11-16 21:32:20 +00:00
10 changed files with 85 additions and 61 deletions

No files matched your search

+12
View File
@@ -7,6 +7,18 @@ 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
### Changed
- `WpUuid` API
## [3.4.0] - 2026-11-11 ## [3.4.0] - 2026-11-11
### Changed ### Changed
+1 -1
View File
@@ -1 +1 @@
3.4.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);
}
+2 -4
View File
@@ -51,8 +51,7 @@ u64 wpFileRead(void *dst_buf, WpFile *file, u64 byte_count) {
} }
i64 wpFileWrite(const void *src_buf, WpFile *file, u64 byte_count) { i64 wpFileWrite(const void *src_buf, WpFile *file, u64 byte_count) {
wpDebugAssert(src_buf != NULL && file != NULL, wpDebugAssert(src_buf != NULL && file != NULL, "`src_buf` and `file` should not be NULL.");
"`src_buf` and `file` should not be NULL.");
return fileWrite(src_buf, file, byte_count); return fileWrite(src_buf, file, byte_count);
} }
@@ -67,8 +66,7 @@ i64 wpFileWriteStr8(WpStr8RO *str, WpFile *file) {
} }
u64 wpFileReadArray(WpArray dst_buf, WpFile *file, u64 item_count) { u64 wpFileReadArray(WpArray dst_buf, WpFile *file, u64 item_count) {
wpDebugAssert(dst_buf != NULL && file != NULL, wpDebugAssert(dst_buf != NULL && file != NULL, "`dst_buf` and `file` should not be NULL.");
"`dst_buf` and `file` should not be NULL.");
i64 _file_length = wpFileGetLength(file); i64 _file_length = wpFileGetLength(file);
if (_file_length < 0) { if (_file_length < 0) {
+22 -27
View File
@@ -8,26 +8,9 @@
#include <inttypes.h> #include <inttypes.h>
#define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64) #define UUID_STR_FORMAT ("%.8" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.4" PRIx64 "-%.12" PRIx64)
#define UUID_HEX_FORMAT ("%.16" PRIx64 "%.16" PRIx64)
typedef struct UUID4 UUID4; WpUuid wpUuidGenUuid4(void) {
struct UUID4 {
u64 high;
u64 low;
};
wp_intern UUID4 genUuid4(void);
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid);
WpUuid *wpUuidInitUuid4(WpUuid *uuid) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
UUID4 uuid4 = genUuid4();
uuid4ToUuid(&uuid4, uuid);
return uuid;
}
wp_intern UUID4 genUuid4(void) {
wp_persist WpXor256State state = {0}; wp_persist WpXor256State state = {0};
wp_persist b8 initialised = false; wp_persist b8 initialised = false;
@@ -36,7 +19,7 @@ wp_intern UUID4 genUuid4(void) {
state = wpPrngXorshiftInit(); state = wpPrngXorshiftInit();
} }
UUID4 uuid = (UUID4){ WpUuid uuid = {
.high = wpPrngXorshift256(&state), .high = wpPrngXorshift256(&state),
.low = wpPrngXorshift256(&state), .low = wpPrngXorshift256(&state),
}; };
@@ -47,12 +30,24 @@ wp_intern UUID4 genUuid4(void) {
return uuid; return uuid;
} }
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid) { void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf) {
u64 group1 = uuid4->high >> 32; wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
u64 group2 = (uuid4->high << 32) >> 48; wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_STR_BUF_LENGTH,
u64 group3 = (uuid4->high << 48) >> 48; "String buffer is NULL or doesn't have sufficient capacity");
u64 group4 = uuid4->low >> 48;
u64 group5 = (uuid4->low << 16) >> 16;
wpStr8Format(&(uuid->uuid), UUID_STR_FORMAT, group1, group2, group3, group4, group5); u64 group1 = uuid->high >> 32;
u64 group2 = (uuid->high << 32) >> 48;
u64 group3 = (uuid->high << 48) >> 48;
u64 group4 = uuid->low >> 48;
u64 group5 = (uuid->low << 16) >> 16;
wpStr8Format(buf, UUID_STR_FORMAT, group1, group2, group3, group4, group5);
}
void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_HEX_BUF_LENGTH,
"String buffer is NULL or doesn't have sufficient capacity");
wpStr8Format(buf, UUID_HEX_FORMAT, uuid->high, uuid->low);
} }
+9 -29
View File
@@ -11,37 +11,17 @@
BEGIN_C_LINKAGE BEGIN_C_LINKAGE
#endif // !WP_PLATFORM_CPP #endif // !WP_PLATFORM_CPP
#define WP_UUID_BUF_LENGTH 48 #define WP_UUID_STR_BUF_LENGTH 36
#define WP_UUID_SPEC WP_STR8_SPEC #define WP_UUID_HEX_BUF_LENGTH 32
#define wpUuidVarg(WpUuid) wpStr8Varg((WpUuid).uuid)
typedef struct WpUuid WpUuid; typedef struct WpUuid {
struct WpUuid { u64 high;
WpStr8 uuid; u64 low;
}; } WpUuid;
// TODO (Abdelrahman): Update UUID implementation to work properly with C++ and tests for validation WpUuid wpUuidGenUuid4(void);
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf);
#ifdef WP_PLATFORM_CPP void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf);
#define wpUuidGenUuid4() ([&](){ \
wp_persist WpUuid uuid = wpUuidCreate(); \
return *(wpUuidInitUuid4(&uuid)); \
}())
#else
#define wpUuidGenUuid4() *(wpUuidInitUuid4(&wpUuidCreate()))
#endif
/* Low level UUID API */
#ifdef WP_PLATFORM_CPP
#define wpUuidCreate() ([&](){ return WpUuid{wpStr8Buf(WP_UUID_BUF_LENGTH)}; }())
#else
#define wpUuidCreate() ((WpUuid){.uuid = wpStr8Buf(WP_UUID_BUF_LENGTH)})
#endif
// Just returns the same pointer that was passed in with the UUID initialised.
// Fails when passed a NULL pointer.
WpUuid *wpUuidInitUuid4(WpUuid *uuid);
#ifdef WP_PLATFORM_CPP #ifdef WP_PLATFORM_CPP
END_C_LINKAGE END_C_LINKAGE