added ucx_list_free_contents()

Wed, 21 Oct 2015 16:32:30 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 21 Oct 2015 16:32:30 +0200
changeset 211
07a284486fa1
parent 210
6bdb04d87236
child 212
c766c423dee6

added ucx_list_free_contents()

test/list_tests.c file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
--- a/test/list_tests.c	Thu Oct 15 16:52:53 2015 +0200
+++ b/test/list_tests.c	Wed Oct 21 16:32:30 2015 +0200
@@ -340,8 +340,8 @@
     UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy");
 
     UCX_TEST_END
-    free(copy->next->data);
-    free(copy->data);
+    
+    ucx_list_free_contents(copy, free);
 
     free(world);
     free(hello);
--- a/ucx/list.c	Thu Oct 15 16:52:53 2015 +0200
+++ b/ucx/list.c	Wed Oct 21 16:32:30 2015 +0200
@@ -76,6 +76,13 @@
     }
 }
 
+void ucx_list_free_contents(UcxList* list, ucx_destructor destr) {
+    while (list != NULL) {
+        destr(list->data);
+        list = list->next;
+    }
+}
+
 UcxList *ucx_list_append(UcxList *l, void *data)  {
     return ucx_list_append_a(ucx_default_allocator(), l, data);
 }
--- a/ucx/list.h	Thu Oct 15 16:52:53 2015 +0200
+++ b/ucx/list.h	Wed Oct 21 16:32:30 2015 +0200
@@ -146,12 +146,14 @@
  * Destroys the entire list.
  * 
  * The members of the list are not automatically freed, so ensure they are
- * otherwise referenced or a memory leak will occur.
+ * otherwise referenced or destroyed by ucx_list_free_contents().
+ * Otherwise, a memory leak is likely to occur.
  * 
  * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call
  * to ucx_list_first() on the argument must return the argument itself)
  * 
  * @param list the list to free
+ * @see ucx_list_free_contents()
  */
 void ucx_list_free(UcxList *list);
 
@@ -167,6 +169,20 @@
 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list);
 
 /**
+ * Destroys the contents of the specified list by calling the specified
+ * destructor on each of them.
+ * 
+ * Note, that the contents are not usable afterwards and the list should be
+ * destroyed with ucx_list_free().
+ * 
+ * @param list the list for which the contents shall be freed
+ * @param destr the destructor function (e.g. stdlib free())
+ * @see ucx_list_free()
+ */
+void ucx_list_free_contents(UcxList* list, ucx_destructor destr);
+
+
+/**
  * Inserts an element at the end of the list.
  * 
  * This is generally an O(n) operation, as the end of the list is retrieved with

mercurial