refactor(linked list): improved allocation error handling

This commit is contained in:
Mineplay 2025-05-15 13:53:02 -05:00
parent ca59eec627
commit 4cfd56bcea
3 changed files with 73 additions and 27 deletions

View file

@ -22,6 +22,7 @@
* -----------------------------------------------------------------------------
*/
#include "../../Include/Fledasty/Core/LinkedList.h"
#include "Fledasty/Utils/Error.h"
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
@ -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;
}
}

View file

@ -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;
}

View file

@ -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;
}