Update queue implementation to use ring buffer
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user