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; diff --git a/include/list.h b/include/list.h index 03851a82..70af49f6 100644 --- a/include/list.h +++ b/include/list.h @@ -9,7 +9,6 @@ typedef struct { list_t *create_list(void); void list_free(list_t *list); -void list_foreach(list_t *list, void (*callback)(void* item)); void list_add(list_t *list, void *item); void list_insert(list_t *list, int index, void *item); void list_del(list_t *list, int index); @@ -27,4 +26,10 @@ void list_stable_sort(list_t *list, int compare(const void *a, const void *b)); void list_swap(list_t *list, int src, int dest); // move item to end of list void list_move_to_end(list_t *list, void *item); + +/* Calls `free` for each item in the list, then frees the list. + * Do not use this to free lists of primitives or items that require more + * complicated deallocation code. + */ +void free_flat_list(list_t *list); #endif diff --git a/include/stringop.h b/include/stringop.h index 919e605c..215f7916 100644 --- a/include/stringop.h +++ b/include/stringop.h @@ -19,7 +19,6 @@ int lenient_strcmp(char *a, char *b); // Simply split a string with delims, free with `free_flat_list` list_t *split_string(const char *str, const char *delims); -void free_flat_list(list_t *list); // Splits an argument string, keeping quotes intact char **split_args(const char *str, int *argc); diff --git a/sway/commands/bind.c b/sway/commands/bind.c index c8b634b9..52a245fa 100644 --- a/sway/commands/bind.c +++ b/sway/commands/bind.c @@ -23,9 +23,7 @@ void free_sway_binding(struct sway_binding *binding) { return; } - if (binding->keys) { - free_flat_list(binding->keys); - } + free_flat_list(binding->keys); free(binding->input); free(binding->command); free(binding); diff --git a/sway/commands/reload.c b/sway/commands/reload.c index c64de4bd..c236911e 100644 --- a/sway/commands/reload.c +++ b/sway/commands/reload.c @@ -24,8 +24,7 @@ static void do_reload(void *data) { if (!load_main_config(config->current_config_path, true, false)) { wlr_log(WLR_ERROR, "Error(s) reloading config"); - list_foreach(bar_ids, free); - list_free(bar_ids); + free_flat_list(bar_ids); return; } @@ -42,9 +41,7 @@ static void do_reload(void *data) { } } } - - list_foreach(bar_ids, free); - list_free(bar_ids); + free_flat_list(bar_ids); config_update_font_height(true); root_for_each_container(rebuild_textures_iterator, NULL); diff --git a/sway/config.c b/sway/config.c index ff7de4b9..efb95e31 100644 --- a/sway/config.c +++ b/sway/config.c @@ -654,8 +654,7 @@ bool read_config(FILE *file, struct sway_config *config, if (read + length > config_size) { wlr_log(WLR_ERROR, "Config file changed during reading"); - list_foreach(stack, free); - list_free(stack); + free_flat_list(stack); free(line); return false; } @@ -684,8 +683,7 @@ bool read_config(FILE *file, struct sway_config *config, } char *expanded = expand_line(block, line, brace_detected > 0); if (!expanded) { - list_foreach(stack, free); - list_free(stack); + free_flat_list(stack); free(line); return false; } @@ -750,8 +748,7 @@ bool read_config(FILE *file, struct sway_config *config, free(line); free_cmd_results(res); } - list_foreach(stack, free); - list_free(stack); + free_flat_list(stack); config->current_config_line_number = 0; config->current_config_line = NULL; diff --git a/sway/config/bar.c b/sway/config/bar.c index 36e10527..82457ef4 100644 --- a/sway/config/bar.c +++ b/sway/config/bar.c @@ -53,9 +53,7 @@ void free_bar_config(struct bar_config *bar) { free_bar_binding(binding); } list_free(bar->bindings); - if (bar->outputs) { - free_flat_list(bar->outputs); - } + free_flat_list(bar->outputs); if (bar->pid != 0) { terminate_swaybar(bar->pid); } diff --git a/sway/tree/container.c b/sway/tree/container.c index 91e8dd7f..ac433577 100644 --- a/sway/tree/container.c +++ b/sway/tree/container.c @@ -21,6 +21,7 @@ #include "sway/tree/arrange.h" #include "sway/tree/view.h" #include "sway/tree/workspace.h" +#include "list.h" #include "log.h" #include "stringop.h" @@ -67,8 +68,7 @@ void container_destroy(struct sway_container *con) { list_free(con->current.children); list_free(con->outputs); - list_foreach(con->marks, free); - list_free(con->marks); + free_flat_list(con->marks); wlr_texture_destroy(con->marks_focused); wlr_texture_destroy(con->marks_focused_inactive); wlr_texture_destroy(con->marks_unfocused); @@ -1267,7 +1267,9 @@ bool container_find_and_unmark(char *mark) { } void container_clear_marks(struct sway_container *con) { - list_foreach(con->marks, free); + for (int i = 0; i < con->marks->length; ++i) { + free(con->marks->items[i]); + } con->marks->length = 0; ipc_event_window(con, "mark"); } diff --git a/swayidle/main.c b/swayidle/main.c index dd7d9de3..9a76e58c 100644 --- a/swayidle/main.c +++ b/swayidle/main.c @@ -365,11 +365,6 @@ static int parse_args(int argc, char *argv[]) { return 0; } -static void register_zero_idle_timeout(void *item) { - struct swayidle_timeout_cmd *cmd = item; - register_timeout(cmd, 0); -} - static int handle_signal(int sig, void *data) { switch (sig) { case SIGINT: @@ -378,7 +373,9 @@ static int handle_signal(int sig, void *data) { return 0; case SIGUSR1: wlr_log(WLR_DEBUG, "Got SIGUSR1"); - list_foreach(state.timeout_cmds, register_zero_idle_timeout); + for (int i = 0; i < state.timeout_cmds->length; ++i) { + register_timeout(state.timeout_cmds->items[i], 0); + } return 1; } assert(false); // not reached @@ -409,11 +406,6 @@ static int display_event(int fd, uint32_t mask, void *data) { return count; } -static void register_idle_timeout(void *item) { - struct swayidle_timeout_cmd *cmd = item; - register_timeout(cmd, cmd->timeout); -} - int main(int argc, char *argv[]) { if (parse_args(argc, argv) != 0) { return -1; @@ -458,7 +450,10 @@ int main(int argc, char *argv[]) { sway_terminate(0); } - list_foreach(state.timeout_cmds, register_idle_timeout); + for (int i = 0; i < state.timeout_cmds->length; ++i) { + struct swayidle_timeout_cmd *cmd = state.timeout_cmds->items[i]; + register_timeout(cmd, cmd->timeout); + } wl_display_roundtrip(state.display);