Mercurial > hg > th-libs
changeset 156:f9254c34ad05
Add new utility function for linked lists, th_llist_free_func_node().
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 07 Feb 2015 01:37:11 +0200 |
parents | 4cd94649475d |
children | e96c900ca6f1 |
files | th_util.c th_util.h |
diffstat | 2 files changed, 24 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/th_util.c Sat Feb 07 00:19:43 2015 +0200 +++ b/th_util.c Sat Feb 07 01:37:11 2015 +0200 @@ -239,6 +239,20 @@ return res; } + +void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *)) +{ + th_llist_t *curr = list; + + while (curr != NULL) + { + th_llist_t *next = curr->next; + freefunc(curr); + curr = next; + } +} + + void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data)) { th_llist_t *curr = list; @@ -246,7 +260,7 @@ while (curr != NULL) { th_llist_t *next = curr->next; - if (freefunc != NULL && curr->data != NULL) + if (curr->data != NULL) freefunc(curr->data); th_free(curr); curr = next; @@ -256,7 +270,14 @@ void th_llist_free(th_llist_t *list) { - th_llist_free_func(list, NULL); + th_llist_t *curr = list; + + while (curr != NULL) + { + th_llist_t *next = curr->next; + th_free(curr); + curr = next; + } }
--- a/th_util.h Sat Feb 07 00:19:43 2015 +0200 +++ b/th_util.h Sat Feb 07 01:37:11 2015 +0200 @@ -155,6 +155,7 @@ th_llist_t * th_llist_new(void *data); void th_llist_free(th_llist_t *list); void th_llist_free_func(th_llist_t *list, void (*freefunc)(void *data)); +void th_llist_free_func_node(th_llist_t *list, void (*freefunc)(th_llist_t *node)); void th_llist_append_node(th_llist_t **list, th_llist_t *node); th_llist_t * th_llist_append(th_llist_t **list, void *data);