Compare commits
6 Commits
4cc8cb3d25
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3d3452f523 | ||
|
|
8e41b627bc | ||
|
|
7a54c28c0f | ||
|
|
bd659e64fc | ||
|
|
21ac756fad | ||
|
|
243f04c0ca |
@@ -104,7 +104,8 @@ void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size)
|
||||
dst_header->count = copy_count;
|
||||
}
|
||||
|
||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size) {
|
||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
|
||||
ArrayInitFlags flags, u64 item_size) {
|
||||
wapp_runtime_assert(allocator != NULL && array != NULL, "`allocator` and `array` should not be NULL");
|
||||
_array_validate(array, item_size);
|
||||
|
||||
@@ -113,7 +114,8 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
|
||||
ArrayHeader *header = _array_header(array);
|
||||
if (header->count >= header->capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(header->capacity * 2);
|
||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, header->item_size, false);
|
||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, flags,
|
||||
header->item_size);
|
||||
if (!output) {
|
||||
output = array;
|
||||
goto RETURN_ARRAY_APPEND_ALLOC;
|
||||
@@ -123,11 +125,16 @@ GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array,
|
||||
|
||||
_array_append_capped(output, value, item_size);
|
||||
|
||||
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||
_array_set_count(output, _array_capacity(output));
|
||||
}
|
||||
|
||||
RETURN_ARRAY_APPEND_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
|
||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||
ArrayInitFlags flags, u64 item_size) {
|
||||
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||
_array_validate(dst, item_size);
|
||||
_array_validate(src, item_size);
|
||||
@@ -139,7 +146,8 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
|
||||
u64 remaining_capacity = dst_header->capacity - dst_header->count;
|
||||
if (src_header->count >= remaining_capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity, dst_header->item_size, false);
|
||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
|
||||
flags, dst_header->item_size);
|
||||
if (!output) {
|
||||
output = dst;
|
||||
goto RETURN_ARRAY_EXTEND_ALLOC;
|
||||
@@ -149,11 +157,16 @@ GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, c
|
||||
|
||||
_array_extend_capped(output, src, item_size);
|
||||
|
||||
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||
_array_set_count(output, _array_capacity(output));
|
||||
}
|
||||
|
||||
RETURN_ARRAY_EXTEND_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size) {
|
||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||
ArrayInitFlags flags, u64 item_size) {
|
||||
wapp_runtime_assert(allocator != NULL && dst != NULL && src != NULL, "`allocator`, `dst` and `src` should not be NULL");
|
||||
_array_validate(dst, item_size);
|
||||
_array_validate(src, item_size);
|
||||
@@ -165,7 +178,7 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
|
||||
if (src_header->count >= dst_header->capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(dst_header->capacity * 2);
|
||||
output = (GenericArray )_array_alloc_capacity(allocator, new_capacity,
|
||||
src_header->item_size, false);
|
||||
flags, src_header->item_size);
|
||||
if (!output) {
|
||||
output = dst;
|
||||
goto RETURN_ARRAY_COPY_ALLOC;
|
||||
@@ -174,6 +187,10 @@ GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, con
|
||||
|
||||
_array_copy_capped(output, src, item_size);
|
||||
|
||||
if ((flags & ARRAY_INIT_FILLED) == ARRAY_INIT_FILLED) {
|
||||
_array_set_count(output, _array_capacity(output));
|
||||
}
|
||||
|
||||
RETURN_ARRAY_COPY_ALLOC:
|
||||
return output;
|
||||
}
|
||||
@@ -203,28 +220,45 @@ u64 _array_calc_alloc_size(u64 capacity, u64 item_size) {
|
||||
return sizeof(ArrayHeader) + item_size * capacity;
|
||||
}
|
||||
|
||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size,
|
||||
ArrayInitFlags flags) {
|
||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
|
||||
u64 item_size) {
|
||||
wapp_runtime_assert(allocator != NULL, "`allocator` should not be NULL");
|
||||
|
||||
GenericArray output = NULL;
|
||||
|
||||
u64 allocation_size = _array_calc_alloc_size(capacity, item_size);
|
||||
ArrayHeader *header = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!header) {
|
||||
void *buffer = wapp_mem_allocator_alloc(allocator, allocation_size);
|
||||
if (!buffer) {
|
||||
goto RETURN_ARRAY_ALLOC;
|
||||
}
|
||||
|
||||
output = (u8 *)(header + 1);
|
||||
header->magic = WAPP_ARRAY_MAGIC;
|
||||
header->count = flags & ARRAY_INIT_FILLED ? capacity : 0;
|
||||
header->capacity = capacity;
|
||||
header->item_size = item_size;
|
||||
output = _array_from_preallocated_buffer(buffer, allocation_size, flags, item_size);
|
||||
|
||||
RETURN_ARRAY_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
|
||||
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
|
||||
u64 item_size) {
|
||||
wapp_runtime_assert(buffer != NULL, "`buffer` should not be NULL");
|
||||
|
||||
i64 data_buffer_size = (i64)buffer_size - (i64)(sizeof(ArrayHeader));
|
||||
if (data_buffer_size <= 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
u64 item_capacity = (u64)data_buffer_size / item_size;
|
||||
ArrayHeader *header = (ArrayHeader *)buffer;
|
||||
GenericArray output = (u8 *)(header + 1);
|
||||
header->magic = WAPP_ARRAY_MAGIC;
|
||||
header->count = flags & ARRAY_INIT_FILLED ? item_capacity : 0;
|
||||
header->capacity = item_capacity;
|
||||
header->item_size = item_size;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
wapp_persist inline void _array_validate(const GenericArray array, u64 item_size) {
|
||||
ArrayHeader *header = _array_header(array);
|
||||
wapp_runtime_assert(WAPP_ARRAY_MAGIC == header->magic, "`array` is not a valid wapp array");
|
||||
|
||||
@@ -17,9 +17,6 @@ BEGIN_C_LINKAGE
|
||||
#define _calc_array_count(TYPE, ...) wapp_misc_utils_va_args_count(TYPE, __VA_ARGS__)
|
||||
#define _calc_array_capacity(TYPE, ...) wapp_misc_utils_u64_round_up_pow2(_calc_array_count(TYPE, __VA_ARGS__) * 2)
|
||||
|
||||
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
|
||||
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, sizeof(TYPE), FLAGS))
|
||||
|
||||
typedef struct Str8 Str8;
|
||||
|
||||
// NOTE (Abdelrahman): Typedefs to distinguish arrays from regular pointers
|
||||
@@ -138,40 +135,48 @@ typedef enum {
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_set(TYPE, ARRAY, INDEX, VALUE_PTR) \
|
||||
(_array_set((GenericArray)ARRAY, \
|
||||
INDEX, \
|
||||
(u8 *)VALUE_PTR, \
|
||||
sizeof(TYPE)))
|
||||
INDEX, \
|
||||
(u8 *)VALUE_PTR, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_append_capped(TYPE, ARRAY, VALUE_PTR) \
|
||||
(_array_append_capped((GenericArray)ARRAY, \
|
||||
(u8 *)VALUE_PTR, \
|
||||
sizeof(TYPE)))
|
||||
(u8 *)VALUE_PTR, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_extend_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
||||
(_array_extend_capped((GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_copy_capped(TYPE, DST_ARRAY, SRC_ARRAY) \
|
||||
(_array_copy_capped((GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR) \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, ARRAY, VALUE_PTR, FLAGS) \
|
||||
((TYPE *)_array_append_alloc(ALLOCATOR_PTR, \
|
||||
(GenericArray)ARRAY, \
|
||||
(u8 *)VALUE_PTR, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
||||
(GenericArray)ARRAY, \
|
||||
(u8 *)VALUE_PTR, \
|
||||
FLAGS, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_extend_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
|
||||
((TYPE *)_array_extend_alloc(ALLOCATOR_PTR, \
|
||||
(GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY) \
|
||||
(GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
FLAGS, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_copy_alloc(TYPE, ALLOCATOR_PTR, DST_ARRAY, SRC_ARRAY, FLAGS) \
|
||||
((TYPE *)_array_copy_alloc(ALLOCATOR_PTR, \
|
||||
(GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
(GenericArray)DST_ARRAY, \
|
||||
(GenericArray)SRC_ARRAY, \
|
||||
FLAGS, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_clear(TYPE, ARRAY) \
|
||||
(_array_clear((GenericArray)ARRAY, \
|
||||
sizeof(TYPE)))
|
||||
#define wapp_array_calc_alloc_size(TYPE, CAPACITY) _array_calc_alloc_size(CAPACITY, sizeof(TYPE))
|
||||
#define wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, FLAGS) \
|
||||
((TYPE *)_array_alloc_capacity(ALLOCATOR_PTR, CAPACITY, FLAGS, sizeof(TYPE)))
|
||||
#define wapp_array_from_preallcated_buffer(TYPE, BUFFER, BUFFER_SIZE) \
|
||||
((TYPE *)_array_from_preallcated_buffer(BUFFER, BUFFER_SIZE, sizeof(TYPE)))
|
||||
|
||||
|
||||
typedef struct header ArrayHeader;
|
||||
struct header {
|
||||
@@ -190,14 +195,19 @@ void _array_set(GenericArray array, u64 index, void *value, u64 item_siz
|
||||
void _array_append_capped(GenericArray array, void *value, u64 item_size);
|
||||
void _array_extend_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
||||
void _array_copy_capped(GenericArray dst, const GenericArray src, u64 item_size);
|
||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value, u64 item_size);
|
||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src, u64 item_size);
|
||||
GenericArray _array_append_alloc(const Allocator *allocator, GenericArray array, void *value,
|
||||
ArrayInitFlags flags, u64 item_size);
|
||||
GenericArray _array_extend_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||
ArrayInitFlags flags, u64 item_size);
|
||||
GenericArray _array_copy_alloc(const Allocator *allocator, GenericArray dst, const GenericArray src,
|
||||
ArrayInitFlags flags, u64 item_size);
|
||||
void *_array_pop(GenericArray array, u64 item_size);
|
||||
void _array_clear(GenericArray array, u64 item_size);
|
||||
u64 _array_calc_alloc_size(u64 capacity, u64 item_size);
|
||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, u64 item_size,
|
||||
ArrayInitFlags flags);
|
||||
GenericArray _array_alloc_capacity(const Allocator *allocator, u64 capacity, ArrayInitFlags flags,
|
||||
u64 item_size);
|
||||
GenericArray _array_from_preallocated_buffer(void *buffer, u64 buffer_size, ArrayInitFlags flags,
|
||||
u64 item_size);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
@@ -3,24 +3,83 @@
|
||||
#include "queue.h"
|
||||
#include "../array/array.h"
|
||||
#include "../../common/assert/assert.h"
|
||||
#include "../../common/misc/misc_utils.h"
|
||||
#include <string.h>
|
||||
|
||||
void _queue_pop_front(GenericQueue *queue, void *output, u64 item_size) {
|
||||
wapp_debug_assert(queue != NULL && output != NULL, "`queue` and `output` should not be NULL");
|
||||
void _queue_push(GenericQueue *queue, void *item, u64 item_size) {
|
||||
wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
|
||||
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
memcpy(output, queue->items, item_size);
|
||||
u64 capacity = wapp_array_capacity(queue->items);
|
||||
if (queue->count >= capacity) { return; }
|
||||
|
||||
// NOTE (Abdelrahman): Uses a while loop instead of a for loop to get rid of
|
||||
// MSVC Spectre mitigation warnings
|
||||
u64 new_count = wapp_array_count(queue->items) - 1;
|
||||
u64 index = 0;
|
||||
b8 running = index < new_count;
|
||||
while (running) {
|
||||
_array_set(queue->items, index, _array_get(queue->items, index + 1, item_size), item_size);
|
||||
++index;
|
||||
running = index < new_count;
|
||||
u64 index = (queue->back)++;
|
||||
_array_set(queue->items, index, item, item_size);
|
||||
++(queue->count);
|
||||
|
||||
if (queue->back >= capacity) {
|
||||
queue->back = 0;
|
||||
}
|
||||
}
|
||||
|
||||
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size) {
|
||||
wapp_debug_assert(allocator != NULL && queue != NULL && item != NULL,
|
||||
"`allocator`, `queue` and `item` should not be NULL");
|
||||
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
GenericQueue *output = queue;
|
||||
|
||||
u64 capacity = wapp_array_capacity(queue->items);
|
||||
if (queue->count >= capacity) {
|
||||
u64 new_capacity = wapp_misc_utils_u64_round_up_pow2(capacity * 2);
|
||||
u64 array_size = _array_calc_alloc_size(new_capacity, item_size);
|
||||
u64 alloc_size = sizeof(GenericQueue) + array_size;
|
||||
void *buffer = wapp_mem_allocator_alloc(allocator, alloc_size);
|
||||
if (!buffer) {
|
||||
goto RETURN_QUEUE_PUSH_ALLOC;
|
||||
}
|
||||
|
||||
memset((void *)buffer, 0, alloc_size);
|
||||
|
||||
output = (GenericQueue *)buffer;
|
||||
output->items = _array_from_preallocated_buffer((void *)(output + 1), array_size, ARRAY_INIT_FILLED, item_size);
|
||||
|
||||
// NOTE (Abdelrahman): When the queue is full, the front and back indices should
|
||||
// always be the same
|
||||
u64 front_count = capacity - queue->front;
|
||||
u64 back_count = queue->back;
|
||||
void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size));
|
||||
|
||||
memcpy(output->items, copy_boundary, front_count * item_size);
|
||||
if (back_count > 0) {
|
||||
void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size));
|
||||
memcpy(back_copy_dst, queue->items, back_count * item_size);
|
||||
}
|
||||
|
||||
output->front = 0;
|
||||
output->back = front_count + back_count;
|
||||
output->count = queue->count;
|
||||
}
|
||||
|
||||
wapp_array_set_count(queue->items, new_count);
|
||||
_queue_push(output, item, item_size);
|
||||
|
||||
RETURN_QUEUE_PUSH_ALLOC:
|
||||
return output;
|
||||
}
|
||||
|
||||
void *_queue_pop(GenericQueue *queue, u64 item_size) {
|
||||
wapp_debug_assert(queue != NULL, "`queue` should not be NULL");
|
||||
wapp_runtime_assert(item_size == wapp_array_item_size(queue->items), "Invalid type");
|
||||
|
||||
if (queue->count == 0) { return NULL; }
|
||||
|
||||
u64 index = (queue->front)++;
|
||||
--(queue->count);
|
||||
|
||||
u64 capacity = wapp_array_capacity(queue->items);
|
||||
if (queue->front >= capacity) {
|
||||
queue->front = 0;
|
||||
}
|
||||
|
||||
return _array_get(queue->items, index, item_size);
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#define QUEUE_H
|
||||
|
||||
#include "../array/array.h"
|
||||
#include "../mem/allocator/mem_allocator.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../common/platform/platform.h"
|
||||
|
||||
@@ -13,6 +14,9 @@ BEGIN_C_LINKAGE
|
||||
|
||||
typedef struct {
|
||||
GenericArray items;
|
||||
u64 front;
|
||||
u64 back;
|
||||
u64 count;
|
||||
} GenericQueue;
|
||||
|
||||
// NOTE (Abdelrahman): GenericQueue typedefs for readability
|
||||
@@ -38,41 +42,56 @@ typedef GenericQueue Str8Queue;
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
#define wapp_queue(TYPE, CAPACITY) ([&]() { \
|
||||
wapp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_NONE); \
|
||||
wapp_persist GenericQueue queue = {arr}; \
|
||||
wapp_persist GenericArray arr = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED); \
|
||||
wapp_persist GenericQueue queue = { \
|
||||
arr, \
|
||||
0, \
|
||||
0, \
|
||||
0, \
|
||||
}; \
|
||||
\
|
||||
return queue; \
|
||||
}())
|
||||
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ([&]() { \
|
||||
wapp_persist GenericQueue queue = { \
|
||||
wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_NONE) \
|
||||
wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED) \
|
||||
0, \
|
||||
0, \
|
||||
0, \
|
||||
}; \
|
||||
\
|
||||
return queue; \
|
||||
}())
|
||||
#else
|
||||
#define wapp_queue(TYPE, CAPACITY) ((GenericQueue){ \
|
||||
.items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_NONE) \
|
||||
.items = wapp_array_with_capacity(TYPE, CAPACITY, ARRAY_INIT_FILLED), \
|
||||
.front = 0, \
|
||||
.back = 0, \
|
||||
.count = 0, \
|
||||
})
|
||||
#define wapp_queue_alloc(TYPE, ALLOCATOR_PTR, CAPACITY) ((GenericQueue){ \
|
||||
.items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_NONE) \
|
||||
.items = wapp_array_alloc_capacity(TYPE, ALLOCATOR_PTR, CAPACITY, ARRAY_INIT_FILLED), \
|
||||
.front = 0, \
|
||||
.back = 0, \
|
||||
.count = 0, \
|
||||
})
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#define wapp_queue_count(QUEUE_PTR) (wapp_array_count((QUEUE_PTR)->items))
|
||||
#define wapp_queue_capacity(QUEUE_PTR) (wapp_array_capacity((QUEUE_PTR)->items))
|
||||
#define wapp_queue_item_size(QUEUE_PTR) (wapp_array_item_size((QUEUE_PTR)->items))
|
||||
#define wapp_queue_push_back(TYPE, QUEUE_PTR, VALUE_PTR) ( \
|
||||
wapp_array_append_capped(TYPE, (QUEUE_PTR)->items, VALUE_PTR) \
|
||||
#define wapp_queue_push(TYPE, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queue_push(QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
#define wapp_queue_push_back_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
|
||||
wapp_array_append_alloc(TYPE, ALLOCATOR_PTR, (QUEUE_PTR)->items, VALUE_PTR) \
|
||||
#define wapp_queue_push_alloc(TYPE, ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR) ( \
|
||||
_queue_push_alloc(ALLOCATOR_PTR, QUEUE_PTR, VALUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
#define wapp_queue_pop_front(TYPE, QUEUE_PTR, OUTPUT_PTR) ( \
|
||||
_queue_pop_front(QUEUE_PTR, OUTPUT_PTR, sizeof(TYPE)) \
|
||||
#define wapp_queue_pop(TYPE, QUEUE_PTR) ( \
|
||||
(TYPE *)_queue_pop(QUEUE_PTR, sizeof(TYPE)) \
|
||||
)
|
||||
|
||||
void _queue_pop_front(GenericQueue *queue, void *output, u64 item_size);
|
||||
void _queue_push(GenericQueue *queue, void *item, u64 item_size);
|
||||
GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *item, u64 item_size);
|
||||
void *_queue_pop(GenericQueue *queue, u64 item_size);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
|
||||
@@ -10,6 +10,12 @@
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../base/array/array.h"
|
||||
#include "../../../base/strings/str8/str8.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_APPLE
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
#define lseek64 lseek
|
||||
#endif // !WAPP_PLATFORM_APPLE
|
||||
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
@@ -166,7 +166,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
||||
I32Array array1 = wapp_array(i32, 1, 2, 3, 4, 5, 6, 7, 8);
|
||||
I32Array array2 = wapp_array(i32, 1, 2);
|
||||
|
||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}));
|
||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &((i32){10}), ARRAY_INIT_NONE);
|
||||
result = arr_ptr == array1;
|
||||
|
||||
u64 count = 4;
|
||||
@@ -174,7 +174,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
||||
b8 running = true;
|
||||
while (running) {
|
||||
i32 num = (i32)index;
|
||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
|
||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
|
||||
|
||||
++index;
|
||||
running = index < count;
|
||||
@@ -194,10 +194,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
|
||||
I32Array array2 = wapp_array(i32, 1, 2);
|
||||
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
||||
|
||||
I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3);
|
||||
I32Array arr_ptr = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
|
||||
result = arr_ptr == array1;
|
||||
|
||||
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3);
|
||||
arr_ptr = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
|
||||
result = result && arr_ptr != array2;
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&allocator);
|
||||
@@ -215,7 +215,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
||||
I32Array array = NULL;
|
||||
|
||||
u64 expected_count = 5;
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
|
||||
result = wapp_array_count(array) == expected_count && array == dst1;
|
||||
|
||||
u64 index = 0;
|
||||
@@ -228,7 +228,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
||||
}
|
||||
|
||||
expected_count = 5;
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
|
||||
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
||||
|
||||
index = 0;
|
||||
|
||||
@@ -169,7 +169,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
||||
I32Array array2 = wapp_array(i32, 1, 2);
|
||||
|
||||
i32 num = 10;
|
||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num);
|
||||
I32Array arr_ptr = wapp_array_append_alloc(i32, &allocator, array1, &num, ARRAY_INIT_NONE);
|
||||
result = arr_ptr == array1;
|
||||
|
||||
u64 count = 4;
|
||||
@@ -177,7 +177,7 @@ TestFuncResult test_i32_array_append_alloc(void) {
|
||||
b8 running = true;
|
||||
while (running) {
|
||||
num = (i32)index;
|
||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num);
|
||||
arr_ptr = wapp_array_append_alloc(i32, &allocator, array2, &num, ARRAY_INIT_NONE);
|
||||
|
||||
++index;
|
||||
running = index < count;
|
||||
@@ -197,10 +197,10 @@ TestFuncResult test_i32_array_extend_alloc(void) {
|
||||
I32Array array2 = wapp_array(i32, 1, 2);
|
||||
I32Array array3 = wapp_array(i32, 1, 2, 3, 4);
|
||||
|
||||
I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3);
|
||||
I32Array array = wapp_array_extend_alloc(i32, &allocator, array1, array3, ARRAY_INIT_NONE);
|
||||
result = array == array1;
|
||||
|
||||
array = wapp_array_extend_alloc(i32, &allocator, array2, array3);
|
||||
array = wapp_array_extend_alloc(i32, &allocator, array2, array3, ARRAY_INIT_NONE);
|
||||
result = result && array != array2;
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&allocator);
|
||||
@@ -218,7 +218,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
||||
I32Array array = nullptr;
|
||||
|
||||
u64 expected_count = 5;
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src);
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst1, src, ARRAY_INIT_NONE);
|
||||
result = wapp_array_count(array) == expected_count && array == dst1;
|
||||
|
||||
u64 index = 0;
|
||||
@@ -231,7 +231,7 @@ TestFuncResult test_i32_array_copy_alloc(void) {
|
||||
}
|
||||
|
||||
expected_count = 5;
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src);
|
||||
array = wapp_array_copy_alloc(i32, &allocator, dst2, src, ARRAY_INIT_NONE);
|
||||
result = result && wapp_array_count(array) == expected_count && array != dst2;
|
||||
|
||||
index = 0;
|
||||
|
||||
@@ -3,61 +3,96 @@
|
||||
#include "wapp.h"
|
||||
#include "test_queue.h"
|
||||
|
||||
wapp_persist Allocator arena = {0};
|
||||
wapp_persist I32Queue queue;
|
||||
#define CAPACITY 8
|
||||
|
||||
TestFuncResult test_queue_push_back(void) {
|
||||
arena = wapp_mem_arena_allocator_init(MB(64));
|
||||
TestFuncResult test_queue_push(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
queue = wapp_queue_alloc(i32, &arena, 64);
|
||||
|
||||
result = result && queue.items != NULL && wapp_queue_capacity(&queue) == 64 && wapp_queue_count(&queue) == 0;
|
||||
|
||||
i32 n1 = 1;
|
||||
i32 n2 = 2;
|
||||
i32 n3 = 3;
|
||||
i32 n4 = 4;
|
||||
i32 n5 = 5;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n1);
|
||||
result = result && wapp_queue_count(&queue) == 1;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n2);
|
||||
result = result && wapp_queue_count(&queue) == 2;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n3);
|
||||
result = result && wapp_queue_count(&queue) == 3;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n4);
|
||||
result = result && wapp_queue_count(&queue) == 4;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n5);
|
||||
result = result && wapp_queue_count(&queue) == 5;
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = ((i32 *)(queue.items)) + i;
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_queue_pop_front(void) {
|
||||
TestFuncResult test_queue_push_alloc(void) {
|
||||
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
i32 num;
|
||||
i32 item = 8;
|
||||
u64 new_capacity = CAPACITY * 2;
|
||||
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
u64 capacity = wapp_queue_capacity(&queue);
|
||||
result = result && capacity == new_capacity;
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 1;
|
||||
for (u64 i = 0; i < 2; ++i) {
|
||||
wapp_queue_pop(i32, &queue);
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 2;
|
||||
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||
for (u64 i = 0; i < remaining; ++i) {
|
||||
item = remaining + i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 3;
|
||||
++item;
|
||||
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 4;
|
||||
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 5;
|
||||
i32 *arr = (i32 *)queue.items;
|
||||
for (u64 i = 0; i < queue.count; ++i) {
|
||||
// NOTE (Abdelrahman): First queue value is currently 2
|
||||
result = result && arr[i] == (i32)(2 + i);
|
||||
}
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&arena);
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_queue_pop(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
u64 half_count = queue.count / 2;
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 item = (i32)i + CAPACITY;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||
}
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
@@ -1,61 +1,96 @@
|
||||
#include "wapp.h"
|
||||
#include "test_queue.h"
|
||||
|
||||
wapp_persist Allocator arena = {};
|
||||
wapp_persist I32Queue queue;
|
||||
#define CAPACITY 8
|
||||
|
||||
TestFuncResult test_queue_push_back(void) {
|
||||
arena = wapp_mem_arena_allocator_init(MB(64));
|
||||
TestFuncResult test_queue_push(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
queue = wapp_queue_alloc(i32, &arena, 64);
|
||||
|
||||
result = result && queue.items != NULL && wapp_queue_capacity(&queue) == 64 && wapp_queue_count(&queue) == 0;
|
||||
|
||||
i32 n1 = 1;
|
||||
i32 n2 = 2;
|
||||
i32 n3 = 3;
|
||||
i32 n4 = 4;
|
||||
i32 n5 = 5;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n1);
|
||||
result = result && wapp_queue_count(&queue) == 1;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n2);
|
||||
result = result && wapp_queue_count(&queue) == 2;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n3);
|
||||
result = result && wapp_queue_count(&queue) == 3;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n4);
|
||||
result = result && wapp_queue_count(&queue) == 4;
|
||||
|
||||
wapp_queue_push_back(i32, &queue, &n5);
|
||||
result = result && wapp_queue_count(&queue) == 5;
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = ((i32 *)(queue.items)) + i;
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_queue_pop_front(void) {
|
||||
TestFuncResult test_queue_push_alloc(void) {
|
||||
Allocator arena = wapp_mem_arena_allocator_init(MiB(64));
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
i32 num;
|
||||
i32 item = 8;
|
||||
u64 new_capacity = CAPACITY * 2;
|
||||
I32Queue *new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
u64 capacity = wapp_queue_capacity(&queue);
|
||||
result = result && capacity == new_capacity;
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 1;
|
||||
for (u64 i = 0; i < 2; ++i) {
|
||||
wapp_queue_pop(i32, &queue);
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 2;
|
||||
u64 remaining = wapp_queue_capacity(&queue) - queue.count;
|
||||
for (u64 i = 0; i < remaining; ++i) {
|
||||
item = remaining + i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 3;
|
||||
++item;
|
||||
new_queue = wapp_queue_push_alloc(i32, &arena, &queue, &item);
|
||||
if (new_queue && new_queue != &queue) {
|
||||
queue = *new_queue;
|
||||
}
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 4;
|
||||
result = result && wapp_queue_capacity(&queue) == new_capacity * 2;
|
||||
|
||||
wapp_queue_pop_front(i32, &queue, &num);
|
||||
result = result && num == 5;
|
||||
i32 *arr = (i32 *)queue.items;
|
||||
for (u64 i = 0; i < queue.count; ++i) {
|
||||
// NOTE (Abdelrahman): First queue value is currently 2
|
||||
result = result && arr[i] == (i32)(2 + i);
|
||||
}
|
||||
|
||||
wapp_mem_arena_allocator_destroy(&arena);
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
TestFuncResult test_queue_pop(void) {
|
||||
I32Queue queue = wapp_queue(i32, CAPACITY);
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 item = (i32)i;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
b8 result = true;
|
||||
u64 half_count = queue.count / 2;
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)i;
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < half_count; ++i) {
|
||||
i32 item = (i32)i + CAPACITY;
|
||||
wapp_queue_push(i32, &queue, &item);
|
||||
}
|
||||
|
||||
for (u64 i = 0; i < CAPACITY; ++i) {
|
||||
i32 *value = wapp_queue_pop(i32, &queue);
|
||||
result = result && value != NULL && *value == (i32)half_count + (i32)i;
|
||||
}
|
||||
|
||||
return wapp_tester_result(result);
|
||||
}
|
||||
|
||||
@@ -5,7 +5,8 @@
|
||||
|
||||
#include "wapp.h"
|
||||
|
||||
TestFuncResult test_queue_push_back(void);
|
||||
TestFuncResult test_queue_pop_front(void);
|
||||
TestFuncResult test_queue_push(void);
|
||||
TestFuncResult test_queue_push_alloc(void);
|
||||
TestFuncResult test_queue_pop(void);
|
||||
|
||||
#endif // !TEST_QUEUE_H
|
||||
|
||||
@@ -44,8 +44,9 @@ int main(void) {
|
||||
test_i32_array_copy_alloc,
|
||||
test_i32_array_pop,
|
||||
test_i32_array_clear,
|
||||
test_queue_push_back,
|
||||
test_queue_pop_front,
|
||||
test_queue_push,
|
||||
test_queue_push_alloc,
|
||||
test_queue_pop,
|
||||
test_str8_lit,
|
||||
test_str8_lit_ro,
|
||||
test_str8_buf,
|
||||
|
||||
@@ -44,8 +44,9 @@ int main(void) {
|
||||
test_i32_array_copy_alloc,
|
||||
test_i32_array_pop,
|
||||
test_i32_array_clear,
|
||||
test_queue_push_back,
|
||||
test_queue_pop_front,
|
||||
test_queue_push,
|
||||
test_queue_push_alloc,
|
||||
test_queue_pop,
|
||||
test_str8_lit,
|
||||
test_str8_lit_ro,
|
||||
test_str8_buf,
|
||||
|
||||
Reference in New Issue
Block a user