Mon, 18 Apr 2022 15:29:52 +0200
remove list destructor
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 |
--- a/src/cx/list.h Mon Apr 18 14:41:19 2022 +0200 +++ b/src/cx/list.h Mon Apr 18 15:29:52 2022 +0200 @@ -71,10 +71,6 @@ */ CxAllocator const *allocator; /** - * A mandatory destructor for the list structure. - */ - cx_destructor_func list_destructor; - /** * An optional destructor for the list contents. */ cx_destructor_func content_destructor; @@ -95,11 +91,6 @@ */ size_t capacity; /** - * Flag indicating whether cxListDestroy() shall free the list structure, - * even if cx_list_s.list_destructor did not do that. - */ - bool autofree; - /** * Flag indicating whether cxListDestroy() shall free each list element, * even if cx_list_s.content_destructor did not do that. */ @@ -111,6 +102,11 @@ */ struct cx_list_class_s { /** + * Destructor function. + */ + void (*destructor)(struct cx_list_s *list); + + /** * Member function for adding an element. */ int (*add)( @@ -193,25 +189,6 @@ typedef struct cx_list_s CxList; /** - * Convenience function to configure the memory management for this list. - * - * @param list the list to configure - * @param list_destructor an alternative list destructor to use (if \c NULL, the current destructor remains unchanged) - * @param content_destructor the content destructor to use (if \c NULL, no content destructor is used) - * @param list_autofree a flag indicating, if the list allocator shall free the list, if the destructor did not do that - * @param content_autofree a flag indicating, if the list allocator shall free an element, - * if the content destructor did not do that or no content destructor exists - */ -__attribute__((__nonnull__(1))) -void cxListMemoryMgmt( - CxList *list, - cx_destructor_func list_destructor, - cx_destructor_func content_destructor, - bool list_autofree, - bool content_autofree -); - -/** * Adds an item to the end of the list. * * @param list the list
--- a/src/linked_list.c Mon Apr 18 14:41:19 2022 +0200 +++ b/src/linked_list.c Mon Apr 18 15:29:52 2022 +0200 @@ -733,7 +733,20 @@ return cx_ll_insert_iter(iter, &elem, prepend); } +static void cx_ll_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; + } + // do not free the list pointer, this is just a destructor! +} + static cx_list_class cx_linked_list_class = { + cx_ll_destructor, cx_ll_add, cx_ll_insert, cx_ll_insert_iter, @@ -747,6 +760,7 @@ }; static cx_list_class cx_pointer_linked_list_class = { + cx_ll_destructor, cx_pll_add, cx_pll_insert, cx_pll_insert_iter, @@ -759,20 +773,6 @@ 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 const *allocator, CxListComparator comparator, @@ -784,7 +784,6 @@ 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; @@ -802,7 +801,6 @@ 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; @@ -821,7 +819,9 @@ if (list == NULL) return NULL; cx_for_n (i, num_items) { if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) { - return cx_ll_default_destructor(list); + cx_ll_destructor(list); + cxFree(allocator, list); + return NULL; } } return list;
--- a/src/list.c Mon Apr 18 14:41:19 2022 +0200 +++ b/src/list.c Mon Apr 18 15:29:52 2022 +0200 @@ -50,23 +50,7 @@ } } } - if (list->autofree) { - cxFree(list->allocator, list->list_destructor(list)); - return NULL; - } else { - return list->list_destructor(list); - } + list->cl->destructor(list); + cxFree(list->allocator, list); + return NULL; } - -void cxListMemoryMgmt( - CxList *list, - cx_destructor_func list_destructor, - cx_destructor_func content_destructor, - bool list_autofree, - bool content_autofree -) { - if (list_destructor != NULL) list->list_destructor = list_destructor; - list->content_destructor = content_destructor; - list->autofree = list_autofree; - list->autofree_contents = content_autofree; -}