From 19e831ed3da2aba75d56e46c57967bcc60442d57 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Sat, 8 Dec 2018 22:52:29 +0000 Subject: list.c: Remove list_foreach Most occurrences have been replaced by `free_flat_list` which has been moved from stringop.c to list.c. The rest have been replaced by for loops. --- common/list.c | 21 ++++++++++++--------- common/loop.c | 6 ++---- common/stringop.c | 8 -------- 3 files changed, 14 insertions(+), 21 deletions(-) (limited to 'common') diff --git a/common/list.c b/common/list.c index ee268c9a..a0b42512 100644 --- a/common/list.c +++ b/common/list.c @@ -30,15 +30,6 @@ void list_free(list_t *list) { free(list); } -void list_foreach(list_t *list, void (*callback)(void *item)) { - if (list == NULL || callback == NULL) { - return; - } - for (int i = 0; i < list->length; i++) { - callback(list->items[i]); - } -} - void list_add(list_t *list, void *item) { list_resize(list); list->items[list->length++] = item; @@ -156,3 +147,15 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { list_inplace_sort(list, 0, list->length - 1, compare); } } + +void free_flat_list(list_t *list) { + if (!list) { + return; + } + + for (int i = 0; i < list->length; ++i) { + free(list->items[i]); + } + list_free(list); +} + diff --git a/common/loop.c b/common/loop.c index 82b80017..ad2b4a64 100644 --- a/common/loop.c +++ b/common/loop.c @@ -45,10 +45,8 @@ struct loop *loop_create(void) { } void loop_destroy(struct loop *loop) { - list_foreach(loop->fd_events, free); - list_foreach(loop->timers, free); - list_free(loop->fd_events); - list_free(loop->timers); + free_flat_list(loop->fd_events); + free_flat_list(loop->timers); free(loop->fds); free(loop); } diff --git a/common/stringop.c b/common/stringop.c index df016e9d..4b8c9a38 100644 --- a/common/stringop.c +++ b/common/stringop.c @@ -97,14 +97,6 @@ list_t *split_string(const char *str, const char *delims) { return res; } -void free_flat_list(list_t *list) { - int i; - for (i = 0; i < list->length; ++i) { - free(list->items[i]); - } - list_free(list); -} - char **split_args(const char *start, int *argc) { *argc = 0; int alloc = 2; -- cgit v1.2.3 From c8776fac4232f9faab0a78ef3e18dc4366496421 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Sat, 8 Dec 2018 23:55:14 +0000 Subject: Cleanup list code --- common/list.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'common') diff --git a/common/list.c b/common/list.c index a0b42512..2a803702 100644 --- a/common/list.c +++ b/common/list.c @@ -48,8 +48,7 @@ void list_del(list_t *list, int index) { } void list_cat(list_t *list, list_t *source) { - int i; - for (i = 0; i < source->length; ++i) { + for (int i = 0; i < source->length; ++i) { list_add(list, source->items[i]); } } -- cgit v1.2.3 From 98c1e19466c0d83c8e1ca86eda5b273eda7eff3c Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Sun, 9 Dec 2018 01:15:23 +0000 Subject: list.c: rename free_flat_list to list_free_items_and_destroy --- common/list.c | 2 +- common/loop.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'common') diff --git a/common/list.c b/common/list.c index 2a803702..9805ceec 100644 --- a/common/list.c +++ b/common/list.c @@ -147,7 +147,7 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)) { } } -void free_flat_list(list_t *list) { +void list_free_items_and_destroy(list_t *list) { if (!list) { return; } diff --git a/common/loop.c b/common/loop.c index ad2b4a64..295f3a30 100644 --- a/common/loop.c +++ b/common/loop.c @@ -45,8 +45,8 @@ struct loop *loop_create(void) { } void loop_destroy(struct loop *loop) { - free_flat_list(loop->fd_events); - free_flat_list(loop->timers); + list_free_items_and_destroy(loop->fd_events); + list_free_items_and_destroy(loop->timers); free(loop->fds); free(loop); } -- cgit v1.2.3