1.1 --- a/src/linked_list.c Tue Feb 15 19:07:14 2022 +0100 1.2 +++ b/src/linked_list.c Tue Feb 15 19:41:48 2022 +0100 1.3 @@ -757,24 +757,35 @@ 1.4 cx_pll_iterator, 1.5 }; 1.6 1.7 +static CxList *cx_ll_default_destructor(CxList *list) { 1.8 + cx_linked_list *ll = (cx_linked_list *) list; 1.9 + 1.10 + cx_linked_list_node *node = ll->begin; 1.11 + while (node) { 1.12 + void *next = node->next; 1.13 + cxFree(list->allocator, node); 1.14 + node = next; 1.15 + } 1.16 + 1.17 + cxFree(list->allocator, list); 1.18 + return NULL; 1.19 +} 1.20 + 1.21 CxList *cxLinkedListCreate( 1.22 CxAllocator *allocator, 1.23 CxListComparator comparator, 1.24 size_t item_size 1.25 ) { 1.26 - cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); 1.27 + cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); 1.28 if (list == NULL) 1.29 return NULL; 1.30 1.31 list->base.cl = &cx_linked_list_class; 1.32 list->base.allocator = allocator; 1.33 + list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; 1.34 list->base.cmpfunc = comparator; 1.35 list->base.itemsize = item_size; 1.36 list->base.capacity = SIZE_MAX; 1.37 - list->base.size = 0; 1.38 - 1.39 - list->begin = NULL; 1.40 - list->end = NULL; 1.41 1.42 return (CxList *) list; 1.43 } 1.44 @@ -783,19 +794,16 @@ 1.45 CxAllocator *allocator, 1.46 CxListComparator comparator 1.47 ) { 1.48 - cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); 1.49 + cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); 1.50 if (list == NULL) 1.51 return NULL; 1.52 1.53 list->base.cl = &cx_pointer_linked_list_class; 1.54 list->base.allocator = allocator; 1.55 + list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; 1.56 list->base.cmpfunc = comparator; 1.57 list->base.itemsize = sizeof(void *); 1.58 list->base.capacity = SIZE_MAX; 1.59 - list->base.size = 0; 1.60 - 1.61 - list->begin = NULL; 1.62 - list->end = NULL; 1.63 1.64 return (CxList *) list; 1.65 } 1.66 @@ -811,22 +819,8 @@ 1.67 if (list == NULL) return NULL; 1.68 for (size_t i = 0; i < num_items; i++) { 1.69 if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { 1.70 - cxLinkedListDestroy(list); 1.71 - return NULL; 1.72 + return cx_ll_default_destructor(list); 1.73 } 1.74 } 1.75 return list; 1.76 } 1.77 - 1.78 -void cxLinkedListDestroy(CxList *list) { 1.79 - cx_linked_list *ll = (cx_linked_list *) list; 1.80 - 1.81 - cx_linked_list_node *node = ll->begin; 1.82 - while (node) { 1.83 - void *next = node->next; 1.84 - cxFree(list->allocator, node); 1.85 - node = next; 1.86 - } 1.87 - 1.88 - cxFree(list->allocator, list); 1.89 -}