From 4f4439828fc4f2b51c86898c6bbe1007ae1082db Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Fri, 8 Jun 2018 12:52:10 +0100 Subject: [PATCH 1/3] Added cfulist_set_free_fn() In analogy with cfuhash_set_free_fn() --- doc/libcfu.texi | 7 ++++++- src/cfulist.c | 8 +++++++- src/cfulist.h | 4 +--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/doc/libcfu.texi b/doc/libcfu.texi index 3b1a38d..89f7854 100644 --- a/doc/libcfu.texi +++ b/doc/libcfu.texi @@ -490,7 +490,12 @@ cfulist_map(). The return value is used to build a new list. @deftypefun {cfulist_t *} cfulist_new (); - Returns a new list. + Creates a new list. +@end deftypefun + +@deftypefun {cfulist_t *} cfulist_new_with_free_fn (cfulist_free_fn_t @var{ff}) + + Same as cfulist_new() except automatically calls cfulist_set_free_fn(). @end deftypefun @deftypefun {size_t} cfulist_num_entries (cfulist_t *@var{list}) diff --git a/src/cfulist.c b/src/cfulist.c index 052d515..b58846e 100644 --- a/src/cfulist.c +++ b/src/cfulist.c @@ -84,10 +84,16 @@ cfulist_new(void) { cfulist_t * cfulist_new_with_free_fn(cfulist_free_fn_t free_fn) { cfulist_t *list = cfulist_new(); - list->free_fn = free_fn; + cfulist_set_free_function (list, free_fn); return list; } +int +cfulist_set_free_function(cfulist_t *list, cfulist_free_fn_t ff) { + if (ff) list->free_fn = ff; + return 0; +} + size_t cfulist_num_entries(cfulist_t *list) { return list->num_entries; diff --git a/src/cfulist.h b/src/cfulist.h index 52e92ec..e7cea4f 100644 --- a/src/cfulist.h +++ b/src/cfulist.h @@ -65,9 +65,7 @@ typedef void (*cfulist_free_fn_t)(void *data); /* Returns a new list. */ cfulist_t * cfulist_new(void); -/* Same as cfulist_new(), but set a function to be called on each - * element when the list is destroyed. - */ +/* Same as cfulist_new() except automatically calls cfulist_set_free_fn(). */ cfulist_t * cfulist_new_with_free_fn(cfulist_free_fn_t free_fn); /* Returns the number of entries in the list. */ From 703622e03d7925faea41f0e879102de03e0d5f6c Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Fri, 8 Jun 2018 13:36:23 +0100 Subject: [PATCH 2/3] Fix cfulist_destroy_with_free_fn() doc. It was declared again as cfulist_destroy() and didn't mention the effects related to cfulist_set_free_function(). --- doc/libcfu.texi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/doc/libcfu.texi b/doc/libcfu.texi index 89f7854..ba0f0b4 100644 --- a/doc/libcfu.texi +++ b/doc/libcfu.texi @@ -581,10 +581,12 @@ position in the new list. Free all resources used by the list. @end deftypefun -@deftypefun {void} cfulist_destroy (cfulist_t * @var{list}, cfulist_free_fn_t @var{free_fn}) +@deftypefun {void} cfulist_destroy_with_free_fn (cfulist_t * @var{list}, cfulist_free_fn_t @var{free_fn}) - Free all resources used by the list. If free_fn is not NULL, call it +Free all resources used by the list. If free_fn is not NULL, is called for each element of the list, passing the data to it as a void *. +If free_fn is not NULL, it overrides any function set previously +with cfulist_set_free_function(). @end deftypefun From ff7404a58a935d6ec9a49e723aad934ad2407bd2 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Fri, 8 Jun 2018 19:48:02 +0100 Subject: [PATCH 3/3] cfulist_delete_data_with_free_fn was updated because of cfulist_set_free_function() Now the doc explains better what happens with (if) a free function was previously set. --- doc/libcfu.texi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/libcfu.texi b/doc/libcfu.texi index d066b8f..33d109f 100644 --- a/doc/libcfu.texi +++ b/doc/libcfu.texi @@ -522,7 +522,8 @@ cfulist_map(). The return value is used to build a new list. @deftypefun {void} cfulist_delete_data_with_free_fn (cfulist_t * @var{list}, void * @var{data}, cfulist_free_fn_t @var{ff}) Deletes the entry in the list associated with value. If ff is not NULL, it - is called for value of the entry passed as its only argument. + is called for value of the entry passed as its only argument. If ff is not NULL, + it overrides any function set previously with cfulist_set_free_function(). @end deftypefun