# HG changeset patch # User Mike Becker # Date 1445437950 -7200 # Node ID 07a284486fa11a0109e2ed971b66b6adf958747e # Parent 6bdb04d87236e063d62166883736de38f03b0911 added ucx_list_free_contents() diff -r 6bdb04d87236 -r 07a284486fa1 test/list_tests.c --- 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); diff -r 6bdb04d87236 -r 07a284486fa1 ucx/list.c --- 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); } diff -r 6bdb04d87236 -r 07a284486fa1 ucx/list.h --- 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. * * Caution: the argument MUST 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