diff --git a/Include/Fledasty/Core/Queue.h b/Include/Fledasty/Core/Queue.h index 70a3c9e..b0e6a34 100644 --- a/Include/Fledasty/Core/Queue.h +++ b/Include/Fledasty/Core/Queue.h @@ -40,4 +40,6 @@ typedef struct { FledastyError fledasty_queue_initialize(FledastyQueue *new_queue); FledastyError fledasty_queue_destroy(FledastyQueue *current_queue); +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size); + #endif \ No newline at end of file diff --git a/Include/Fledasty/Utils/Error.h b/Include/Fledasty/Utils/Error.h index c981843..91f5dc7 100644 --- a/Include/Fledasty/Utils/Error.h +++ b/Include/Fledasty/Utils/Error.h @@ -25,7 +25,7 @@ typedef enum { FLEDASTY_ERROR_NONE = 0, - FLEDASTY_ERROR_ALLOCATION = 1, + FLEDASTY_ERROR_FAILED_ALLOCATION = 1, FLEDASTY_ERROR_INVALID_POINTER = 2, } FledastyError; diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index b9108af..6f3a41a 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -23,6 +23,7 @@ #include #include +#include FledastyError fledasty_queue_initialize(FledastyQueue *new_queue) { if (new_queue == NULL) { @@ -49,12 +50,12 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { HallocyError error = hallocy_free(previousNode->value); if (error != HALLOCY_ERROR_NONE) { - return FLEDASTY_ERROR_ALLOCATION; + return FLEDASTY_ERROR_FAILED_ALLOCATION; } error = hallocy_free(previousNode); if (error != HALLOCY_ERROR_NONE) { - return FLEDASTY_ERROR_ALLOCATION; + return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -62,5 +63,35 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { current_queue->start = NULL; current_queue->end = NULL; + return FLEDASTY_ERROR_NONE; +} + +FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value, const size_t size) { + if (current_queue == NULL || value == NULL) { + return FLEDASTY_ERROR_INVALID_POINTER; + } + + FledastyQueueNode *new_queue_node = (FledastyQueueNode*)hallocy_malloc(sizeof(FledastyQueueNode)); + if (new_queue_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + new_queue_node->value = (FledastyQueueNode*)hallocy_malloc(size); + hallocy_copy_memory(new_queue_node->value, value, size); + + new_queue_node->next = NULL; + if (current_queue->start == NULL) { + current_queue->start = new_queue_node; + current_queue->end = new_queue_node; + + new_queue_node->previous = NULL; + } else { + new_queue_node->previous = current_queue->end; + + current_queue->end->next = new_queue_node; + current_queue->end = new_queue_node; + } + + current_queue->size += 1; return FLEDASTY_ERROR_NONE; } \ No newline at end of file diff --git a/Tests/Main.c b/Tests/Main.c index d2da10b..4a0fac6 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -25,6 +25,18 @@ int main() { FledastyQueue test_queue; fledasty_queue_initialize(&test_queue); + + int value = 10; + for (int i = 0; i < 10; i += 1) { + fledasty_queue_push(&test_queue, &i, sizeof(int)); + } + + FledastyQueueNode *current_node = test_queue.start; + for (int i = 0; i < test_queue.size; i++) { + printf("Element %d\n", *((int*)current_node->value)); + current_node = current_node->next; + } + fledasty_queue_destroy(&test_queue); printf("Done\n");