From 4cfd56bcea0551ef0d0b205edc83f01e540d06a6 Mon Sep 17 00:00:00 2001 From: Mineplay Date: Thu, 15 May 2025 13:53:02 -0500 Subject: [PATCH] refactor(linked list): improved allocation error handling --- Src/Core/LinkedList.c | 92 ++++++++++++++++++++++++++++++++----------- Src/Core/Queue.c | 5 +-- Src/Core/Stack.c | 3 +- 3 files changed, 73 insertions(+), 27 deletions(-) diff --git a/Src/Core/LinkedList.c b/Src/Core/LinkedList.c index 76d90e1..905d5b9 100644 --- a/Src/Core/LinkedList.c +++ b/Src/Core/LinkedList.c @@ -22,6 +22,7 @@ * ----------------------------------------------------------------------------- */ #include "../../Include/Fledasty/Core/LinkedList.h" +#include "Fledasty/Utils/Error.h" #include #include @@ -41,14 +42,30 @@ FledastyError fledasty_linked_list_initialize(FledastyLinkedList *new_linked_lis new_linked_list->size = values_size; new_linked_list->start = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + if (new_linked_list->start == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_linked_list->start->value = hallocy_malloc(new_linked_list->element_byte_size); + if (new_linked_list->start->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(new_linked_list->start->value, values, new_linked_list->element_byte_size); new_linked_list->start->next = NULL; FledastyLinkedListNode *last_node = new_linked_list->start; for (size_t index = new_linked_list->element_byte_size; index < new_linked_list->size * new_linked_list->element_byte_size; index += new_linked_list->element_byte_size) { FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); + if (new_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_node->value = hallocy_malloc(new_linked_list->element_byte_size); + if (new_node->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_linked_list->element_byte_size); new_node->next = NULL; @@ -73,13 +90,11 @@ FledastyError fledasty_linked_list_destroy(FledastyLinkedList *current_linked_li previous_node = current_node; current_node = current_node->next; - HallocyError result = hallocy_free(previous_node->value); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(previous_node->value) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - result = hallocy_free(previous_node); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } @@ -93,8 +108,15 @@ FledastyError fledasty_linked_list_append(FledastyLinkedList *current_linked_lis } FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); - + if (new_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + if (new_node->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); new_node->next = NULL; @@ -115,10 +137,16 @@ FledastyError fledasty_linked_list_insert_at_index(FledastyLinkedList *current_l } FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); - - new_node->value = hallocy_malloc(current_linked_list->element_byte_size); - hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + if (new_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + if (new_node->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); if (index == 0) { new_node->next = current_linked_list->start; current_linked_list->start = new_node; @@ -157,10 +185,16 @@ FledastyError fledasty_linked_list_insert_before_value(FledastyLinkedList *curre } FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); - - new_node->value = hallocy_malloc(current_linked_list->element_byte_size); - hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + if (new_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + if (new_node->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); if (previous_node == NULL) { new_node->next = current_linked_list->start; current_linked_list->start = new_node; @@ -188,10 +222,16 @@ FledastyError fledasty_linked_list_insert_after_value(FledastyLinkedList *curren } FledastyLinkedListNode *new_node = (FledastyLinkedListNode*)hallocy_malloc(sizeof(FledastyLinkedListNode)); - - new_node->value = hallocy_malloc(current_linked_list->element_byte_size); - hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); + if (new_node == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + new_node->value = hallocy_malloc(current_linked_list->element_byte_size); + if (new_node->value == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_node->value, value, current_linked_list->element_byte_size); if (current_node == current_linked_list->end) { new_node->next = NULL; @@ -232,8 +272,13 @@ FledastyError fledasty_linked_list_remove_at_index(FledastyLinkedList *current_l previous_node->next = current_node->next; } - hallocy_free(current_node->value); - hallocy_free(current_node); + if (hallocy_free(current_node->value) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; @@ -265,8 +310,13 @@ FledastyError fledasty_linked_list_remove_value(FledastyLinkedList *current_link previous_node->next = current_node->next; } - hallocy_free(current_node->value); - hallocy_free(current_node); + if (hallocy_free(current_node->value) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + if (hallocy_free(current_node) != HALLOCY_ERROR_NONE) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } current_linked_list->size -= 1; return FLEDASTY_ERROR_NONE; @@ -283,13 +333,11 @@ FledastyError fledasty_linked_list_clear(FledastyLinkedList *current_linked_list previous_node = current_node; current_node = current_node->next; - HallocyError result = hallocy_free(previous_node->value); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(previous_node->value) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } - result = hallocy_free(previous_node); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(previous_node) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } } diff --git a/Src/Core/Queue.c b/Src/Core/Queue.c index 5070b4b..6b28ffd 100644 --- a/Src/Core/Queue.c +++ b/Src/Core/Queue.c @@ -63,8 +63,7 @@ FledastyError fledasty_queue_destroy(FledastyQueue *current_queue) { return FLEDASTY_ERROR_INVALID_POINTER; } - HallocyError result = hallocy_free(current_queue->buffer); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(current_queue->buffer) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; } @@ -79,8 +78,8 @@ FledastyError fledasty_queue_push(FledastyQueue *current_queue, void *value) { if (current_queue->size == current_queue->capacity) { current_queue->capacity += current_queue->capacity; - current_queue->buffer = (unsigned char*)hallocy_realloc(current_queue->buffer, current_queue->capacity * current_queue->element_byte_size); + 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; } diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index 6b9096b..30e2058 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -61,8 +61,7 @@ FledastyError fledasty_stack_destroy(FledastyStack *current_stack) { return FLEDASTY_ERROR_INVALID_POINTER; } - HallocyError result = hallocy_free(current_stack->buffer); - if (result != HALLOCY_ERROR_NONE) { + if (hallocy_free(current_stack->buffer) != HALLOCY_ERROR_NONE) { return FLEDASTY_ERROR_FAILED_ALLOCATION; }