Compare commits
9 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 1bbe124e3e | |||
| c1b2e29c07 | |||
| a51686c8f0 | |||
| f599b5f425 | |||
| 0063bb8641 | |||
| 2a20362b44 | |||
| 02372cb644 | |||
| 5804c5d345 | |||
| ffdeb5fdc2 |
@@ -121,13 +121,17 @@ help:
|
|||||||
@$(ECHO_E) " $(GREEN)make help$(RESET) Print this help message and exit."
|
@$(ECHO_E) " $(GREEN)make help$(RESET) Print this help message and exit."
|
||||||
|
|
||||||
full: LIB_SRC = src/wapp.c
|
full: LIB_SRC = src/wapp.c
|
||||||
full: INCLUDES = common os base prng testing uuid
|
full: INCLUDES = common os base log prng testing uuid
|
||||||
full: install
|
full: install
|
||||||
|
|
||||||
base: LIB_SRC = src/base/wapp_base.c
|
base: LIB_SRC = src/base/wapp_base.c
|
||||||
base: INCLUDES = common base
|
base: INCLUDES = common base
|
||||||
base: install
|
base: install
|
||||||
|
|
||||||
|
log: LIB_SRC = src/log/wapp_log.c
|
||||||
|
log: INCLUDES = common os base log
|
||||||
|
log: install
|
||||||
|
|
||||||
os: LIB_SRC = src/os/wapp_os.c
|
os: LIB_SRC = src/os/wapp_os.c
|
||||||
os: INCLUDES = common os base
|
os: INCLUDES = common os base
|
||||||
os: install
|
os: install
|
||||||
|
|||||||
+45
-35
@@ -7,14 +7,14 @@
|
|||||||
#include "../../common/aliases/aliases.h"
|
#include "../../common/aliases/aliases.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
|
|
||||||
#define _array_header(ARRAY) (WpArrayHeader *)(wpMiscUtilsOffsetPointer(ARRAY, (i64)sizeof(WpArrayHeader) * -1))
|
#define _arrayHeader(ARRAY) (WpArrayHeader *)(wpMiscUtilsOffsetPointer(ARRAY, (i64)sizeof(WpArrayHeader) * -1))
|
||||||
|
|
||||||
wp_persist inline void _array_validate(const WpArray array, u64 item_size);
|
wp_persist inline void _arrayValidate(const WpArray array, u64 item_size);
|
||||||
|
|
||||||
u64 _arrayCount(WpArray array) {
|
u64 _arrayCount(WpArray array) {
|
||||||
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
|
|
||||||
return header->count;
|
return header->count;
|
||||||
@@ -23,7 +23,7 @@ u64 _arrayCount(WpArray array) {
|
|||||||
u64 _arrayCapacity(WpArray array) {
|
u64 _arrayCapacity(WpArray array) {
|
||||||
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
|
|
||||||
return header->capacity;
|
return header->capacity;
|
||||||
@@ -32,7 +32,7 @@ u64 _arrayCapacity(WpArray array) {
|
|||||||
u64 _arrayItemSize(WpArray array) {
|
u64 _arrayItemSize(WpArray array) {
|
||||||
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
|
|
||||||
return header->item_size;
|
return header->item_size;
|
||||||
@@ -41,7 +41,7 @@ u64 _arrayItemSize(WpArray array) {
|
|||||||
void _arraySetCount(WpArray array, u64 count) {
|
void _arraySetCount(WpArray array, u64 count) {
|
||||||
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
wpDebugAssert(array != NULL, "`array` should not be NULL");
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
|
|
||||||
header->count = count;
|
header->count = count;
|
||||||
@@ -49,9 +49,9 @@ void _arraySetCount(WpArray array, u64 count) {
|
|||||||
|
|
||||||
void *_arrayGet(WpArray array, u64 index, u64 item_size) {
|
void *_arrayGet(WpArray array, u64 index, u64 item_size) {
|
||||||
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_arrayValidate(array, item_size);
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(index < header->count, "`index` is out of bounds");
|
wpRuntimeAssert(index < header->count, "`index` is out of bounds");
|
||||||
|
|
||||||
return wpMiscUtilsOffsetPointer(array, header->item_size * index);
|
return wpMiscUtilsOffsetPointer(array, header->item_size * index);
|
||||||
@@ -60,15 +60,15 @@ void *_arrayGet(WpArray array, u64 index, u64 item_size) {
|
|||||||
void _arraySet(WpArray array, u64 index, void *value, u64 item_size) {
|
void _arraySet(WpArray array, u64 index, void *value, u64 item_size) {
|
||||||
void *item = _arrayGet(array, index, item_size);
|
void *item = _arrayGet(array, index, item_size);
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
memcpy(item, value, header->item_size);
|
memcpy(item, value, header->item_size);
|
||||||
}
|
}
|
||||||
|
|
||||||
void _arrayAppendCapped(WpArray array, void *value, u64 item_size) {
|
void _arrayAppendCapped(WpArray array, void *value, u64 item_size) {
|
||||||
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_arrayValidate(array, item_size);
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
if (header->count >= header->capacity) { return; }
|
if (header->count >= header->capacity) { return; }
|
||||||
|
|
||||||
u64 index = (header->count)++;
|
u64 index = (header->count)++;
|
||||||
@@ -77,11 +77,11 @@ void _arrayAppendCapped(WpArray array, void *value, u64 item_size) {
|
|||||||
|
|
||||||
void _arrayExtendCappend(WpArray dst, const WpArray src, u64 item_size) {
|
void _arrayExtendCappend(WpArray dst, const WpArray src, u64 item_size) {
|
||||||
wpRuntimeAssert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
|
wpRuntimeAssert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_arrayValidate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_arrayValidate(src, item_size);
|
||||||
|
|
||||||
WpArrayHeader *src_header = _array_header(src);
|
WpArrayHeader *src_header = _arrayHeader(src);
|
||||||
WpArrayHeader *dst_header = _array_header(dst);
|
WpArrayHeader *dst_header = _arrayHeader(dst);
|
||||||
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
||||||
|
|
||||||
u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity;
|
u64 copy_count = src_header->count < remaining_capacity ? src_header->count : remaining_capacity;
|
||||||
@@ -92,13 +92,13 @@ void _arrayExtendCappend(WpArray dst, const WpArray src, u64 item_size) {
|
|||||||
|
|
||||||
void _arrayCopyCapped(WpArray dst, const WpArray src, u64 item_size) {
|
void _arrayCopyCapped(WpArray dst, const WpArray src, u64 item_size) {
|
||||||
wpRuntimeAssert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
|
wpRuntimeAssert(dst != NULL && src != NULL, "`dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_arrayValidate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_arrayValidate(src, item_size);
|
||||||
|
|
||||||
_arrayClear(dst, item_size);
|
_arrayClear(dst, item_size);
|
||||||
|
|
||||||
WpArrayHeader *src_header = _array_header(src);
|
WpArrayHeader *src_header = _arrayHeader(src);
|
||||||
WpArrayHeader *dst_header = _array_header(dst);
|
WpArrayHeader *dst_header = _arrayHeader(dst);
|
||||||
u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity;
|
u64 copy_count = src_header->count < dst_header->capacity ? src_header->count : dst_header->capacity;
|
||||||
memcpy((void *)dst, (void *)src, copy_count * src_header->item_size);
|
memcpy((void *)dst, (void *)src, copy_count * src_header->item_size);
|
||||||
dst_header->count = copy_count;
|
dst_header->count = copy_count;
|
||||||
@@ -107,11 +107,11 @@ void _arrayCopyCapped(WpArray dst, const WpArray src, u64 item_size) {
|
|||||||
WpArray _arrayAppendAlloc(const WpAllocator *allocator, WpArray array, void *value,
|
WpArray _arrayAppendAlloc(const WpAllocator *allocator, WpArray array, void *value,
|
||||||
WpArrayInitFlags flags, u64 item_size) {
|
WpArrayInitFlags flags, u64 item_size) {
|
||||||
wpRuntimeAssert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
|
wpRuntimeAssert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_arrayValidate(array, item_size);
|
||||||
|
|
||||||
WpArray output = array;
|
WpArray output = array;
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
if (header->count >= header->capacity) {
|
if (header->count >= header->capacity) {
|
||||||
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(header->capacity * 2);
|
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(header->capacity * 2);
|
||||||
output = (WpArray )_arrayAllocCapacity(allocator, new_capacity, flags,
|
output = (WpArray )_arrayAllocCapacity(allocator, new_capacity, flags,
|
||||||
@@ -136,13 +136,13 @@ RETURN_ARRAY_APPEND_ALLOC:
|
|||||||
WpArray _arrayExtendAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src,
|
WpArray _arrayExtendAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src,
|
||||||
WpArrayInitFlags flags, u64 item_size) {
|
WpArrayInitFlags flags, u64 item_size) {
|
||||||
wpRuntimeAssert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
wpRuntimeAssert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_arrayValidate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_arrayValidate(src, item_size);
|
||||||
|
|
||||||
WpArray output = dst;
|
WpArray output = dst;
|
||||||
|
|
||||||
WpArrayHeader *src_header = _array_header(src);
|
WpArrayHeader *src_header = _arrayHeader(src);
|
||||||
WpArrayHeader *dst_header = _array_header(dst);
|
WpArrayHeader *dst_header = _arrayHeader(dst);
|
||||||
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
||||||
if (src_header->count >= remaining_capacity) {
|
if (src_header->count >= remaining_capacity) {
|
||||||
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(dst_header->capacity * 2);
|
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(dst_header->capacity * 2);
|
||||||
@@ -168,13 +168,13 @@ RETURN_ARRAY_EXTEND_ALLOC:
|
|||||||
WpArray _arrayCopyAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src,
|
WpArray _arrayCopyAlloc(const WpAllocator *allocator, WpArray dst, const WpArray src,
|
||||||
WpArrayInitFlags flags, u64 item_size) {
|
WpArrayInitFlags flags, u64 item_size) {
|
||||||
wpRuntimeAssert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
wpRuntimeAssert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||||
_array_validate(dst, item_size);
|
_arrayValidate(dst, item_size);
|
||||||
_array_validate(src, item_size);
|
_arrayValidate(src, item_size);
|
||||||
|
|
||||||
WpArray output = dst;
|
WpArray output = dst;
|
||||||
|
|
||||||
WpArrayHeader *src_header = _array_header(src);
|
WpArrayHeader *src_header = _arrayHeader(src);
|
||||||
WpArrayHeader *dst_header = _array_header(dst);
|
WpArrayHeader *dst_header = _arrayHeader(dst);
|
||||||
if (src_header->count >= dst_header->capacity) {
|
if (src_header->count >= dst_header->capacity) {
|
||||||
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(dst_header->capacity * 2);
|
u64 new_capacity = wpMiscUtilsU64RoundUpPow2(dst_header->capacity * 2);
|
||||||
output = (WpArray )_arrayAllocCapacity(allocator, new_capacity,
|
output = (WpArray )_arrayAllocCapacity(allocator, new_capacity,
|
||||||
@@ -197,9 +197,9 @@ RETURN_ARRAY_COPY_ALLOC:
|
|||||||
|
|
||||||
void *_arrayPop(WpArray array, u64 item_size) {
|
void *_arrayPop(WpArray array, u64 item_size) {
|
||||||
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_arrayValidate(array, item_size);
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
if (header->count == 0) { return NULL; }
|
if (header->count == 0) { return NULL; }
|
||||||
|
|
||||||
u64 index = header->count - 1;
|
u64 index = header->count - 1;
|
||||||
@@ -210,9 +210,9 @@ void *_arrayPop(WpArray array, u64 item_size) {
|
|||||||
|
|
||||||
void _arrayClear(WpArray array, u64 item_size) {
|
void _arrayClear(WpArray array, u64 item_size) {
|
||||||
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
wpRuntimeAssert(array != NULL, "`array` should not be NULL");
|
||||||
_array_validate(array, item_size);
|
_arrayValidate(array, item_size);
|
||||||
|
|
||||||
WpArrayHeader *header = _array_header(array);
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
header->count = 0;
|
header->count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -259,8 +259,18 @@ WpArray _arrayFromPreallocatedBuffer(void *buffer, u64 buffer_size, WpArrayInitF
|
|||||||
return output;
|
return output;
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_persist inline void _array_validate(const WpArray array, u64 item_size) {
|
void _arrayDealloc(const WpAllocator *allocator, WpArray *array, u64 item_size) {
|
||||||
WpArrayHeader *header = _array_header(array);
|
wpRuntimeAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
u64 capacity = wpArrayCapacity(*array);
|
||||||
|
u64 allocation_size = _arrayCalcAllocSize(capacity, item_size);
|
||||||
|
void *header = (void *)_arrayHeader(*array);
|
||||||
|
wpMemAllocatorFree(allocator, &header, allocation_size);
|
||||||
|
*array = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_persist inline void _arrayValidate(const WpArray array, u64 item_size) {
|
||||||
|
WpArrayHeader *header = _arrayHeader(array);
|
||||||
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
wpRuntimeAssert(WP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||||
wpRuntimeAssert(item_size == header->item_size, "Invalid item type provided");
|
wpRuntimeAssert(item_size == header->item_size, "Invalid item type provided");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -176,6 +176,10 @@ typedef enum {
|
|||||||
((TYPE *)_arrayAllocCapacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE)))
|
((TYPE *)_arrayAllocCapacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE)))
|
||||||
#define wpArrayFromPreallcatedBuffer(TYPE, BUFFER, BUFFER_SIZE) \
|
#define wpArrayFromPreallcatedBuffer(TYPE, BUFFER, BUFFER_SIZE) \
|
||||||
((TYPE *)_array_from_preallcated_buffer(BUFFER, BUFFER_SIZE, sizeof(TYPE)))
|
((TYPE *)_array_from_preallcated_buffer(BUFFER, BUFFER_SIZE, sizeof(TYPE)))
|
||||||
|
// Only needed for allocators like malloc where each allocation has to be freed on its own.
|
||||||
|
// No need to use it for allocators like Arena.
|
||||||
|
#define wpArrayDealloc(TYPE, ALLOCATOR_PTR, ARRAY_DPTR) \
|
||||||
|
(_arrayDealloc(ALLOCATOR_PTR, (WpArray *)ARRAY_DPTR, sizeof(TYPE)))
|
||||||
|
|
||||||
|
|
||||||
typedef struct WpArrayHeader WpArrayHeader;
|
typedef struct WpArrayHeader WpArrayHeader;
|
||||||
@@ -208,6 +212,7 @@ WpArray _arrayAllocCapacity(const WpAllocator *allocator, u64 capacity, WpArrayI
|
|||||||
u64 item_size);
|
u64 item_size);
|
||||||
WpArray _arrayFromPreallocatedBuffer(void *buffer, u64 buffer_size, WpArrayInitFlags flags,
|
WpArray _arrayFromPreallocatedBuffer(void *buffer, u64 buffer_size, WpArrayInitFlags flags,
|
||||||
u64 item_size);
|
u64 item_size);
|
||||||
|
void _arrayDealloc(const WpAllocator *allocator, WpArray *array, u64 item_size);
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -169,13 +169,13 @@ typedef WpDblNode WpStr8Node;
|
|||||||
WpDblList *_dblListAlloc(const WpAllocator *allocator, u64 item_size);
|
WpDblList *_dblListAlloc(const WpAllocator *allocator, u64 item_size);
|
||||||
WpDblNode *_dblListNodeAlloc(const WpAllocator *allocator, void *item, u64 item_size);
|
WpDblNode *_dblListNodeAlloc(const WpAllocator *allocator, void *item, u64 item_size);
|
||||||
WpDblNode *_dblListGet(const WpDblList *list, u64 index, u64 item_size);
|
WpDblNode *_dblListGet(const WpDblList *list, u64 index, u64 item_size);
|
||||||
void _dblListPushFront(WpDblList *list, WpDblNode *node, u64 item_size);
|
void _dblListPushFront(WpDblList *list, WpDblNode *node, u64 item_size);
|
||||||
void _dblListPushBack(WpDblList *list, WpDblNode *node, u64 item_size);
|
void _dblListPushBack(WpDblList *list, WpDblNode *node, u64 item_size);
|
||||||
void _dblListInsert(WpDblList *list, WpDblNode *node, u64 index, u64 item_size);
|
void _dblListInsert(WpDblList *list, WpDblNode *node, u64 index, u64 item_size);
|
||||||
WpDblNode *_dblListPopFront(WpDblList *list, u64 item_size);
|
WpDblNode *_dblListPopFront(WpDblList *list, u64 item_size);
|
||||||
WpDblNode *_dblListPopBack(WpDblList *list, u64 item_size);
|
WpDblNode *_dblListPopBack(WpDblList *list, u64 item_size);
|
||||||
WpDblNode *_dblListRemove(WpDblList *list, u64 index, u64 item_size);
|
WpDblNode *_dblListRemove(WpDblList *list, u64 index, u64 item_size);
|
||||||
void _dblListEmpty(WpDblList *list, u64 item_size);
|
void _dblListEmpty(WpDblList *list, u64 item_size);
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -6,30 +6,73 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size) {
|
void *wpMemAllocatorAlloc(const WpAllocator *allocator, u64 size) {
|
||||||
wpDebugAssert(allocator != NULL && (allocator->alloc) != NULL, "`allocator` and `allocator->alloc` should not be NULL");
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_ALLOC)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return allocator->alloc(size, allocator->obj);
|
return allocator->alloc(size, allocator->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment) {
|
void *wpMemAllocatorAllocAligned(const WpAllocator *allocator, u64 size, u64 alignment) {
|
||||||
wpDebugAssert(allocator != NULL && (allocator->alloc_aligned) != NULL, "`allocator` and `allocator->alloc_aligned` should not be NULL");
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_ALLOC_ALIGNED)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return allocator->alloc_aligned(size, alignment, allocator->obj);
|
return allocator->alloc_aligned(size, alignment, allocator->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
wpDebugAssert(allocator != NULL && (allocator->realloc) != NULL, "`allocator` and `allocator->realloc` should not be NULL");
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_REALLOC)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return allocator->realloc(ptr, old_size, new_size, allocator->obj);
|
return allocator->realloc(ptr, old_size, new_size, allocator->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
wpDebugAssert(allocator != NULL && (allocator->realloc_aligned) != NULL, "`allocator` and `allocator->realloc_aligned` should not be NULL");
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_REALLOC_ALIGNED)) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
return allocator->realloc_aligned(ptr, old_size, new_size, alignment, allocator->obj);
|
return allocator->realloc_aligned(ptr, old_size, new_size, alignment, allocator->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size) {
|
void wpMemAllocatorFree(const WpAllocator *allocator, void **ptr, u64 size) {
|
||||||
if (!allocator || !(allocator->free)) {
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
if (!wpMemAllocatorOpSupported(allocator, WP_MEM_OP_FREE)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
allocator->free(ptr, size, allocator->obj);
|
allocator->free(ptr, size, allocator->obj);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op) {
|
||||||
|
wpDebugAssert(allocator != NULL, "`allocator` should not be NULL");
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case WP_MEM_OP_ALLOC:
|
||||||
|
return allocator->alloc != NULL;
|
||||||
|
case WP_MEM_OP_ALLOC_ALIGNED:
|
||||||
|
return allocator->alloc_aligned != NULL;
|
||||||
|
case WP_MEM_OP_REALLOC:
|
||||||
|
return allocator->realloc != NULL;
|
||||||
|
case WP_MEM_OP_REALLOC_ALIGNED:
|
||||||
|
return allocator->realloc_aligned != NULL;
|
||||||
|
case WP_MEM_OP_FREE:
|
||||||
|
return allocator->free != NULL;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|||||||
@@ -11,6 +11,16 @@
|
|||||||
BEGIN_C_LINKAGE
|
BEGIN_C_LINKAGE
|
||||||
#endif // !WP_PLATFORM_CPP
|
#endif // !WP_PLATFORM_CPP
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
WP_MEM_OP_ALLOC,
|
||||||
|
WP_MEM_OP_ALLOC_ALIGNED,
|
||||||
|
WP_MEM_OP_REALLOC,
|
||||||
|
WP_MEM_OP_REALLOC_ALIGNED,
|
||||||
|
WP_MEM_OP_FREE,
|
||||||
|
|
||||||
|
COUNT_MEM_OPS
|
||||||
|
} WpMemOp;
|
||||||
|
|
||||||
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);
|
||||||
@@ -42,6 +52,7 @@ void *wpMemAllocatorRealloc(const WpAllocator *allocator, void *ptr, u64 old_siz
|
|||||||
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);
|
||||||
|
b8 wpMemAllocatorOpSupported(const WpAllocator *allocator, WpMemOp op);
|
||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
END_C_LINKAGE
|
END_C_LINKAGE
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ typedef WpQueue WpStr8Queue;
|
|||||||
|
|
||||||
#ifdef WP_PLATFORM_CPP
|
#ifdef WP_PLATFORM_CPP
|
||||||
#define wpQueue(TYPE, CAPACITY) ([&]() { \
|
#define wpQueue(TYPE, CAPACITY) ([&]() { \
|
||||||
wp_persist WpArray arr = wpArrayWithCapacity(TYPE, CAPACITY, WP_ARRAY_INIT_FILLED); \
|
wp_persist WpArray arr = wpArrayWithCapacity(TYPE, CAPACITY, WP_ARRAY_INIT_FILLED); \
|
||||||
wp_persist WpQueue queue = { \
|
wp_persist WpQueue queue = { \
|
||||||
arr, \
|
arr, \
|
||||||
0, \
|
0, \
|
||||||
@@ -54,7 +54,7 @@ typedef WpQueue WpStr8Queue;
|
|||||||
}())
|
}())
|
||||||
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
||||||
wp_persist WpQueue queue = { \
|
wp_persist WpQueue queue = { \
|
||||||
wpArrayAllocCapacity(TYPE, ALLOCATOR_PTR, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
wpArrayAllocCapacity(TYPE, ALLOCATOR_PTR, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
||||||
0, \
|
0, \
|
||||||
0, \
|
0, \
|
||||||
0, \
|
0, \
|
||||||
@@ -64,13 +64,13 @@ typedef WpQueue WpStr8Queue;
|
|||||||
}())
|
}())
|
||||||
#else
|
#else
|
||||||
#define wpQueue(TYPE, CAPACITY) ((WpQueue){ \
|
#define wpQueue(TYPE, CAPACITY) ((WpQueue){ \
|
||||||
.items = wpArrayWithCapacity(TYPE, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
.items = wpArrayWithCapacity(TYPE, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
||||||
.front = 0, \
|
.front = 0, \
|
||||||
.back = 0, \
|
.back = 0, \
|
||||||
.count = 0, \
|
.count = 0, \
|
||||||
})
|
})
|
||||||
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((WpQueue){ \
|
#define wpQueueAlloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((WpQueue){ \
|
||||||
.items = wpArrayAllocCapacity(TYPE, ALLOCATOR_PTR, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
.items = wpArrayAllocCapacity(TYPE, ALLOCATOR_PTR, CAPACITY, WP_ARRAY_INIT_FILLED), \
|
||||||
.front = 0, \
|
.front = 0, \
|
||||||
.back = 0, \
|
.back = 0, \
|
||||||
.count = 0, \
|
.count = 0, \
|
||||||
@@ -88,6 +88,11 @@ typedef WpQueue WpStr8Queue;
|
|||||||
#define wpQueuePop(TYPE, QUEUE_PTR) ( \
|
#define wpQueuePop(TYPE, QUEUE_PTR) ( \
|
||||||
(TYPE *)_queuePop(QUEUE_PTR, sizeof(TYPE)) \
|
(TYPE *)_queuePop(QUEUE_PTR, sizeof(TYPE)) \
|
||||||
)
|
)
|
||||||
|
#define wpQueueDealloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR) \
|
||||||
|
(wpArrayDealloc(TYPE, ALLOCATOR_PTR, &((QUEUE_PTR)->items)), \
|
||||||
|
(QUEUE_PTR)->front = 0, \
|
||||||
|
(QUEUE_PTR)->back = 0, \
|
||||||
|
(QUEUE_PTR)->count = 0)
|
||||||
|
|
||||||
void _queuePush(WpQueue *queue, void *item, u64 item_size);
|
void _queuePush(WpQueue *queue, void *item, u64 item_size);
|
||||||
WpQueue *_queuePushAlloc(const WpAllocator *allocator, WpQueue *queue, void *item, u64 item_size);
|
WpQueue *_queuePushAlloc(const WpAllocator *allocator, WpQueue *queue, void *item, u64 item_size);
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#include "stream.h"
|
||||||
|
#include "../../common/aliases/aliases.h"
|
||||||
|
#include "../../common/assert/assert.h"
|
||||||
|
|
||||||
|
void _streamValidate(const WpStream *stream, u64 item_size);
|
||||||
|
|
||||||
|
b8 _streamAtEnd(const WpStream *stream, u64 item_size) {
|
||||||
|
_streamValidate(stream, item_size);
|
||||||
|
return stream->position >= stream->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
b8 _streamHasNext(const WpStream *stream, u64 item_size) {
|
||||||
|
_streamValidate(stream, item_size);
|
||||||
|
return stream->position + 1 < stream->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *_streamPeek(const WpStream *stream, u64 item_size) {
|
||||||
|
if (_streamHasNext(stream, item_size)) {
|
||||||
|
u64 index = (stream->position + 1) * stream->item_size;
|
||||||
|
return (void *)(&((u8 *)(stream->data))[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void *_streamConsume(WpStream *stream, u64 item_size) {
|
||||||
|
if (!_streamAtEnd(stream, item_size)) {
|
||||||
|
u64 index = stream->position++ * stream->item_size;
|
||||||
|
return (void *)(&((u8 *)(stream->data))[index]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void _streamValidate(const WpStream *stream, u64 item_size) {
|
||||||
|
wpDebugAssert(stream != NULL, "`stream` should not be NULL");
|
||||||
|
wpRuntimeAssert(stream->item_size == item_size, "Invalid item type provided");
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#ifndef STREAM_H
|
||||||
|
#define STREAM_H
|
||||||
|
|
||||||
|
#include "../../common/aliases/aliases.h"
|
||||||
|
#include "../../common/platform/platform.h"
|
||||||
|
|
||||||
|
#ifdef WP_PLATFORM_CPP
|
||||||
|
BEGIN_C_LINKAGE
|
||||||
|
#endif // !WP_PLATFORM_CPP
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
void *data;
|
||||||
|
u64 count;
|
||||||
|
u64 position;
|
||||||
|
u64 item_size;
|
||||||
|
} WpStream;
|
||||||
|
|
||||||
|
// NOTE (Abdelrahman): WpStream typedefs for readability
|
||||||
|
typedef WpStream WpVoidPtrStream;
|
||||||
|
typedef WpStream WpC8Stream;
|
||||||
|
typedef WpStream WpC16Stream;
|
||||||
|
typedef WpStream WpC32Stream;
|
||||||
|
typedef WpStream WpU8Stream;
|
||||||
|
typedef WpStream WpU16Stream;
|
||||||
|
typedef WpStream WpU32Stream;
|
||||||
|
typedef WpStream WpU64Stream;
|
||||||
|
typedef WpStream WpB8Stream;
|
||||||
|
typedef WpStream WpI8Stream;
|
||||||
|
typedef WpStream WpI16Stream;
|
||||||
|
typedef WpStream WpI32Stream;
|
||||||
|
typedef WpStream WpI64Stream;
|
||||||
|
typedef WpStream WpF32Stream;
|
||||||
|
typedef WpStream WpF64Stream;
|
||||||
|
typedef WpStream WpF128Stream;
|
||||||
|
typedef WpStream WpUptrStream;
|
||||||
|
typedef WpStream WpIptrStream;
|
||||||
|
typedef WpStream WpStr8Stream;
|
||||||
|
|
||||||
|
#ifdef WP_PLATFORM_CPP
|
||||||
|
#define wpStream(TYPE, DATA, SIZE) ([&]() { \
|
||||||
|
return WpStream{ \
|
||||||
|
DATA, \
|
||||||
|
SIZE / sizeof(TYPE), \
|
||||||
|
0, \
|
||||||
|
sizeof(TYPE) \
|
||||||
|
}; \
|
||||||
|
}())
|
||||||
|
#else
|
||||||
|
#define wpStream(TYPE, DATA, SIZE) ( \
|
||||||
|
(WpStream){ \
|
||||||
|
.data = DATA, \
|
||||||
|
.count = SIZE / sizeof(TYPE), \
|
||||||
|
.position = 0, \
|
||||||
|
.item_size = sizeof(TYPE) \
|
||||||
|
} \
|
||||||
|
)
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define wpStreamAtEnd(TYPE, STREAM) _streamAtEnd(STREAM, sizeof(TYPE))
|
||||||
|
#define wpStreamHasNext(TYPE, STREAM) _streamHasNext(STREAM, sizeof(TYPE))
|
||||||
|
#define wpStreamPeek(TYPE, STREAM) ((TYPE *)_streamPeek(STREAM, sizeof(TYPE)))
|
||||||
|
#define wpStreamConsume(TYPE, STREAM) ((TYPE *)_streamConsume(STREAM, sizeof(TYPE)))
|
||||||
|
|
||||||
|
b8 _streamAtEnd(const WpStream *stream, u64 item_size);
|
||||||
|
b8 _streamHasNext(const WpStream *stream, u64 item_size);
|
||||||
|
void *_streamPeek(const WpStream *stream, u64 item_size);
|
||||||
|
void *_streamConsume(WpStream *stream, u64 item_size);
|
||||||
|
|
||||||
|
#ifdef WP_PLATFORM_CPP
|
||||||
|
END_C_LINKAGE
|
||||||
|
#endif // !WP_PLATFORM_CPP
|
||||||
|
|
||||||
|
#endif // !STREAM_H
|
||||||
@@ -57,19 +57,19 @@ typedef const WpStr8 WpStr8RO;
|
|||||||
// Utilises the fact that memcpy returns pointer to dest buffer and that getting
|
// Utilises the fact that memcpy returns pointer to dest buffer and that getting
|
||||||
// address of compound literals is valid in C to create a string on the stack
|
// address of compound literals is valid in C to create a string on the stack
|
||||||
#define wpStr8Lit(STRING) ((WpStr8){.capacity = (sizeof(STRING) - 1) * 2, \
|
#define wpStr8Lit(STRING) ((WpStr8){.capacity = (sizeof(STRING) - 1) * 2, \
|
||||||
.size = sizeof(STRING) - 1, \
|
.size = sizeof(STRING) - 1, \
|
||||||
.buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), \
|
.buf = memcpy(&((c8 [sizeof(STRING) * 2]){0}), \
|
||||||
STRING, \
|
STRING, \
|
||||||
sizeof(STRING))})
|
sizeof(STRING))})
|
||||||
#define wpStr8LitRo(STRING) ((WpStr8RO){.capacity = sizeof(STRING) - 1, \
|
#define wpStr8LitRo(STRING) ((WpStr8RO){.capacity = sizeof(STRING) - 1, \
|
||||||
.size = sizeof(STRING) - 1, \
|
.size = sizeof(STRING) - 1, \
|
||||||
.buf = (c8 *)STRING})
|
.buf = (c8 *)STRING})
|
||||||
// To be used only when initialising a static storage variable in compilers that don't support
|
// To be used only when initialising a static storage variable in compilers that don't support
|
||||||
// initialisers with the syntax of wpStr8LitRo (e.g. gcc). Should only be used when necessary
|
// initialisers with the syntax of wpStr8LitRo (e.g. gcc). Should only be used when necessary
|
||||||
// and only be assigned to a WpStr8RO variable to avoid any attempt at modifying the string
|
// and only be assigned to a WpStr8RO variable to avoid any attempt at modifying the string
|
||||||
#define wpStr8LitRoInitialiserList(STRING) {.capacity = sizeof(STRING) - 1, \
|
#define wpStr8LitRoInitialiserList(STRING) {.capacity = sizeof(STRING) - 1, \
|
||||||
.size = sizeof(STRING) - 1, \
|
.size = sizeof(STRING) - 1, \
|
||||||
.buf = (c8 *)STRING}
|
.buf = (c8 *)STRING}
|
||||||
#endif // !WP_PLATFORM_CPP
|
#endif // !WP_PLATFORM_CPP
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
#include "array/array.c"
|
#include "array/array.c"
|
||||||
#include "dbl_list/dbl_list.c"
|
#include "dbl_list/dbl_list.c"
|
||||||
#include "queue/queue.c"
|
#include "queue/queue.c"
|
||||||
|
#include "stream/stream.c"
|
||||||
#include "mem/allocator/mem_allocator.c"
|
#include "mem/allocator/mem_allocator.c"
|
||||||
#include "mem/utils/mem_utils.c"
|
#include "mem/utils/mem_utils.c"
|
||||||
#include "strings/str8/str8.c"
|
#include "strings/str8/str8.c"
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
#include "array/array.h"
|
#include "array/array.h"
|
||||||
#include "dbl_list/dbl_list.h"
|
#include "dbl_list/dbl_list.h"
|
||||||
#include "queue/queue.h"
|
#include "queue/queue.h"
|
||||||
|
#include "stream/stream.h"
|
||||||
#include "mem/allocator/mem_allocator.h"
|
#include "mem/allocator/mem_allocator.h"
|
||||||
#include "mem/utils/mem_utils.h"
|
#include "mem/utils/mem_utils.h"
|
||||||
#include "strings/str8/str8.h"
|
#include "strings/str8/str8.h"
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
#ifndef PLATFORM_H
|
#ifndef PLATFORM_H
|
||||||
#define PLATFORM_H
|
#define PLATFORM_H
|
||||||
|
|
||||||
|
// Operating system
|
||||||
#if defined(__ANDROID__)
|
#if defined(__ANDROID__)
|
||||||
#define WP_PLATFORM_ANDROID
|
#define WP_PLATFORM_ANDROID
|
||||||
#define WP_PLATFORM_POSIX
|
#define WP_PLATFORM_POSIX
|
||||||
@@ -60,6 +61,39 @@
|
|||||||
#error "Unrecognised platform"
|
#error "Unrecognised platform"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
// Compiler
|
||||||
|
#if defined(__clang__)
|
||||||
|
#define WP_PLATFORM_CLANG
|
||||||
|
|
||||||
|
#if defined(__GNUG__)
|
||||||
|
#define WP_PLATFORM_CLANGPP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define WP_PLATFORM_CLANG_MAJOR __clang_major__
|
||||||
|
#define WP_PLATFORM_CLANG_MINOR __clang_minor__
|
||||||
|
#define WP_PLATFORM_CLANG_PATCH __clang_patchlevel__
|
||||||
|
#elif defined(__GNUC__) || defined(__GNUG__)
|
||||||
|
#define WP_PLATFORM_GCC
|
||||||
|
|
||||||
|
#if defined(__GNUG__)
|
||||||
|
#define WP_PLATFORM_GPP
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#define WP_PLATFORM_GCC_MAJOR __GNUC__
|
||||||
|
#define WP_PLATFORM_GCC_MINOR __GNUC_MINOR__
|
||||||
|
|
||||||
|
#if defined(__GNUC_PATCHLEVEL__)
|
||||||
|
#define WP_PLATFORM_GCC_PATCH __GNUC_PATCHLEVEL__
|
||||||
|
#endif
|
||||||
|
#elif defined(_MSC_VER)
|
||||||
|
#define WP_PLATFORM_MSVC
|
||||||
|
#define WP_PLATFORM_MSVC_FULL_VERSION _MSC_FULL_VER
|
||||||
|
#define WP_PLATFORM_MSVC_VERSION _MSC_VER
|
||||||
|
#else
|
||||||
|
#error "Unrecognised compiler"
|
||||||
|
#endif
|
||||||
|
|
||||||
|
// Language
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
#define WP_PLATFORM_CPP
|
#define WP_PLATFORM_CPP
|
||||||
#define WP_PLATFORM_CPP_VERSION __cplusplus
|
#define WP_PLATFORM_CPP_VERSION __cplusplus
|
||||||
|
|||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#include "test_stream.h"
|
||||||
|
|
||||||
|
#define COUNT 3
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_at_end(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
for (u32 i = 0; i < COUNT; ++i) {
|
||||||
|
wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (i == 2 ? wpStreamAtEnd(c8, &stream) : !wpStreamAtEnd(c8, &stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_peek(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
c8 *c = NULL;
|
||||||
|
while (!wpStreamAtEnd(c8, &stream)) {
|
||||||
|
c = wpStreamPeek(c8, &stream);
|
||||||
|
wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (wpStreamAtEnd(c8, &stream) ? c == NULL : c != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_consume(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
c8 *c = NULL;
|
||||||
|
for (u32 i = 0; i <= COUNT; ++i) {
|
||||||
|
c = wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (i == COUNT ? c == NULL : c != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
@@ -0,0 +1,50 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#include "test_stream.h"
|
||||||
|
|
||||||
|
#define COUNT 3
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_at_end(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
for (u32 i = 0; i < COUNT; ++i) {
|
||||||
|
wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (i == 2 ? wpStreamAtEnd(c8, &stream) : !wpStreamAtEnd(c8, &stream));
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_peek(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
c8 *c = NULL;
|
||||||
|
while (!wpStreamAtEnd(c8, &stream)) {
|
||||||
|
c = wpStreamPeek(c8, &stream);
|
||||||
|
wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (wpStreamAtEnd(c8, &stream) ? c == NULL : c != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_consume(void) {
|
||||||
|
b8 result = true;
|
||||||
|
|
||||||
|
c8 characters[COUNT] = {'a', 'b', 'c'};
|
||||||
|
WpC8Stream stream = wpStream(c8, characters, COUNT);
|
||||||
|
|
||||||
|
c8 *c = NULL;
|
||||||
|
for (u32 i = 0; i <= COUNT; ++i) {
|
||||||
|
c = wpStreamConsume(c8, &stream);
|
||||||
|
result = result && (i == COUNT ? c == NULL : c != NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
return wpTesterResult(result);
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
// vim:fileencoding=utf-8:foldmethod=marker
|
||||||
|
|
||||||
|
#ifndef TEST_STREAM_H
|
||||||
|
#define TEST_STREAM_H
|
||||||
|
|
||||||
|
#include "wapp.h"
|
||||||
|
|
||||||
|
WpTestFuncResult test_stream_at_end(void);
|
||||||
|
WpTestFuncResult test_stream_peek(void);
|
||||||
|
WpTestFuncResult test_stream_consume(void);
|
||||||
|
|
||||||
|
#endif // !TEST_STREAM_H
|
||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "test_str8_array.h"
|
#include "test_str8_array.h"
|
||||||
#include "test_i32_array.h"
|
#include "test_i32_array.h"
|
||||||
#include "test_queue.h"
|
#include "test_queue.h"
|
||||||
|
#include "test_stream.h"
|
||||||
#include "test_cpath.h"
|
#include "test_cpath.h"
|
||||||
#include "test_file.h"
|
#include "test_file.h"
|
||||||
#include "test_shell_commander.h"
|
#include "test_shell_commander.h"
|
||||||
@@ -47,6 +48,9 @@ int main(void) {
|
|||||||
test_queue_push,
|
test_queue_push,
|
||||||
test_queue_push_alloc,
|
test_queue_push_alloc,
|
||||||
test_queue_pop,
|
test_queue_pop,
|
||||||
|
test_stream_at_end,
|
||||||
|
test_stream_peek,
|
||||||
|
test_stream_consume,
|
||||||
test_str8_lit,
|
test_str8_lit,
|
||||||
test_str8_lit_ro,
|
test_str8_lit_ro,
|
||||||
test_str8_buf,
|
test_str8_buf,
|
||||||
|
|||||||
@@ -5,6 +5,7 @@
|
|||||||
#include "test_str8_array.h"
|
#include "test_str8_array.h"
|
||||||
#include "test_i32_array.h"
|
#include "test_i32_array.h"
|
||||||
#include "test_queue.h"
|
#include "test_queue.h"
|
||||||
|
#include "test_stream.h"
|
||||||
#include "test_cpath.h"
|
#include "test_cpath.h"
|
||||||
#include "test_file.h"
|
#include "test_file.h"
|
||||||
#include "test_shell_commander.h"
|
#include "test_shell_commander.h"
|
||||||
@@ -47,6 +48,9 @@ int main(void) {
|
|||||||
test_queue_push,
|
test_queue_push,
|
||||||
test_queue_push_alloc,
|
test_queue_push_alloc,
|
||||||
test_queue_pop,
|
test_queue_pop,
|
||||||
|
test_stream_at_end,
|
||||||
|
test_stream_peek,
|
||||||
|
test_stream_consume,
|
||||||
test_str8_lit,
|
test_str8_lit,
|
||||||
test_str8_lit_ro,
|
test_str8_lit_ro,
|
||||||
test_str8_buf,
|
test_str8_buf,
|
||||||
|
|||||||
Reference in New Issue
Block a user