src/linked_list.c

changeset 524
e98b09018d32
parent 521
e5dc54131d55
child 528
4fbfac557df8
     1.1 --- a/src/linked_list.c	Mon Apr 18 14:41:19 2022 +0200
     1.2 +++ b/src/linked_list.c	Mon Apr 18 15:29:52 2022 +0200
     1.3 @@ -733,7 +733,20 @@
     1.4      return cx_ll_insert_iter(iter, &elem, prepend);
     1.5  }
     1.6  
     1.7 +static void cx_ll_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 +    // do not free the list pointer, this is just a destructor!
    1.17 +}
    1.18 +
    1.19  static cx_list_class cx_linked_list_class = {
    1.20 +        cx_ll_destructor,
    1.21          cx_ll_add,
    1.22          cx_ll_insert,
    1.23          cx_ll_insert_iter,
    1.24 @@ -747,6 +760,7 @@
    1.25  };
    1.26  
    1.27  static cx_list_class cx_pointer_linked_list_class = {
    1.28 +        cx_ll_destructor,
    1.29          cx_pll_add,
    1.30          cx_pll_insert,
    1.31          cx_pll_insert_iter,
    1.32 @@ -759,20 +773,6 @@
    1.33          cx_pll_iterator,
    1.34  };
    1.35  
    1.36 -static CxList *cx_ll_default_destructor(CxList *list) {
    1.37 -    cx_linked_list *ll = (cx_linked_list *) list;
    1.38 -
    1.39 -    cx_linked_list_node *node = ll->begin;
    1.40 -    while (node) {
    1.41 -        void *next = node->next;
    1.42 -        cxFree(list->allocator, node);
    1.43 -        node = next;
    1.44 -    }
    1.45 -
    1.46 -    cxFree(list->allocator, list);
    1.47 -    return NULL;
    1.48 -}
    1.49 -
    1.50  CxList *cxLinkedListCreate(
    1.51          CxAllocator const *allocator,
    1.52          CxListComparator comparator,
    1.53 @@ -784,7 +784,6 @@
    1.54  
    1.55      list->base.cl = &cx_linked_list_class;
    1.56      list->base.allocator = allocator;
    1.57 -    list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
    1.58      list->base.cmpfunc = comparator;
    1.59      list->base.itemsize = item_size;
    1.60      list->base.capacity = SIZE_MAX;
    1.61 @@ -802,7 +801,6 @@
    1.62  
    1.63      list->base.cl = &cx_pointer_linked_list_class;
    1.64      list->base.allocator = allocator;
    1.65 -    list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
    1.66      list->base.cmpfunc = comparator;
    1.67      list->base.itemsize = sizeof(void *);
    1.68      list->base.capacity = SIZE_MAX;
    1.69 @@ -821,7 +819,9 @@
    1.70      if (list == NULL) return NULL;
    1.71      cx_for_n (i, num_items) {
    1.72          if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
    1.73 -            return cx_ll_default_destructor(list);
    1.74 +            cx_ll_destructor(list);
    1.75 +            cxFree(allocator, list);
    1.76 +            return NULL;
    1.77          }
    1.78      }
    1.79      return list;

mercurial