From 57c303eeaff9a793a5475c3823fc2662b100f106 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 1 Dec 2019 14:59:35 +0300 Subject: [PATCH] Use g_clear_slist() and g_clear_list(). Signed-off-by: Andrew Borodin --- lib/glibcompat.c | 65 +++++++++++++++++++++++++++++++++++++ lib/glibcompat.h | 5 +++ lib/widget/menu.c | 5 ++- src/editor/syntax.c | 3 +- src/filemanager/mountlist.c | 3 +- src/help.c | 3 +- src/vfs/cpio/cpio.c | 4 +-- src/vfs/smbfs/smbfs.c | 7 +--- 8 files changed, 78 insertions(+), 17 deletions(-) diff --git a/lib/glibcompat.c b/lib/glibcompat.c index d2ca0b6eb..48a2409e5 100644 --- a/lib/glibcompat.c +++ b/lib/glibcompat.c @@ -91,6 +91,71 @@ g_list_free_full (GList * list, GDestroyNotify free_func) #endif /* ! GLIB_CHECK_VERSION (2, 28, 0) */ +#if ! GLIB_CHECK_VERSION (2, 64, 0) +/** + * g_clear_slist: (skip) + * @slist_ptr: (not nullable): a #GSList return location + * @destroy: (nullable): the function to pass to g_slist_free_full() or NULL to not free elements + * + * Clears a pointer to a #GSList, freeing it and, optionally, freeing its elements using @destroy. + * + * @slist_ptr must be a valid pointer. If @slist_ptr points to a null #GSList, this does nothing. + * + * Since: 2.64 + */ +void +g_clear_slist (GSList ** slist_ptr, GDestroyNotify destroy) +{ + GSList *slist; + + slist = *slist_ptr; + + if (slist != NULL) + { + *slist_ptr = NULL; + + if (destroy != NULL) + g_slist_free_full (slist, destroy); + else + g_slist_free (slist); + } +} + +/* --------------------------------------------------------------------------------------------- */ + +/** + * g_clear_list: + * @list_ptr: (not nullable): a #GList return location + * @destroy: (nullable): the function to pass to g_list_free_full() or NULL to not free elements + * + * Clears a pointer to a #GList, freeing it and, optionally, freeing its elements using @destroy. + * + * @list_ptr must be a valid pointer. If @list_ptr points to a null #GList, this does nothing. + * + * Since: 2.64 + */ +void +g_clear_list (GList ** list_ptr, GDestroyNotify destroy) +{ + GList *list; + + list = *list_ptr; + + if (list != NULL) + { + *list_ptr = NULL; + + if (destroy != NULL) + g_list_free_full (list, destroy); + else + g_list_free (list); + } +} + +/* --------------------------------------------------------------------------------------------- */ + +#endif /* ! GLIB_CHECK_VERSION (2, 64, 0) */ + #if ! GLIB_CHECK_VERSION (2, 32, 0) /** * g_queue_free_full: diff --git a/lib/glibcompat.h b/lib/glibcompat.h index d61b76aef..94f7737a7 100644 --- a/lib/glibcompat.h +++ b/lib/glibcompat.h @@ -16,6 +16,11 @@ 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) */ +#if ! GLIB_CHECK_VERSION (2, 64, 0) +void g_clear_slist (GSList ** slist_ptr, GDestroyNotify destroy); +void g_clear_list (GList ** list_ptr, GDestroyNotify destroy); +#endif /* ! GLIB_CHECK_VERSION (2, 64, 0) */ + #if ! GLIB_CHECK_VERSION (2, 32, 0) void g_queue_free_full (GQueue * queue, GDestroyNotify free_func); #endif /* ! GLIB_CHECK_VERSION (2, 32, 0) */ diff --git a/lib/widget/menu.c b/lib/widget/menu.c index 14023ea76..1e088a407 100644 --- a/lib/widget/menu.c +++ b/lib/widget/menu.c @@ -622,11 +622,10 @@ menubar_refresh (WMenuBar * menubar) /* --------------------------------------------------------------------------------------------- */ -static void +static inline void menubar_free_menu (WMenuBar * menubar) { - if (menubar->menu != NULL) - g_list_free_full (menubar->menu, (GDestroyNotify) destroy_menu); + g_clear_list (&menubar->menu, (GDestroyNotify) destroy_menu); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/editor/syntax.c b/src/editor/syntax.c index 0ac319048..5ab92da83 100644 --- a/src/editor/syntax.c +++ b/src/editor/syntax.c @@ -1448,8 +1448,7 @@ edit_free_syntax_rules (WEdit * edit) g_ptr_array_foreach (edit->rules, (GFunc) context_rule_free, NULL); g_ptr_array_free (edit->rules, TRUE); edit->rules = NULL; - g_slist_free_full (edit->syntax_marker, g_free); - edit->syntax_marker = NULL; + g_clear_slist (&edit->syntax_marker, g_free); tty_color_free_all_tmp (); } diff --git a/src/filemanager/mountlist.c b/src/filemanager/mountlist.c index 1c4cf70e1..96d59352c 100644 --- a/src/filemanager/mountlist.c +++ b/src/filemanager/mountlist.c @@ -1461,8 +1461,7 @@ void free_my_statfs (void) { #ifdef HAVE_INFOMOUNT_LIST - g_slist_free_full (mc_mount_list, (GDestroyNotify) free_mount_entry); - mc_mount_list = NULL; + g_clear_slist (&mc_mount_list, (GDestroyNotify) free_mount_entry); #endif /* HAVE_INFOMOUNT_LIST */ } diff --git a/src/help.c b/src/help.c index 94615ab30..2ea3ffe13 100644 --- a/src/help.c +++ b/src/help.c @@ -393,8 +393,7 @@ end_link_area (int x, int y) static void clear_link_areas (void) { - g_slist_free_full (link_area, g_free); - link_area = NULL; + g_clear_slist (&link_area, g_free); inside_link_area = FALSE; } diff --git a/src/vfs/cpio/cpio.c b/src/vfs/cpio/cpio.c index 0cdce7ca6..531d06553 100644 --- a/src/vfs/cpio/cpio.c +++ b/src/vfs/cpio/cpio.c @@ -217,8 +217,8 @@ cpio_free_archive (struct vfs_class *me, struct vfs_s_super *super) mc_close (arch->fd); arch->fd = -1; } - g_slist_free_full (arch->deferred, g_free); - arch->deferred = NULL; + + g_clear_slist (&arch->deferred, g_free); } /* --------------------------------------------------------------------------------------------- */ diff --git a/src/vfs/smbfs/smbfs.c b/src/vfs/smbfs/smbfs.c index 681aba06f..245dd0164 100644 --- a/src/vfs/smbfs/smbfs.c +++ b/src/vfs/smbfs/smbfs.c @@ -224,12 +224,7 @@ smbfs_auth_free (struct smb_authinfo const *a) static void smbfs_auth_free_all (void) { - if (auth_list) - { - g_slist_foreach (auth_list, (GFunc) smbfs_auth_free, 0); - g_slist_free (auth_list); - auth_list = 0; - } + g_clear_slist (&auth_list, (GDestroyNotify) smbfs_auth_free); } /* --------------------------------------------------------------------------------------------- */