Merge pull request #3271 from ianyfan/list-cleanup

list.c: Remove list_foreach
This commit is contained in:
Ryan Dwyer 2018-12-09 21:50:19 +10:00 committed by GitHub
commit b61a936c80
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 64 additions and 100 deletions

View file

@ -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;
@ -57,8 +48,7 @@ void list_del(list_t *list, int index) {
} }
void list_cat(list_t *list, list_t *source) { void list_cat(list_t *list, list_t *source) {
int i; for (int i = 0; i < source->length; ++i) {
for (i = 0; i < source->length; ++i) {
list_add(list, source->items[i]); list_add(list, source->items[i]);
} }
} }
@ -156,3 +146,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 list_free_items_and_destroy(list_t *list) {
if (!list) {
return;
}
for (int i = 0; i < list->length; ++i) {
free(list->items[i]);
}
list_free(list);
}

View file

@ -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); list_free_items_and_destroy(loop->fd_events);
list_foreach(loop->timers, free); list_free_items_and_destroy(loop->timers);
list_free(loop->fd_events);
list_free(loop->timers);
free(loop->fds); free(loop->fds);
free(loop); free(loop);
} }

View file

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

View file

@ -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 list_free_items_and_destroy(list_t *list);
#endif #endif

View file

@ -17,9 +17,8 @@ char *lenient_strncat(char *dest, const char *src, size_t len);
// strcmp that also handles null pointers. // strcmp that also handles null pointers.
int lenient_strcmp(char *a, char *b); 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 `list_free_items_and_destroy`
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);

View file

@ -43,8 +43,7 @@ struct cmd_results *checkarg(int argc, const char *name, enum expected_args type
} }
void apply_seat_config(struct seat_config *seat_config) { void apply_seat_config(struct seat_config *seat_config) {
int i; int i = list_seq_find(config->seat_configs, seat_name_cmp, seat_config->name);
i = list_seq_find(config->seat_configs, seat_name_cmp, seat_config->name);
if (i >= 0) { if (i >= 0) {
// merge existing config // merge existing config
struct seat_config *sc = config->seat_configs->items[i]; struct seat_config *sc = config->seat_configs->items[i];

View file

@ -20,15 +20,14 @@ struct cmd_results *bar_cmd_modifier(int argc, char **argv) {
uint32_t tmp_mod; uint32_t tmp_mod;
if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) { if ((tmp_mod = get_modifier_mask_by_name(split->items[i])) > 0) {
mod |= tmp_mod; mod |= tmp_mod;
continue;
} else { } else {
error = cmd_results_new(CMD_INVALID, "modifier", error = cmd_results_new(CMD_INVALID, "modifier",
"Unknown modifier '%s'", split->items[i]); "Unknown modifier '%s'", split->items[i]);
free_flat_list(split); list_free_items_and_destroy(split);
return error; return error;
} }
} }
free_flat_list(split); list_free_items_and_destroy(split);
config->current_bar->modifier = mod; config->current_bar->modifier = mod;
wlr_log(WLR_DEBUG, wlr_log(WLR_DEBUG,
"Show/Hide the bar when pressing '%s' in hide mode.", argv[0]); "Show/Hide the bar when pressing '%s' in hide mode.", argv[0]);

View file

@ -23,9 +23,7 @@ void free_sway_binding(struct sway_binding *binding) {
return; return;
} }
if (binding->keys) { list_free_items_and_destroy(binding->keys);
free_flat_list(binding->keys);
}
free(binding->input); free(binding->input);
free(binding->command); free(binding->command);
free(binding); free(binding);
@ -80,7 +78,6 @@ static int key_qsort_cmp(const void *keyp_a, const void *keyp_b) {
return (key_a < key_b) ? -1 : ((key_a > key_b) ? 1 : 0); return (key_a < key_b) ? -1 : ((key_a > key_b) ? 1 : 0);
} }
/** /**
* From a keycode, bindcode, or bindsym name and the most likely binding type, * From a keycode, bindcode, or bindsym name and the most likely binding type,
* identify the appropriate numeric value corresponding to the key. Return NULL * identify the appropriate numeric value corresponding to the key. Return NULL
@ -223,14 +220,14 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
uint32_t *key = calloc(1, sizeof(uint32_t)); uint32_t *key = calloc(1, sizeof(uint32_t));
if (!key) { if (!key) {
free_sway_binding(binding); free_sway_binding(binding);
free_flat_list(split); list_free_items_and_destroy(split);
return cmd_results_new(CMD_FAILURE, bindtype, return cmd_results_new(CMD_FAILURE, bindtype,
"Unable to allocate binding key"); "Unable to allocate binding key");
} }
*key = key_val; *key = key_val;
list_add(binding->keys, key); list_add(binding->keys, key);
} }
free_flat_list(split); list_free_items_and_destroy(split);
binding->order = binding_order++; binding->order = binding_order++;
// refine region of interest for mouse binding once we are certain // refine region of interest for mouse binding once we are certain
@ -280,7 +277,6 @@ static struct cmd_results *cmd_bindsym_or_bindcode(int argc, char **argv,
wlr_log(WLR_DEBUG, "%s - Bound %s to command `%s` for device '%s'", wlr_log(WLR_DEBUG, "%s - Bound %s to command `%s` for device '%s'",
bindtype, argv[0], binding->command, binding->input); bindtype, argv[0], binding->command, binding->input);
return cmd_results_new(CMD_SUCCESS, NULL, NULL); return cmd_results_new(CMD_SUCCESS, NULL, NULL);
} }
struct cmd_results *cmd_bindsym(int argc, char **argv) { struct cmd_results *cmd_bindsym(int argc, char **argv) {
@ -291,7 +287,6 @@ struct cmd_results *cmd_bindcode(int argc, char **argv) {
return cmd_bindsym_or_bindcode(argc, argv, true); return cmd_bindsym_or_bindcode(argc, argv, true);
} }
/** /**
* Execute the command associated to a binding * Execute the command associated to a binding
*/ */
@ -301,15 +296,14 @@ void seat_execute_command(struct sway_seat *seat, struct sway_binding *binding)
config->handler_context.seat = seat; config->handler_context.seat = seat;
list_t *res_list = execute_command(binding->command, NULL, NULL); list_t *res_list = execute_command(binding->command, NULL, NULL);
bool success = true; bool success = true;
while (res_list->length) { for (int i = 0; i < res_list->length; ++i) {
struct cmd_results *results = res_list->items[0]; struct cmd_results *results = res_list->items[i];
if (results->status != CMD_SUCCESS) { if (results->status != CMD_SUCCESS) {
wlr_log(WLR_DEBUG, "could not run command for binding: %s (%s)", wlr_log(WLR_DEBUG, "could not run command for binding: %s (%s)",
binding->command, results->error); binding->command, results->error);
success = false; success = false;
} }
free_cmd_results(results); free_cmd_results(results);
list_del(res_list, 0);
} }
list_free(res_list); list_free(res_list);
if (success) { if (success) {

View file

@ -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); list_free_items_and_destroy(bar_ids);
list_free(bar_ids);
return; return;
} }
@ -42,9 +41,7 @@ static void do_reload(void *data) {
} }
} }
} }
list_free_items_and_destroy(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);

View file

@ -33,7 +33,7 @@ static struct workspace_config *workspace_config_find_or_create(char *ws_name) {
void free_workspace_config(struct workspace_config *wsc) { void free_workspace_config(struct workspace_config *wsc) {
free(wsc->workspace); free(wsc->workspace);
free_flat_list(wsc->outputs); list_free_items_and_destroy(wsc->outputs);
free(wsc); free(wsc);
} }

View file

@ -38,26 +38,24 @@
struct sway_config *config = NULL; struct sway_config *config = NULL;
static void free_mode(struct sway_mode *mode) { static void free_mode(struct sway_mode *mode) {
int i;
if (!mode) { if (!mode) {
return; return;
} }
free(mode->name); free(mode->name);
if (mode->keysym_bindings) { if (mode->keysym_bindings) {
for (i = 0; i < mode->keysym_bindings->length; i++) { for (int i = 0; i < mode->keysym_bindings->length; i++) {
free_sway_binding(mode->keysym_bindings->items[i]); free_sway_binding(mode->keysym_bindings->items[i]);
} }
list_free(mode->keysym_bindings); list_free(mode->keysym_bindings);
} }
if (mode->keycode_bindings) { if (mode->keycode_bindings) {
for (i = 0; i < mode->keycode_bindings->length; i++) { for (int i = 0; i < mode->keycode_bindings->length; i++) {
free_sway_binding(mode->keycode_bindings->items[i]); free_sway_binding(mode->keycode_bindings->items[i]);
} }
list_free(mode->keycode_bindings); list_free(mode->keycode_bindings);
} }
if (mode->mouse_bindings) { if (mode->mouse_bindings) {
for (i = 0; i < mode->mouse_bindings->length; i++) { for (int i = 0; i < mode->mouse_bindings->length; i++) {
free_sway_binding(mode->mouse_bindings->items[i]); free_sway_binding(mode->mouse_bindings->items[i]);
} }
list_free(mode->mouse_bindings); list_free(mode->mouse_bindings);
@ -446,7 +444,7 @@ bool load_main_config(const char *file, bool is_active, bool validating) {
} }
} }
free_flat_list(secconfigs); list_free_items_and_destroy(secconfigs);
} }
*/ */
@ -654,8 +652,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); list_free_items_and_destroy(stack);
list_free(stack);
free(line); free(line);
return false; return false;
} }
@ -684,8 +681,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); list_free_items_and_destroy(stack);
list_free(stack);
free(line); free(line);
return false; return false;
} }
@ -750,8 +746,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); list_free_items_and_destroy(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;

View file

@ -49,13 +49,10 @@ void free_bar_config(struct bar_config *bar) {
free(bar->font); free(bar->font);
free(bar->separator_symbol); free(bar->separator_symbol);
for (int i = 0; i < bar->bindings->length; i++) { for (int i = 0; i < bar->bindings->length; i++) {
struct bar_binding *binding = bar->bindings->items[i]; free_bar_binding(bar->bindings->items[i]);
free_bar_binding(binding);
} }
list_free(bar->bindings); list_free(bar->bindings);
if (bar->outputs) { list_free_items_and_destroy(bar->outputs);
free_flat_list(bar->outputs);
}
if (bar->pid != 0) { if (bar->pid != 0) {
terminate_swaybar(bar->pid); terminate_swaybar(bar->pid);
} }

View file

@ -117,11 +117,8 @@ void free_seat_config(struct seat_config *seat) {
free(seat->name); free(seat->name);
for (int i = 0; i < seat->attachments->length; ++i) { for (int i = 0; i < seat->attachments->length; ++i) {
struct seat_attachment_config *attachment = seat_attachment_config_free(seat->attachments->items[i]);
seat->attachments->items[i];
seat_attachment_config_free(attachment);
} }
list_free(seat->attachments); list_free(seat->attachments);
free(seat); free(seat);
} }

View file

@ -73,14 +73,11 @@ static void handle_display_destroy(struct wl_listener *listener, void *data) {
unlink(ipc_sockaddr->sun_path); unlink(ipc_sockaddr->sun_path);
while (ipc_client_list->length) { while (ipc_client_list->length) {
struct ipc_client *client = ipc_client_list->items[0]; ipc_client_disconnect(ipc_client_list->items[ipc_client_list->length-1]);
ipc_client_disconnect(client);
} }
list_free(ipc_client_list); list_free(ipc_client_list);
if (ipc_sockaddr) {
free(ipc_sockaddr); free(ipc_sockaddr);
}
wl_list_remove(&ipc_display_destroy.link); wl_list_remove(&ipc_display_destroy.link);
} }

View file

@ -393,13 +393,12 @@ int main(int argc, char **argv) {
while (config->cmd_queue->length) { while (config->cmd_queue->length) {
char *line = config->cmd_queue->items[0]; char *line = config->cmd_queue->items[0];
list_t *res_list = execute_command(line, NULL, NULL); list_t *res_list = execute_command(line, NULL, NULL);
while (res_list->length) { for (int i = 0; i < res_list->length; ++i) {
struct cmd_results *res = res_list->items[0]; struct cmd_results *res = res_list->items[i];
if (res->status != CMD_SUCCESS) { if (res->status != CMD_SUCCESS) {
wlr_log(WLR_ERROR, "Error on line '%s': %s", line, res->error); wlr_log(WLR_ERROR, "Error on line '%s': %s", line, res->error);
} }
free_cmd_results(res); free_cmd_results(res);
list_del(res_list, 0);
} }
list_free(res_list); list_free(res_list);
free(line); free(line);

View file

@ -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); list_free_items_and_destroy(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");
} }

View file

@ -142,7 +142,7 @@ void workspace_destroy(struct sway_workspace *workspace) {
free(workspace->name); free(workspace->name);
free(workspace->representation); free(workspace->representation);
free_flat_list(workspace->output_priority); list_free_items_and_destroy(workspace->output_priority);
list_free(workspace->floating); list_free(workspace->floating);
list_free(workspace->tiling); list_free(workspace->tiling);
list_free(workspace->current.floating); list_free(workspace->current.floating);

View file

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

View file

@ -407,9 +407,8 @@ void swaynag_destroy(struct swaynag *swaynag) {
swaynag->run_display = false; swaynag->run_display = false;
free(swaynag->message); free(swaynag->message);
while (swaynag->buttons->length) { for (int i = 0; i < swaynag->buttons->length; ++i) {
struct swaynag_button *button = swaynag->buttons->items[0]; struct swaynag_button *button = swaynag->buttons->items[i];
list_del(swaynag->buttons, 0);
free(button->text); free(button->text);
free(button->action); free(button->action);
free(button); free(button);

View file

@ -147,10 +147,8 @@ void swaynag_type_free(struct swaynag_type *type) {
} }
void swaynag_types_free(list_t *types) { void swaynag_types_free(list_t *types) {
while (types->length) { for (int i = 0; i < types->length; ++i) {
struct swaynag_type *type = types->items[0]; swaynag_type_free(types->items[i]);
swaynag_type_free(type);
list_del(types, 0);
} }
list_free(types); list_free(types);
} }