Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d6755d4d4 | ||
|
|
ae7a0806df | ||
|
|
1a00f82823 | ||
|
|
fc4f3f49d2 |
No files matched your search
@@ -7,6 +7,24 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
## [3.6.1] - 2026-11-23
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
|
||||||
|
- Arena initialisation checks
|
||||||
|
|
||||||
|
## [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
|
||||||
|
|||||||
@@ -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;
|
||||||
|
|||||||
@@ -19,20 +19,26 @@ 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 void *(WpMemAllocFunc)(u64 size, void *alloc_obj);
|
typedef struct WpAllocatorUsage {
|
||||||
typedef void *(WpMemAllocAlignedFunc)(u64 size, u64 alignment, void *alloc_obj);
|
u64 capacity;
|
||||||
typedef void *(WpMemReallocFunc)(void *ptr, u64 old_size, u64 new_size, void *alloc_obj);
|
u64 used;
|
||||||
typedef void *(WpMemReallocAlignedFunc)(void *ptr, u64 old_size, u64 new_size, u64 alignment, void *alloc_obj);
|
} WpAllocatorUsage;
|
||||||
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;
|
typedef void *(WpMemAllocFunc)(u64 size, void *alloc_obj);
|
||||||
struct WpAllocator {
|
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 {
|
||||||
void *obj;
|
void *obj;
|
||||||
WpMemAllocFunc *alloc;
|
WpMemAllocFunc *alloc;
|
||||||
WpMemAllocAlignedFunc *alloc_aligned;
|
WpMemAllocAlignedFunc *alloc_aligned;
|
||||||
@@ -41,7 +47,8 @@ struct WpAllocator {
|
|||||||
WpMemFreeFunc *free;
|
WpMemFreeFunc *free;
|
||||||
WpMemTempBeginFunc *temp_begin;
|
WpMemTempBeginFunc *temp_begin;
|
||||||
WpMemTempEndFunc *temp_end;
|
WpMemTempEndFunc *temp_end;
|
||||||
};
|
WpMemQueryUsageFunc *query_usage;
|
||||||
|
} WpAllocator;
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
#define wpMemAllocatorInvalid(ALLOCATOR) ([&]() { \
|
#define wpMemAllocatorInvalid(ALLOCATOR) ([&]() { \
|
||||||
@@ -52,15 +59,16 @@ struct WpAllocator {
|
|||||||
#define wpMemAllocatorInvalid(ALLOCATOR) (memcmp(ALLOCATOR, &((WpAllocator){0}), sizeof(WpAllocator)) == 0)
|
#define wpMemAllocatorInvalid(ALLOCATOR) (memcmp(ALLOCATOR, &((WpAllocator){0}), sizeof(WpAllocator)) == 0)
|
||||||
#endif // !WP_PLATFORM_CPP
|
#endif // !WP_PLATFORM_CPP
|
||||||
|
|
||||||
void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size);
|
void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size);
|
||||||
void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment);
|
void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment);
|
||||||
void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size);
|
void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_size, u64 new_size);
|
||||||
void *wpMemAllocatorReallocAligned(const WpAllocator *allocator, void *ptr, u64 old_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 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);
|
||||||
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
|
WpAllocatorUsage wpMemAllocatorQueryUsage(const WpAllocator *allocator);
|
||||||
|
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -33,11 +33,11 @@ struct WpArena {
|
|||||||
};
|
};
|
||||||
|
|
||||||
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size) {
|
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size) {
|
||||||
if (!arena || *arena || buffer_size < sizeof(WpArena)) {
|
if (arena == NULL || *arena != NULL || buffer_size < sizeof(WpArena)) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
*arena = (WpArena *)buffer;
|
*arena = (WpArena *)buffer;
|
||||||
WpArena *arena_ptr = *arena;
|
WpArena *arena_ptr = *arena;
|
||||||
|
|
||||||
arena_ptr->buf = (u8 *)(arena_ptr + 1);
|
arena_ptr->buf = (u8 *)(arena_ptr + 1);
|
||||||
@@ -51,15 +51,15 @@ b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer) {
|
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer) {
|
||||||
if (!arena || *arena || base_capacity == 0) {
|
if (arena == NULL || *arena != NULL || base_capacity == 0) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 size = sizeof(WpArena) + (base_capacity >= ARENA_MINIMUM_CAPACITY ? base_capacity : ARENA_MINIMUM_CAPACITY);
|
u64 size = sizeof(WpArena) + (base_capacity >= ARENA_MINIMUM_CAPACITY ? base_capacity : ARENA_MINIMUM_CAPACITY);
|
||||||
u64 alloc_size = wpMiscUtilsU64RoundUpPow2(size);
|
u64 alloc_size = wpMiscUtilsU64RoundUpPow2(size);
|
||||||
u8 *allocated = (u8 *)wpOsMemAlloc(NULL, alloc_size, WP_MEM_ACCESS_READ_WRITE, flags,
|
u8 *allocated = (u8 *)wpOsMemAlloc(NULL, alloc_size, WP_MEM_ACCESS_READ_WRITE, flags,
|
||||||
zero_buffer ? WP_MEM_INIT_INITIALISED : WP_MEM_INIT_UNINITIALISED);
|
zero_buffer ? WP_MEM_INIT_INITIALISED : WP_MEM_INIT_UNINITIALISED);
|
||||||
if (!allocated) {
|
if (allocated == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -77,7 +77,7 @@ b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocF
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
WpArena *arena_ptr = *arena;
|
WpArena *arena_ptr = *arena;
|
||||||
arena_ptr->type = ARENA_STORAGE_TYPE_ALLOCATED;
|
arena_ptr->type = ARENA_STORAGE_TYPE_ALLOCATED;
|
||||||
arena_ptr->committed = committed;
|
arena_ptr->committed = committed;
|
||||||
|
|
||||||
@@ -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");
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
||||||
@@ -27,16 +28,17 @@ typedef struct WpArena WpArena;
|
|||||||
* control over how the WpArena is initialised. Wrapper macros are provided for
|
* control over how the WpArena is initialised. Wrapper macros are provided for
|
||||||
* easier use.
|
* easier use.
|
||||||
*/
|
*/
|
||||||
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
|
b8 wpMemArenaInitAllocatedCustom(WpArena **arena, u64 base_capacity, WpMemAllocFlags flags, b8 zero_buffer);
|
||||||
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size);
|
b8 wpMemArenaInitBuffer(WpArena **arena, u8 *buffer, u64 buffer_size);
|
||||||
void *wpMemArenaAlloc(WpArena *arena, u64 size);
|
void *wpMemArenaAlloc(WpArena *arena, u64 size);
|
||||||
void *wpMemArenaAllocAligned(WpArena *arena, u64 size, u64 alignment);
|
void *wpMemArenaAllocAligned(WpArena *arena, u64 size, u64 alignment);
|
||||||
void *wpMemArenaRealloc(WpArena *arena, void *ptr, u64 old_size, u64 new_size);
|
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);
|
||||||
void wpMemArenaClear(WpArena *arena);
|
WpAllocatorUsage wpMemArenaQueryUsage(WpArena *arena);
|
||||||
void wpMemArenaDestroy(WpArena **arena);
|
void wpMemArenaClear(WpArena *arena);
|
||||||
|
void wpMemArenaDestroy(WpArena **arena);
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -5,14 +5,15 @@
|
|||||||
#include "../../../common/aliases/aliases.h"
|
#include "../../../common/aliases/aliases.h"
|
||||||
#include "../../../common/assert/assert.h"
|
#include "../../../common/assert/assert.h"
|
||||||
|
|
||||||
wp_intern void initialiseArenaAllocator(WpAllocator *allocator);
|
wp_intern void initialiseArenaAllocator(WpAllocator *allocator);
|
||||||
wp_intern void *memArenaAlloc(u64 size, void *alloc_obj);
|
wp_intern void *memArenaAlloc(u64 size, void *alloc_obj);
|
||||||
wp_intern void *memArenaAllocAligned(u64 size, u64 alignment, 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 *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,
|
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 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) {
|
||||||
@@ -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,
|
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;
|
WpArena *arena = (WpArena *)alloc_obj;
|
||||||
return wpMemArenaReallocAligned(arena, ptr, old_size, new_size, alignment);
|
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;
|
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
@@ -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) {
|
||||||
|
|||||||
+24
-29
@@ -8,36 +8,19 @@
|
|||||||
#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;
|
||||||
|
|
||||||
if (!initialised) {
|
if (!initialised) {
|
||||||
initialised = true;
|
initialised = true;
|
||||||
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
@@ -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
|
||||||
|
|||||||
Reference in new issue
Block a user