From c24d41f41920c78c857f90d3e80f5eb41bdc1795 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 24 Apr 2025 04:49:52 -0500 Subject: [PATCH] fix(queue): fixed reallocation issue and made init of queue consistent with stack --- Include/Fledasty/Core/Queue.h | 2 +- Src/Core/Queue.c | 40 +++++++++++++++++++++++------------ Src/Core/Stack.c | 2 +- Tests/Main.c | 2 +- 4 files changed, 29 insertions(+), 17 deletions(-) diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index 762262f..2775230 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -35,7 +35,7 @@ typedef struct { unsigned char *buffer; } FledastyQueue; -FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size); +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, const size_t element_byte_size); FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value); diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index 37d44c1..f06898e 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -26,23 +26,35 @@ #include #include -FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, const size_t element_byte_size) { +FledastyError fledasty_queue_initialize(FledastyQueue *new_queue, void *values, const size_t values_size, const size_t element_byte_size) { if (new_queue == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - new_queue->size = 0; - new_queue->capacity = 10; + new_queue->head = 0; new_queue->element_byte_size = element_byte_size; + if (values == NULL || values_size == 0) { + new_queue->size = 0; + new_queue->capacity = 10; - new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size); - if (new_queue->buffer == NULL) { - return FLEDASTY_ERROR_FAILED_ALLOCATION; + new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size); + if (new_queue->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } else { + new_queue->size = values_size; + new_queue->capacity = values_size + (values_size / 2); + + new_queue->buffer = (unsigned char*)hallocy_malloc(new_queue->capacity * element_byte_size); + if (new_queue->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_queue->buffer, values, values_size * element_byte_size); } - new_queue->head = 0; - new_queue->tail = 0; - + new_queue->tail = new_queue->size; + return FLEDASTY_ERROR_NONE; } @@ -66,15 +78,16 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { } if (current_queue->size == current_queue->capacity) { - current_queue->capacity = current_queue->capacity + (current_queue->capacity / 2); + current_queue->capacity += current_queue->capacity; current_queue->buffer = (unsigned char*)hallocy_realloc(current_queue->buffer, current_queue->capacity * current_queue->element_byte_size); if (current_queue->buffer == NULL) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - size_t new_head = current_queue->capacity - current_queue->head; - hallocy_move_memory(current_queue->buffer + (current_queue->head * current_queue->element_byte_size), current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->size - current_queue->tail); + size_t head_length = current_queue->size - current_queue->head; + size_t new_head = current_queue->capacity - head_length; + hallocy_move_memory(current_queue->buffer + (new_head * current_queue->element_byte_size), current_queue->buffer + (current_queue->head * current_queue->element_byte_size), head_length * current_queue->element_byte_size); current_queue->head = new_head; } @@ -105,10 +118,9 @@ void *fledasty_queue_pop(FledastyQueue *current_queue) { void *value_address = current_queue->buffer + (current_queue->head * current_queue->element_byte_size); current_queue->size -= 1; + current_queue->head += 1; if (current_queue->head == current_queue->capacity) { current_queue->head = 0; - } else { - current_queue->head += 1; } return value_address; diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index f59775c..b35b517 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -75,7 +75,7 @@ FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value) { } if (current_stack->size == current_stack->capacity) { - current_stack->capacity = current_stack->capacity + (current_stack->capacity / 2); + current_stack->capacity += current_stack->capacity; current_stack->buffer = (unsigned char*)hallocy_realloc(current_stack->buffer, current_stack->capacity); if (current_stack->buffer == NULL) { diff --git a/Tests/Main.c b/Tests/Main.c index 8a4b193..383bf48 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,7 +25,7 @@ int main() { FledastyQueue test_queue; - fledasty_queue_initialize(&test_queue, sizeof(int)); + fledasty_queue_initialize(&test_queue, (int[]){11, 12, 13, 14, 15}, 5, sizeof(int)); for (int i = 0; i < 10; i += 1) { fledasty_queue_push(&test_queue, &i);