refactor(doubly linked list): improved allocation error handling

This commit is contained in:
Mineplay 2025-05-20 05:23:25 -05:00
parent 827723e725
commit 4134d55492

View file

@ -41,16 +41,54 @@ FledastyError fledasty_doubly_linked_list_initialize(FledastyDoublyLinkedList *n
new_doubly_linked_list->size = values_size;
new_doubly_linked_list->start = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_doubly_linked_list->start == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_doubly_linked_list->start->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
if (new_doubly_linked_list->start->value == NULL) {
hallocy_free(new_doubly_linked_list->start);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_doubly_linked_list->start->value, values, new_doubly_linked_list->element_byte_size);
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;
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
}
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(new_doubly_linked_list->element_byte_size);
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;
current_node = current_node->next;
hallocy_free(previous_node->value);
hallocy_free(previous_node);
}
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_node->value, (unsigned char*)values + index, new_doubly_linked_list->element_byte_size);
new_node->previous = last_node;
@ -77,13 +115,11 @@ FledastyError fledasty_doubly_list_destroy(FledastyDoublyLinkedList *current_dou
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;
}
}
@ -97,8 +133,16 @@ FledastyError fledasty_doubly_linked_list_append(FledastyDoublyLinkedList *curre
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
new_node->previous = current_doubly_linked_list->end;
@ -121,8 +165,16 @@ FledastyError fledasty_doubly_linked_list_insert_at_index(FledastyDoublyLinkedLi
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
if (new_node->value) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (index == 0) {
@ -177,8 +229,16 @@ FledastyError fledasty_doubly_linked_list_insert_before_value(FledastyDoublyLink
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (current_node->previous == NULL) {
@ -214,8 +274,16 @@ FledastyError fledasty_doubly_linked_list_insert_after_value(FledastyDoublyLinke
}
FledastyDoublyLinkedListNode *new_node = (FledastyDoublyLinkedListNode*)hallocy_malloc(sizeof(FledastyDoublyLinkedListNode));
if (new_node == NULL) {
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
new_node->value = hallocy_malloc(current_doubly_linked_list->element_byte_size);
if (new_node->value == NULL) {
hallocy_free(new_node);
return FLEDASTY_ERROR_FAILED_ALLOCATION;
}
hallocy_copy_memory(new_node->value, value, current_doubly_linked_list->element_byte_size);
if (current_node == current_doubly_linked_list->end) {
@ -273,8 +341,13 @@ FledastyError fledasty_doubly_linked_list_remove_at_index(FledastyDoublyLinkedLi
}
}
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_doubly_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
@ -309,8 +382,13 @@ FledastyError fledasty_doubly_linked_list_remove_value(FledastyDoublyLinkedList
}
}
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_doubly_linked_list->size -= 1;
return FLEDASTY_ERROR_NONE;
@ -327,13 +405,11 @@ FledastyError fledasty_doubly_linked_list_clear(FledastyDoublyLinkedList *curren
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;
}
}