From 515493b96349df2d58597311ddd86d074ff5a2fa Mon Sep 17 00:00:00 2001 From: Abdelrahman Said Date: Sun, 17 May 2026 12:20:47 +0100 Subject: [PATCH] Fix queue MSVC errors --- src/base/queue/queue.c | 27 +++++++++++++++++++++++++-- tests/queue/test_queue.c | 2 +- tests/queue/test_queue.cc | 2 +- 3 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/base/queue/queue.c b/src/base/queue/queue.c index 2527248..b6cb0bb 100644 --- a/src/base/queue/queue.c +++ b/src/base/queue/queue.c @@ -30,7 +30,10 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, GenericQueue *output = queue; u64 capacity = wapp_array_capacity(queue->items); - if (queue->count >= capacity) { + + // NOTE (Abdelrahman): Extracted into variable to fix MSVC error + b8 queue_full = queue->count >= capacity; + if (queue_full) { 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; @@ -51,7 +54,27 @@ GenericQueue *_queue_push_alloc(const Allocator *allocator, GenericQueue *queue, void *copy_boundary = (void *)((uptr)(queue->items) + (queue->front * item_size)); memcpy(output->items, copy_boundary, front_count * item_size); - if (back_count > 0) { + + /** + * NOTE (Abdelrahman): Since this is a ring buffer, the elements at the beginning of the array + * aren't always the ones at the front of the queue. When that's the case, the memcpy above + * will only copy a subset of the elements. This is why we need to copy the remaining ones. + * + * Example: Take a queue that looks like this with a capacity of 5 elements + * + * 0 1 | 2 3 4 + * ---------------|----------------------- + * | * | * | * | * | * | + * ---------------|----------------------- + * | + * queue_front = 2 + * queue_back = 2 + * + * In this case, the first memcpy will only copy elements 2-4. The memcpy below will be + * responsible for copying elements 0-1. + */ + b8 items_left_to_copy = back_count > 0; + if (items_left_to_copy) { void *back_copy_dst = (void *)((uptr)(output->items) + (front_count * item_size)); memcpy(back_copy_dst, queue->items, back_count * item_size); } diff --git a/tests/queue/test_queue.c b/tests/queue/test_queue.c index 5dbe5df..1370f2d 100644 --- a/tests/queue/test_queue.c +++ b/tests/queue/test_queue.c @@ -47,7 +47,7 @@ TestFuncResult test_queue_push_alloc(void) { u64 remaining = wapp_queue_capacity(&queue) - queue.count; for (u64 i = 0; i < remaining; ++i) { - item = remaining + i; + item = (i32)(remaining + i); wapp_queue_push(i32, &queue, &item); } diff --git a/tests/queue/test_queue.cc b/tests/queue/test_queue.cc index 860801d..b6a2092 100644 --- a/tests/queue/test_queue.cc +++ b/tests/queue/test_queue.cc @@ -45,7 +45,7 @@ TestFuncResult test_queue_push_alloc(void) { u64 remaining = wapp_queue_capacity(&queue) - queue.count; for (u64 i = 0; i < remaining; ++i) { - item = remaining + i; + item = (i32)(remaining + i); wapp_queue_push(i32, &queue, &item); }