Add cfulist_delete_data_with_free_fn

Conditionals tests in cfulist_delete_data(_with_free_fn) were simplified
This commit is contained in:
Diogo Cordeiro 2018-06-08 11:43:15 +01:00
parent 00f09172c3
commit 4d6a4c0a16
3 changed files with 23 additions and 5 deletions

View File

@ -514,6 +514,14 @@ cfulist_map(). The return value is used to build a new list.
Deletes the entry in the list associated with value. Deletes the entry in the list associated with value.
@end deftypefun @end deftypefun
@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. If ff is not NULL,
it overrides any function set previously with cfulist_set_free_function().
@end deftypefun
@deftypefun {int} cfulist_unshift_data (cfulist_t * @var{list}, void * @var{data}, size_t @var{data_size}) @deftypefun {int} cfulist_unshift_data (cfulist_t * @var{list}, void * @var{data}, size_t @var{data_size})
Add a value at the beginning of the list. Add a value at the beginning of the list.

View File

@ -414,18 +414,25 @@ cfulist_pop(cfulist_t *list) {
void void
cfulist_delete_data(cfulist_t *list, void *data) { cfulist_delete_data(cfulist_t *list, void *data) {
cfulist_delete_data_with_free_fn(list, data, list->free_fn);
return;
}
void
cfulist_delete_data_with_free_fn(cfulist_t *list, void *data, cfulist_free_fn_t free_fn) {
if (!list) { if (!list) {
return; return;
} }
lock_list(list); lock_list(list);
if (list->entries) {
cfulist_entry *entry = list->entries; cfulist_entry *entry = list->entries;
while (entry && entry->data != data) while (entry)
{
if (entry->data != data) {
entry = entry->next; entry = entry->next;
continue;
if (entry && entry->data == data) { } else {
if (!entry->prev) { if (!entry->prev) {
if (entry->next) { if (entry->next) {
assert(list->num_entries > 1); assert(list->num_entries > 1);
@ -439,8 +446,10 @@ cfulist_delete_data(cfulist_t *list, void *data) {
} else { } else {
(entry->prev)->next = entry->next; (entry->prev)->next = entry->next;
} }
if (free_fn) free_fn(entry->data);
free (entry); free (entry);
--list->num_entries; --list->num_entries;
break;
} }
} }

View File

@ -83,6 +83,7 @@ int cfulist_pop_data(cfulist_t *list, void **data, size_t *data_size);
/* Deletes the entry in the list associated with value. */ /* Deletes the entry in the list associated with value. */
void cfulist_delete_data(cfulist_t *list, void *data); void cfulist_delete_data(cfulist_t *list, void *data);
void cfulist_delete_data_with_free_fn(cfulist_t *list, void *data, cfulist_free_fn_t free_fn);
/* Add a value at the beginning of the list. */ /* Add a value at the beginning of the list. */
int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size); int cfulist_unshift_data(cfulist_t *list, void *data, size_t data_size);