remove list destructor

Mon, 18 Apr 2022 15:29:52 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Apr 2022 15:29:52 +0200
changeset 524
e98b09018d32
parent 523
6a981ec4d58b
child 525
536646d1575b

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
     1.1 --- a/src/cx/list.h	Mon Apr 18 14:41:19 2022 +0200
     1.2 +++ b/src/cx/list.h	Mon Apr 18 15:29:52 2022 +0200
     1.3 @@ -71,10 +71,6 @@
     1.4       */
     1.5      CxAllocator const *allocator;
     1.6      /**
     1.7 -     * A mandatory destructor for the list structure.
     1.8 -     */
     1.9 -    cx_destructor_func list_destructor;
    1.10 -    /**
    1.11       * An optional destructor for the list contents.
    1.12       */
    1.13      cx_destructor_func content_destructor;
    1.14 @@ -95,11 +91,6 @@
    1.15       */
    1.16      size_t capacity;
    1.17      /**
    1.18 -     * Flag indicating whether cxListDestroy() shall free the list structure,
    1.19 -     * even if cx_list_s.list_destructor did not do that.
    1.20 -     */
    1.21 -    bool autofree;
    1.22 -    /**
    1.23       * Flag indicating whether cxListDestroy() shall free each list element,
    1.24       * even if cx_list_s.content_destructor did not do that.
    1.25       */
    1.26 @@ -111,6 +102,11 @@
    1.27   */
    1.28  struct cx_list_class_s {
    1.29      /**
    1.30 +     * Destructor function.
    1.31 +     */
    1.32 +    void (*destructor)(struct cx_list_s *list);
    1.33 +
    1.34 +    /**
    1.35       * Member function for adding an element.
    1.36       */
    1.37      int (*add)(
    1.38 @@ -193,25 +189,6 @@
    1.39  typedef struct cx_list_s CxList;
    1.40  
    1.41  /**
    1.42 - * Convenience function to configure the memory management for this list.
    1.43 - *
    1.44 - * @param list the list to configure
    1.45 - * @param list_destructor an alternative list destructor to use (if \c NULL, the current destructor remains unchanged)
    1.46 - * @param content_destructor the content destructor to use (if \c NULL, no content destructor is used)
    1.47 - * @param list_autofree a flag indicating, if the list allocator shall free the list, if the destructor did not do that
    1.48 - * @param content_autofree a flag indicating, if the list allocator shall free an element,
    1.49 - * if the content destructor did not do that or no content destructor exists
    1.50 - */
    1.51 -__attribute__((__nonnull__(1)))
    1.52 -void cxListMemoryMgmt(
    1.53 -        CxList *list,
    1.54 -        cx_destructor_func list_destructor,
    1.55 -        cx_destructor_func content_destructor,
    1.56 -        bool list_autofree,
    1.57 -        bool content_autofree
    1.58 -);
    1.59 -
    1.60 -/**
    1.61   * Adds an item to the end of the list.
    1.62   *
    1.63   * @param list the list
     2.1 --- a/src/linked_list.c	Mon Apr 18 14:41:19 2022 +0200
     2.2 +++ b/src/linked_list.c	Mon Apr 18 15:29:52 2022 +0200
     2.3 @@ -733,7 +733,20 @@
     2.4      return cx_ll_insert_iter(iter, &elem, prepend);
     2.5  }
     2.6  
     2.7 +static void cx_ll_destructor(CxList *list) {
     2.8 +    cx_linked_list *ll = (cx_linked_list *) list;
     2.9 +
    2.10 +    cx_linked_list_node *node = ll->begin;
    2.11 +    while (node) {
    2.12 +        void *next = node->next;
    2.13 +        cxFree(list->allocator, node);
    2.14 +        node = next;
    2.15 +    }
    2.16 +    // do not free the list pointer, this is just a destructor!
    2.17 +}
    2.18 +
    2.19  static cx_list_class cx_linked_list_class = {
    2.20 +        cx_ll_destructor,
    2.21          cx_ll_add,
    2.22          cx_ll_insert,
    2.23          cx_ll_insert_iter,
    2.24 @@ -747,6 +760,7 @@
    2.25  };
    2.26  
    2.27  static cx_list_class cx_pointer_linked_list_class = {
    2.28 +        cx_ll_destructor,
    2.29          cx_pll_add,
    2.30          cx_pll_insert,
    2.31          cx_pll_insert_iter,
    2.32 @@ -759,20 +773,6 @@
    2.33          cx_pll_iterator,
    2.34  };
    2.35  
    2.36 -static CxList *cx_ll_default_destructor(CxList *list) {
    2.37 -    cx_linked_list *ll = (cx_linked_list *) list;
    2.38 -
    2.39 -    cx_linked_list_node *node = ll->begin;
    2.40 -    while (node) {
    2.41 -        void *next = node->next;
    2.42 -        cxFree(list->allocator, node);
    2.43 -        node = next;
    2.44 -    }
    2.45 -
    2.46 -    cxFree(list->allocator, list);
    2.47 -    return NULL;
    2.48 -}
    2.49 -
    2.50  CxList *cxLinkedListCreate(
    2.51          CxAllocator const *allocator,
    2.52          CxListComparator comparator,
    2.53 @@ -784,7 +784,6 @@
    2.54  
    2.55      list->base.cl = &cx_linked_list_class;
    2.56      list->base.allocator = allocator;
    2.57 -    list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
    2.58      list->base.cmpfunc = comparator;
    2.59      list->base.itemsize = item_size;
    2.60      list->base.capacity = SIZE_MAX;
    2.61 @@ -802,7 +801,6 @@
    2.62  
    2.63      list->base.cl = &cx_pointer_linked_list_class;
    2.64      list->base.allocator = allocator;
    2.65 -    list->base.list_destructor = (cx_destructor_func) cx_ll_default_destructor;
    2.66      list->base.cmpfunc = comparator;
    2.67      list->base.itemsize = sizeof(void *);
    2.68      list->base.capacity = SIZE_MAX;
    2.69 @@ -821,7 +819,9 @@
    2.70      if (list == NULL) return NULL;
    2.71      cx_for_n (i, num_items) {
    2.72          if (0 != cxListAdd(list, ((const unsigned char *) array) + i * item_size)) {
    2.73 -            return cx_ll_default_destructor(list);
    2.74 +            cx_ll_destructor(list);
    2.75 +            cxFree(allocator, list);
    2.76 +            return NULL;
    2.77          }
    2.78      }
    2.79      return list;
     3.1 --- a/src/list.c	Mon Apr 18 14:41:19 2022 +0200
     3.2 +++ b/src/list.c	Mon Apr 18 15:29:52 2022 +0200
     3.3 @@ -50,23 +50,7 @@
     3.4              }
     3.5          }
     3.6      }
     3.7 -    if (list->autofree) {
     3.8 -        cxFree(list->allocator, list->list_destructor(list));
     3.9 -        return NULL;
    3.10 -    } else {
    3.11 -        return list->list_destructor(list);
    3.12 -    }
    3.13 +    list->cl->destructor(list);
    3.14 +    cxFree(list->allocator, list);
    3.15 +    return NULL;
    3.16  }
    3.17 -
    3.18 -void cxListMemoryMgmt(
    3.19 -        CxList *list,
    3.20 -        cx_destructor_func list_destructor,
    3.21 -        cx_destructor_func content_destructor,
    3.22 -        bool list_autofree,
    3.23 -        bool content_autofree
    3.24 -) {
    3.25 -    if (list_destructor != NULL) list->list_destructor = list_destructor;
    3.26 -    list->content_destructor = content_destructor;
    3.27 -    list->autofree = list_autofree;
    3.28 -    list->autofree_contents = content_autofree;
    3.29 -}

mercurial