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.
master
Ian Fan 6 years ago
parent 0c3f0dfd16
commit 19e831ed3d

@ -30,15 +30,6 @@ void list_free(list_t *list) {
free(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) { void list_add(list_t *list, void *item) {
list_resize(list); list_resize(list);
list->items[list->length++] = item; 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); 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);
}

@ -45,10 +45,8 @@ struct loop *loop_create(void) {
} }
void loop_destroy(struct loop *loop) { void loop_destroy(struct loop *loop) {
list_foreach(loop->fd_events, free); free_flat_list(loop->fd_events);
list_foreach(loop->timers, free); free_flat_list(loop->timers);
list_free(loop->fd_events);
list_free(loop->timers);
free(loop->fds); free(loop->fds);
free(loop); free(loop);
} }

@ -97,14 +97,6 @@ list_t *split_string(const char *str, const char *delims) {
return res; 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) { char **split_args(const char *start, int *argc) {
*argc = 0; *argc = 0;
int alloc = 2; int alloc = 2;

@ -9,7 +9,6 @@ typedef struct {
list_t *create_list(void); list_t *create_list(void);
void list_free(list_t *list); 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_add(list_t *list, void *item);
void list_insert(list_t *list, int index, void *item); void list_insert(list_t *list, int index, void *item);
void list_del(list_t *list, int index); 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); void list_swap(list_t *list, int src, int dest);
// move item to end of list // move item to end of list
void list_move_to_end(list_t *list, void *item); 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 #endif

@ -19,7 +19,6 @@ int lenient_strcmp(char *a, char *b);
// Simply split a string with delims, free with `free_flat_list` // Simply split a string with delims, free with `free_flat_list`
list_t *split_string(const char *str, const char *delims); list_t *split_string(const char *str, const char *delims);
void free_flat_list(list_t *list);
// Splits an argument string, keeping quotes intact // Splits an argument string, keeping quotes intact
char **split_args(const char *str, int *argc); char **split_args(const char *str, int *argc);

@ -23,9 +23,7 @@ void free_sway_binding(struct sway_binding *binding) {
return; return;
} }
if (binding->keys) {
free_flat_list(binding->keys); free_flat_list(binding->keys);
}
free(binding->input); free(binding->input);
free(binding->command); free(binding->command);
free(binding); free(binding);

@ -24,8 +24,7 @@ static void do_reload(void *data) {
if (!load_main_config(config->current_config_path, true, false)) { if (!load_main_config(config->current_config_path, true, false)) {
wlr_log(WLR_ERROR, "Error(s) reloading config"); wlr_log(WLR_ERROR, "Error(s) reloading config");
list_foreach(bar_ids, free); free_flat_list(bar_ids);
list_free(bar_ids);
return; return;
} }
@ -42,9 +41,7 @@ static void do_reload(void *data) {
} }
} }
} }
free_flat_list(bar_ids);
list_foreach(bar_ids, free);
list_free(bar_ids);
config_update_font_height(true); config_update_font_height(true);
root_for_each_container(rebuild_textures_iterator, NULL); root_for_each_container(rebuild_textures_iterator, NULL);

@ -654,8 +654,7 @@ bool read_config(FILE *file, struct sway_config *config,
if (read + length > config_size) { if (read + length > config_size) {
wlr_log(WLR_ERROR, "Config file changed during reading"); wlr_log(WLR_ERROR, "Config file changed during reading");
list_foreach(stack, free); free_flat_list(stack);
list_free(stack);
free(line); free(line);
return false; return false;
} }
@ -684,8 +683,7 @@ bool read_config(FILE *file, struct sway_config *config,
} }
char *expanded = expand_line(block, line, brace_detected > 0); char *expanded = expand_line(block, line, brace_detected > 0);
if (!expanded) { if (!expanded) {
list_foreach(stack, free); free_flat_list(stack);
list_free(stack);
free(line); free(line);
return false; return false;
} }
@ -750,8 +748,7 @@ bool read_config(FILE *file, struct sway_config *config,
free(line); free(line);
free_cmd_results(res); free_cmd_results(res);
} }
list_foreach(stack, free); free_flat_list(stack);
list_free(stack);
config->current_config_line_number = 0; config->current_config_line_number = 0;
config->current_config_line = NULL; config->current_config_line = NULL;

@ -53,9 +53,7 @@ void free_bar_config(struct bar_config *bar) {
free_bar_binding(binding); free_bar_binding(binding);
} }
list_free(bar->bindings); list_free(bar->bindings);
if (bar->outputs) {
free_flat_list(bar->outputs); free_flat_list(bar->outputs);
}
if (bar->pid != 0) { if (bar->pid != 0) {
terminate_swaybar(bar->pid); terminate_swaybar(bar->pid);
} }

@ -21,6 +21,7 @@
#include "sway/tree/arrange.h" #include "sway/tree/arrange.h"
#include "sway/tree/view.h" #include "sway/tree/view.h"
#include "sway/tree/workspace.h" #include "sway/tree/workspace.h"
#include "list.h"
#include "log.h" #include "log.h"
#include "stringop.h" #include "stringop.h"
@ -67,8 +68,7 @@ void container_destroy(struct sway_container *con) {
list_free(con->current.children); list_free(con->current.children);
list_free(con->outputs); list_free(con->outputs);
list_foreach(con->marks, free); free_flat_list(con->marks);
list_free(con->marks);
wlr_texture_destroy(con->marks_focused); wlr_texture_destroy(con->marks_focused);
wlr_texture_destroy(con->marks_focused_inactive); wlr_texture_destroy(con->marks_focused_inactive);
wlr_texture_destroy(con->marks_unfocused); 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) { 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; con->marks->length = 0;
ipc_event_window(con, "mark"); ipc_event_window(con, "mark");
} }

@ -365,11 +365,6 @@ static int parse_args(int argc, char *argv[]) {
return 0; 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) { static int handle_signal(int sig, void *data) {
switch (sig) { switch (sig) {
case SIGINT: case SIGINT:
@ -378,7 +373,9 @@ static int handle_signal(int sig, void *data) {
return 0; return 0;
case SIGUSR1: case SIGUSR1:
wlr_log(WLR_DEBUG, "Got 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; return 1;
} }
assert(false); // not reached assert(false); // not reached
@ -409,11 +406,6 @@ static int display_event(int fd, uint32_t mask, void *data) {
return count; 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[]) { int main(int argc, char *argv[]) {
if (parse_args(argc, argv) != 0) { if (parse_args(argc, argv) != 0) {
return -1; return -1;
@ -458,7 +450,10 @@ int main(int argc, char *argv[]) {
sway_terminate(0); 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); wl_display_roundtrip(state.display);

Loading…
Cancel
Save