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 122 additions and 98 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]
## [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
### 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);
}
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) {
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
@@ -94,6 +104,8 @@ b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
return allocator->temp_begin != NULL;
case WP_MEM_OP_TEMP_END:
return allocator->temp_end != NULL;
case WP_MEM_OP_QUERY_USAGE:
return allocator->query_usage != NULL;
case COUNT_MEM_OPS:
default:
break;
+25 -16
View File
@@ -19,17 +19,24 @@ typedef enum WpMemOp {
WP_MEM_OP_FREE,
WP_MEM_OP_TEMP_BEGIN,
WP_MEM_OP_TEMP_END,
WP_MEM_OP_QUERY_USAGE,
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 (WpMemTempBeginFunc)(void *alloc_obj);
typedef void (WpMemTempEndFunc)(void *alloc_obj);
typedef struct WpAllocatorUsage {
u64 capacity;
u64 used;
} WpAllocatorUsage;
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 WpAllocatorUsage (WpMemQueryUsageFunc)(void *alloc_obj);
typedef struct WpAllocator WpAllocator;
struct WpAllocator {
@@ -41,6 +48,7 @@ struct WpAllocator {
WpMemFreeFunc *free;
WpMemTempBeginFunc *temp_begin;
WpMemTempEndFunc *temp_end;
WpMemQueryUsageFunc *query_usage;
};
#ifdef WP_PLATFORM_CPP
@@ -52,15 +60,16 @@ struct WpAllocator {
#define wpMemAllocatorInvalid(ALLOCATOR) (memcmp(ALLOCATOR, &((WpAllocator){0}), sizeof(WpAllocator)) == 0)
#endif // !WP_PLATFORM_CPP
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);
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);
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);
void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size);
void wpMemAllocatorTempBegin(const WpAllocator *allocator);
void wpMemAllocatorTempEnd(const WpAllocator *allocator);
WpAllocatorUsage wpMemAllocatorQueryUsage(const WpAllocator *allocator);
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
+9
View File
@@ -172,6 +172,15 @@ void wpMemArenaTempEnd(WpArena *arena) {
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) {
wpDebugAssert(arena != NULL, "`arena` should not be NULL");
+12 -10
View File
@@ -6,6 +6,7 @@
#include "../../mem/mem_os.h"
#include "../../../common/aliases/aliases.h"
#include "../../../common/platform/platform.h"
#include "../../../base/mem/allocator/mem_allocator.h"
#ifdef WP_PLATFORM_CPP
BEGIN_C_LINKAGE
@@ -27,16 +28,17 @@ typedef struct WpArena WpArena;
* control over how the WpArena is initialised. Wrapper macros are provided for
* easier use.
*/
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size);
void *wpMemArenaAlloc(WpArena *arena, u64 size);
void *wpMemArenaAllocAligned(WpArena *arena, u64 size, u64 alignment);
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 wpMemArenaTempBegin(WpArena *arena);
void wpMemArenaTempEnd(WpArena *arena);
void wpMemArenaClear(WpArena *arena);
void wpMemArenaDestroy(WpArena **arena);
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size);
void *wpMemArenaAlloc(WpArena *arena, u64 size);
void *wpMemArenaAllocAligned(WpArena *arena, u64 size, u64 alignment);
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 wpMemArenaTempBegin(WpArena *arena);
void wpMemArenaTempEnd(WpArena *arena);
WpAllocatorUsage wpMemArenaQueryUsage(WpArena *arena);
void wpMemArenaClear(WpArena *arena);
void wpMemArenaDestroy(WpArena **arena);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE
+16 -9
View File
@@ -5,14 +5,15 @@
#include "../../../common/aliases/aliases.h"
#include "../../../common/assert/assert.h"
wp_intern void initialiseArenaAllocator(WpAllocator *allocator);
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);
wp_intern void memArenaTempBegin(void *alloc_obj);
wp_intern void memArenaTempEnd(void *alloc_obj);
wp_intern void initialiseArenaAllocator(WpAllocator *allocator);
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);
wp_intern void memArenaTempBegin(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 allocator = {0};
@@ -56,6 +57,7 @@ wp_intern void initialiseArenaAllocator(WpAllocator *allocator) {
allocator->realloc_aligned = memArenaReallocAligned;
allocator->temp_begin = memArenaTempBegin;
allocator->temp_end = memArenaTempEnd;
allocator->query_usage = memArenaQueryUsage;
}
wp_intern void *memArenaAlloc(u64 size, void *alloc_obj) {
@@ -74,7 +76,7 @@ wp_intern void *memArenaRealloc(void *ptr, u64 old_size, u64 new_size, void *all
}
wp_intern void *memArenaReallocAligned(void *ptr, u64 old_size, u64 new_size, u64 alignment,
void *alloc_obj) {
void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj;
return wpMemArenaReallocAligned(arena, ptr, old_size, new_size, alignment);
}
@@ -88,3 +90,8 @@ wp_intern void memArenaTempEnd(void *alloc_obj) {
WpArena *arena = (WpArena *)alloc_obj;
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) {
wpDebugAssert(src_buf != NULL && file != NULL,
"`src_buf` and `file` should not be NULL.");
wpDebugAssert(src_buf != NULL && file != NULL, "`src_buf` and `file` should not be NULL.");
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) {
wpDebugAssert(dst_buf != NULL && file != NULL,
"`dst_buf` and `file` should not be NULL.");
wpDebugAssert(dst_buf != NULL && file != NULL, "`dst_buf` and `file` should not be NULL.");
i64 _file_length = wpFileGetLength(file);
if (_file_length < 0) {
+24 -29
View File
@@ -8,36 +8,19 @@
#include <inttypes.h>
#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;
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) {
WpUuid wpUuidGenUuid4(void) {
wp_persist WpXor256State state = {0};
wp_persist b8 initialised = false;
wp_persist b8 initialised = false;
if (!initialised) {
initialised = true;
state = wpPrngXorshiftInit();
}
UUID4 uuid = (UUID4){
.high = wpPrngXorshift256(&state),
WpUuid uuid = {
.high = wpPrngXorshift256(&state),
.low = wpPrngXorshift256(&state),
};
@@ -47,12 +30,24 @@ wp_intern UUID4 genUuid4(void) {
return uuid;
}
wp_intern void uuid4ToUuid(const UUID4* uuid4, WpUuid *uuid) {
u64 group1 = uuid4->high >> 32;
u64 group2 = (uuid4->high << 32) >> 48;
u64 group3 = (uuid4->high << 48) >> 48;
u64 group4 = uuid4->low >> 48;
u64 group5 = (uuid4->low << 16) >> 16;
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf) {
wpDebugAssert(uuid != NULL, "`uuid` should not be NULL");
wpRuntimeAssert(buf != NULL && buf->capacity >= WP_UUID_STR_BUF_LENGTH,
"String buffer is NULL or doesn't have sufficient capacity");
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
#endif // !WP_PLATFORM_CPP
#define WP_UUID_BUF_LENGTH 48
#define WP_UUID_SPEC WP_STR8_SPEC
#define wpUuidVarg(WpUuid) wpStr8Varg((WpUuid).uuid)
#define WP_UUID_STR_BUF_LENGTH 36
#define WP_UUID_HEX_BUF_LENGTH 32
typedef struct WpUuid WpUuid;
struct WpUuid {
WpStr8 uuid;
};
typedef struct WpUuid {
u64 high;
u64 low;
} WpUuid;
// TODO (Abdelrahman): Update UUID implementation to work properly with C++ and tests for validation
#ifdef WP_PLATFORM_CPP
#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);
WpUuid wpUuidGenUuid4(void);
void wpUuidToStr8(const WpUuid *uuid, WpStr8 *buf);
void wpUuidToHex(const WpUuid *uuid, WpStr8 *buf);
#ifdef WP_PLATFORM_CPP
END_C_LINKAGE