simplify auto-free contents in lists

Mon, 18 Apr 2022 15:59:09 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 18 Apr 2022 15:59:09 +0200
changeset 525
536646d1575b
parent 524
e98b09018d32
child 526
b070ef465313

simplify auto-free contents in lists

src/cx/list.h file | annotate | diff | comparison | revisions
src/list.c file | annotate | diff | comparison | revisions
--- a/src/cx/list.h	Mon Apr 18 15:29:52 2022 +0200
+++ b/src/cx/list.h	Mon Apr 18 15:59:09 2022 +0200
@@ -90,11 +90,6 @@
      * The capacity of the list (maximum number of elements).
      */
     size_t capacity;
-    /**
-     * Flag indicating whether cxListDestroy() shall free each list element,
-     * even if cx_list_s.content_destructor did not do that.
-     */
-    bool autofree_contents;
 };
 
 /**
--- a/src/list.c	Mon Apr 18 15:29:52 2022 +0200
+++ b/src/list.c	Mon Apr 18 15:59:09 2022 +0200
@@ -29,25 +29,10 @@
 #include "cx/list.h"
 
 CxList *cxListDestroy(CxList *list) {
-    if (list->content_destructor == NULL) {
-        if (list->autofree_contents) {
-            CxIterator iter = cxListBegin(list);
-            cx_foreach(void*, elem, iter) {
-                cxFree(list->allocator, elem);
-            }
-        }
-    } else {
-        // avoid checking this condition every loop iteration
-        if (list->autofree_contents) {
-            CxIterator iter = cxListBegin(list);
-            cx_foreach(void*, elem, iter) {
-                cxFree(list->allocator, list->content_destructor(elem));
-            }
-        } else {
-            CxIterator iter = cxListBegin(list);
-            cx_foreach(void*, elem, iter) {
-                elem = list->content_destructor(elem);
-            }
+    if (list->content_destructor != NULL) {
+        CxIterator iter = cxListBegin(list);
+        cx_foreach(void*, elem, iter) {
+            cxFree(list->allocator, list->content_destructor(elem));
         }
     }
     list->cl->destructor(list);

mercurial