diff -r 33e7b6ebf403 -r a89857072ace src/linked_list.c --- a/src/linked_list.c Tue Feb 15 19:07:14 2022 +0100 +++ b/src/linked_list.c Tue Feb 15 19:41:48 2022 +0100 @@ -757,24 +757,35 @@ cx_pll_iterator, }; +static CxList *cx_ll_default_destructor(CxList *list) { + cx_linked_list *ll = (cx_linked_list *) list; + + cx_linked_list_node *node = ll->begin; + while (node) { + void *next = node->next; + cxFree(list->allocator, node); + node = next; + } + + cxFree(list->allocator, list); + return NULL; +} + CxList *cxLinkedListCreate( CxAllocator *allocator, CxListComparator comparator, size_t item_size ) { - cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); + cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list == NULL) return NULL; list->base.cl = &cx_linked_list_class; list->base.allocator = allocator; + list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; list->base.cmpfunc = comparator; list->base.itemsize = item_size; list->base.capacity = SIZE_MAX; - list->base.size = 0; - - list->begin = NULL; - list->end = NULL; return (CxList *) list; } @@ -783,19 +794,16 @@ CxAllocator *allocator, CxListComparator comparator ) { - cx_linked_list *list = cxMalloc(allocator, sizeof(cx_linked_list)); + cx_linked_list *list = cxCalloc(allocator, 1, sizeof(cx_linked_list)); if (list == NULL) return NULL; list->base.cl = &cx_pointer_linked_list_class; list->base.allocator = allocator; + list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor; list->base.cmpfunc = comparator; list->base.itemsize = sizeof(void *); list->base.capacity = SIZE_MAX; - list->base.size = 0; - - list->begin = NULL; - list->end = NULL; return (CxList *) list; } @@ -811,22 +819,8 @@ if (list == NULL) return NULL; for (size_t i = 0; i < num_items; i++) { if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { - cxLinkedListDestroy(list); - return NULL; + return cx_ll_default_destructor(list); } } return list; } - -void cxLinkedListDestroy(CxList *list) { - cx_linked_list *ll = (cx_linked_list *) list; - - cx_linked_list_node *node = ll->begin; - while (node) { - void *next = node->next; - cxFree(list->allocator, node); - node = next; - } - - cxFree(list->allocator, list); -}