From 29f6dd2a846473bf723d5b1d4c208c94542ac730 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Mon, 4 Nov 2013 12:09:13 +0400 Subject: [PATCH] Use g_list_free_full(). Signed-off-by: Andrew Borodin --- lib/glibcompat.c | 20 ++++++++++++++++++++ lib/glibcompat.h | 1 + lib/widget/dialog.c | 3 +-- lib/widget/history.c | 3 +-- lib/widget/input.c | 3 +-- lib/widget/listbox.c | 3 +-- lib/widget/menu.c | 9 +++------ src/editor/editcmd.c | 3 +-- src/filemanager/panel.c | 3 +-- src/filemanager/treestore.c | 3 +-- src/main.c | 6 ++---- src/viewer/actions_cmd.c | 3 +-- 12 files changed, 34 insertions(+), 26 deletions(-) diff --git a/lib/glibcompat.c b/lib/glibcompat.c index 290600c32..143d79d4e 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -114,6 +114,26 @@ g_slist_free_full (GSList * list, GDestroyNotify free_func) g_slist_foreach (list, (GFunc) free_func, NULL); g_slist_free (list); } + +/* --------------------------------------------------------------------------------------------- */ + +/** + * g_list_free_full: + * @list: a pointer to a #GList + * @free_func: the function to be called to free each element's data + * + * Convenience method, which frees all the memory used by a #GList, and + * calls the specified destroy function on every element's data. + * + * Since: 2.28 + */ +void +g_list_free_full (GList * list, GDestroyNotify free_func) +{ + g_list_foreach (list, (GFunc) free_func, NULL); + g_list_free (list); +} + #endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */ /* --------------------------------------------------------------------------------------------- */ diff --git a/lib/glibcompat.h b/lib/glibcompat.h index 6c93b03d7..bfaa98d60 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -21,6 +21,7 @@ int g_strcmp0 (const char *str1, const char *str2); #if ! GLIB_CHECK_VERSION (2, 28, 0) void g_slist_free_full (GSList * list, GDestroyNotify free_func); +void g_list_free_full (GList * list, GDestroyNotify free_func); #endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */ /*** inline functions ****************************************************************************/ diff --git a/lib/widget/dialog.c b/lib/widget/dialog.c index d9272a1cf..12e899f3f 100644 --- a/lib/widget/dialog.c +++ b/lib/widget/dialog.c @@ -1266,8 +1266,7 @@ dlg_destroy (WDialog * h) /* if some widgets have history, save all history at one moment here */ dlg_save_history (h); dlg_broadcast_msg (h, MSG_DESTROY); - g_list_foreach (h->widgets, (GFunc) g_free, NULL); - g_list_free (h->widgets); + g_list_free_full (h->widgets, g_free); mc_event_group_del (h->event_group); g_free (h->event_group); g_free (h->title); diff --git a/lib/widget/history.c b/lib/widget/history.c index fdc58285a..8e4a2113b 100644 --- a/lib/widget/history.c +++ b/lib/widget/history.c @@ -383,8 +383,7 @@ history_show (GList ** history, Widget * widget, int current) dlg_destroy (query_dlg); - g_list_foreach (*history, (GFunc) g_free, NULL); - g_list_free (*history); + g_list_free_full (*history, g_free); *history = g_list_last (z); return r; diff --git a/lib/widget/input.c b/lib/widget/input.c index a285f5b16..34fc952e1 100644 --- a/lib/widget/input.c +++ b/lib/widget/input.c @@ -900,8 +900,7 @@ input_destroy (WInput * in) { /* history is already saved before this moment */ in->history.list = g_list_first (in->history.list); - g_list_foreach (in->history.list, (GFunc) g_free, NULL); - g_list_free (in->history.list); + g_list_free_full (in->history.list, g_free); } g_free (in->history.name); g_free (in->buffer); diff --git a/lib/widget/listbox.c b/lib/widget/listbox.c index 4f44bdd63..1accc76fc 100644 --- a/lib/widget/listbox.c +++ b/lib/widget/listbox.c @@ -688,8 +688,7 @@ listbox_remove_list (WListbox * l) { if ((l != NULL) && (l->count != 0)) { - g_list_foreach (l->list, (GFunc) listbox_entry_free, NULL); - g_list_free (l->list); + g_list_free_full (l->list, listbox_entry_free); l->list = NULL; l->count = l->pos = l->top = 0; } diff --git a/lib/widget/menu.c b/lib/widget/menu.c index fb993fdc9..14ceab597 100644 --- a/lib/widget/menu.c +++ b/lib/widget/menu.c @@ -828,8 +828,7 @@ void destroy_menu (menu_t * menu) { release_hotkey (menu->text); - g_list_foreach (menu->entries, (GFunc) menu_entry_free, NULL); - g_list_free (menu->entries); + g_list_free_full (menu->entries, (GDestroyNotify) menu_entry_free); g_free (menu->help_node); g_free (menu); } @@ -860,10 +859,8 @@ menubar_set_menu (WMenuBar * menubar, GList * menu) { /* delete previous menu */ if (menubar->menu != NULL) - { - g_list_foreach (menubar->menu, (GFunc) destroy_menu, NULL); - g_list_free (menubar->menu); - } + g_list_free_full (menubar->menu, (GDestroyNotify) destroy_menu); + /* add new menu */ menubar->is_active = FALSE; menubar->is_dropped = FALSE; diff --git a/src/editor/editcmd.c b/src/editor/editcmd.c index 8c6b9206f..b726b850d 100644 --- a/src/editor/editcmd.c +++ b/src/editor/editcmd.c @@ -2708,8 +2708,7 @@ edit_search_cmd (WEdit * edit, gboolean again) edit->last_search_string = (char *) history->data; history->data = NULL; history = g_list_first (history); - g_list_foreach (history, (GFunc) g_free, NULL); - g_list_free (history); + g_list_free_full (history, g_free); #ifdef HAVE_CHARSET edit->search = mc_search_new (edit->last_search_string, -1, cp_source); diff --git a/src/filemanager/panel.c b/src/filemanager/panel.c index 71c77a0b1..cc9479bc7 100644 --- a/src/filemanager/panel.c +++ b/src/filemanager/panel.c @@ -1473,8 +1473,7 @@ panel_destroy (WPanel * p) { /* directory history is already saved before this moment */ p->dir_history = g_list_first (p->dir_history); - g_list_foreach (p->dir_history, (GFunc) g_free, NULL); - g_list_free (p->dir_history); + g_list_free_full (p->dir_history, g_free); } g_free (p->hist_name); diff --git a/src/filemanager/treestore.c b/src/filemanager/treestore.c index ee0b6dae4..61b177c39 100644 --- a/src/filemanager/treestore.c +++ b/src/filemanager/treestore.c @@ -900,8 +900,7 @@ tree_store_end_check (void) vfs_path_free (ts.check_name); ts.check_name = NULL; - g_list_foreach (the_queue, (GFunc) vfs_path_free, NULL); - g_list_free (the_queue); + g_list_free_full (the_queue, (GDestroyNotify) vfs_path_free); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/main.c b/src/main.c index c6c7d9788..b88e892a1 100644 --- a/src/main.c +++ b/src/main.c @@ -469,10 +469,8 @@ main (int argc, char *argv[]) if (mc_global.mc_run_mode != MC_RUN_EDITOR) g_free (mc_run_param0); else - { - g_list_foreach ((GList *) mc_run_param0, (GFunc) mcedit_arg_free, NULL); - g_list_free ((GList *) mc_run_param0); - } + g_list_free_full ((GList *) mc_run_param0, (GDestroyNotify) mcedit_arg_free); + g_free (mc_run_param1); g_free (saved_other_dir); diff --git a/src/viewer/actions_cmd.c b/src/viewer/actions_cmd.c index 0de67db3b..0b74e2908 100644 --- a/src/viewer/actions_cmd.c +++ b/src/viewer/actions_cmd.c @@ -143,8 +143,7 @@ mcview_continue_search_cmd (mcview_t * view) { view->last_search_string = (gchar *) g_strdup (history->data); history = g_list_first (history); - g_list_foreach (history, (GFunc) g_free, NULL); - g_list_free (history); + g_list_free_full (history, g_free); #ifdef HAVE_CHARSET view->search = mc_search_new (view->last_search_string, -1, cp_source);