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.
This commit is contained in:
Ian Fan 2018-12-08 22:52:29 +00:00
parent 0c3f0dfd16
commit 19e831ed3d
11 changed files with 39 additions and 55 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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