Merge pull request #5 from diogogithub/cfulist_set_free_function

Added cfulist_set_free_fn()
This commit is contained in:
Matthew Brush 2018-06-08 16:12:51 -07:00 committed by GitHub
commit f3f439998d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 8 deletions

View File

@ -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})
@ -517,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
@ -588,10 +594,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

View File

@ -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;

View File

@ -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. */