Wed, 21 Oct 2015 16:32:30 +0200
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 |
1.1 --- a/test/list_tests.c Thu Oct 15 16:52:53 2015 +0200 1.2 +++ b/test/list_tests.c Wed Oct 21 16:32:30 2015 +0200 1.3 @@ -340,8 +340,8 @@ 1.4 UCX_TEST_ASSERT(world != copy->next->data, "second element is no copy"); 1.5 1.6 UCX_TEST_END 1.7 - free(copy->next->data); 1.8 - free(copy->data); 1.9 + 1.10 + ucx_list_free_contents(copy, free); 1.11 1.12 free(world); 1.13 free(hello);
2.1 --- a/ucx/list.c Thu Oct 15 16:52:53 2015 +0200 2.2 +++ b/ucx/list.c Wed Oct 21 16:32:30 2015 +0200 2.3 @@ -76,6 +76,13 @@ 2.4 } 2.5 } 2.6 2.7 +void ucx_list_free_contents(UcxList* list, ucx_destructor destr) { 2.8 + while (list != NULL) { 2.9 + destr(list->data); 2.10 + list = list->next; 2.11 + } 2.12 +} 2.13 + 2.14 UcxList *ucx_list_append(UcxList *l, void *data) { 2.15 return ucx_list_append_a(ucx_default_allocator(), l, data); 2.16 }
3.1 --- a/ucx/list.h Thu Oct 15 16:52:53 2015 +0200 3.2 +++ b/ucx/list.h Wed Oct 21 16:32:30 2015 +0200 3.3 @@ -146,12 +146,14 @@ 3.4 * Destroys the entire list. 3.5 * 3.6 * The members of the list are not automatically freed, so ensure they are 3.7 - * otherwise referenced or a memory leak will occur. 3.8 + * otherwise referenced or destroyed by ucx_list_free_contents(). 3.9 + * Otherwise, a memory leak is likely to occur. 3.10 * 3.11 * <b>Caution:</b> the argument <b>MUST</b> denote an entire list (i.e. a call 3.12 * to ucx_list_first() on the argument must return the argument itself) 3.13 * 3.14 * @param list the list to free 3.15 + * @see ucx_list_free_contents() 3.16 */ 3.17 void ucx_list_free(UcxList *list); 3.18 3.19 @@ -167,6 +169,20 @@ 3.20 void ucx_list_free_a(UcxAllocator *allocator, UcxList *list); 3.21 3.22 /** 3.23 + * Destroys the contents of the specified list by calling the specified 3.24 + * destructor on each of them. 3.25 + * 3.26 + * Note, that the contents are not usable afterwards and the list should be 3.27 + * destroyed with ucx_list_free(). 3.28 + * 3.29 + * @param list the list for which the contents shall be freed 3.30 + * @param destr the destructor function (e.g. stdlib free()) 3.31 + * @see ucx_list_free() 3.32 + */ 3.33 +void ucx_list_free_contents(UcxList* list, ucx_destructor destr); 3.34 + 3.35 + 3.36 +/** 3.37 * Inserts an element at the end of the list. 3.38 * 3.39 * This is generally an O(n) operation, as the end of the list is retrieved with