refactor(doubly linked list): changed method used for freeing memory on error

This commit is contained in:
Mineplay 2025-05-20 07:07:04 -05:00
parent 4134d55492
commit 18c7f70fca

View file

@ -26,7 +26,7 @@
#include <Hallocy/Core/Allocator.h>
#include <Hallocy/Core/Memory.h>
#include <Hallocy/Utils/Error.h>
#include <stdio.h>
FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *new_doubly_linked_list, void *values, const size_t values_size, const size_t element_byte_size) {
if (new_doubly_linked_list == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
@ -55,20 +55,22 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n
new_doubly_linked_list->start->previous = NULL;
new_doubly_linked_list->start->next = NULL;
FledastyDoublyLinkedListNode *last_node = new_doubly_linked_list->start;
for (size_t index = new_doubly_linked_list->element_byte_size; index < new_doubly_linked_list->size * new_doubly_linked_list->element_byte_size; index += new_doubly_linked_list->element_byte_size) {
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_node == NULL) {
FledastyDoublyLinkedListNode *previous_node = NULL;
FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
while (current_node->next != NULL) {
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
hallocy_free(current_node->previous->value);
hallocy_free(current_node->previous);
}
hallocy_free(current_node->value);
hallocy_free(current_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
@ -76,16 +78,17 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n
if (new_node->value == NULL) {
hallocy_free(new_node);
FledastyDoublyLinkedListNode *previous_node = NULL;
FledastyDoublyLinkedListNode *current_node = new_doubly_linked_list->start;
while (current_node != NULL) {
previous_node = current_node;
while (current_node->next != NULL) {
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
hallocy_free(current_node->previous->value);
hallocy_free(current_node);
}
hallocy_free(current_node->value);
hallocy_free(current_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}