Sun, 21 May 2023 14:56:10 +0200
fix inconsistent destructor requirements for list and map classes
src/array_list.c | file | annotate | diff | comparison | revisions | |
src/cx/list.h | file | annotate | diff | comparison | revisions | |
src/linked_list.c | file | annotate | diff | comparison | revisions | |
src/list.c | file | annotate | diff | comparison | revisions |
1.1 --- a/src/array_list.c Sun May 21 14:40:05 2023 +0200 1.2 +++ b/src/array_list.c Sun May 21 14:56:10 2023 +0200 1.3 @@ -169,7 +169,24 @@ 1.4 1.5 static void cx_arl_destructor(struct cx_list_s *list) { 1.6 cx_array_list *arl = (cx_array_list *) list; 1.7 + 1.8 + char *ptr = arl->data; 1.9 + 1.10 + if (list->simple_destructor) { 1.11 + for (size_t i = 0; i < list->size; i++) { 1.12 + cx_invoke_simple_destructor(list, ptr); 1.13 + ptr += list->item_size; 1.14 + } 1.15 + } 1.16 + if (list->advanced_destructor) { 1.17 + for (size_t i = 0; i < list->size; i++) { 1.18 + cx_invoke_advanced_destructor(list, ptr); 1.19 + ptr += list->item_size; 1.20 + } 1.21 + } 1.22 + 1.23 cxFree(list->allocator, arl->data); 1.24 + cxFree(list->allocator, list); 1.25 } 1.26 1.27 static size_t cx_arl_insert_array(
2.1 --- a/src/cx/list.h Sun May 21 14:40:05 2023 +0200 2.2 +++ b/src/cx/list.h Sun May 21 14:56:10 2023 +0200 2.3 @@ -70,6 +70,9 @@ 2.4 struct cx_list_class_s { 2.5 /** 2.6 * Destructor function. 2.7 + * 2.8 + * Implementations SHALL invoke the content destructor functions if provided 2.9 + * and SHALL deallocate the list memory, if an allocator is provided. 2.10 */ 2.11 void (*destructor)(struct cx_list_s *list); 2.12
3.1 --- a/src/linked_list.c Sun May 21 14:40:05 2023 +0200 3.2 +++ b/src/linked_list.c Sun May 21 14:56:10 2023 +0200 3.3 @@ -876,11 +876,13 @@ 3.4 3.5 cx_linked_list_node *node = ll->begin; 3.6 while (node) { 3.7 + cx_invoke_destructor(list, node->payload); 3.8 void *next = node->next; 3.9 cxFree(list->allocator, node); 3.10 node = next; 3.11 } 3.12 - // do not free the list pointer, this is just a destructor! 3.13 + 3.14 + cxFree(list->allocator, list); 3.15 } 3.16 3.17 static cx_list_class cx_linked_list_class = {
4.1 --- a/src/list.c Sun May 21 14:40:05 2023 +0200 4.2 +++ b/src/list.c Sun May 21 14:56:10 2023 +0200 4.3 @@ -273,25 +273,7 @@ 4.4 // </editor-fold> 4.5 4.6 void cxListDestroy(CxList *list) { 4.7 - if (list->simple_destructor) { 4.8 - CxIterator iter = cxListIterator(list); 4.9 - cx_foreach(void*, elem, iter) { 4.10 - // already correctly resolved pointer - immediately invoke dtor 4.11 - list->simple_destructor(elem); 4.12 - } 4.13 - } 4.14 - if (list->advanced_destructor) { 4.15 - CxIterator iter = cxListIterator(list); 4.16 - cx_foreach(void*, elem, iter) { 4.17 - // already correctly resolved pointer - immediately invoke dtor 4.18 - list->advanced_destructor(list->destructor_data, elem); 4.19 - } 4.20 - } 4.21 - 4.22 list->cl->destructor(list); 4.23 - if (list->allocator) { 4.24 - cxFree(list->allocator, list); 4.25 - } 4.26 } 4.27 4.28 int cxListCompare(