summaryrefslogtreecommitdiff
path: root/glib/glist.c
diff options
context:
space:
mode:
authorErnestas Kulik <ekulik@redhat.com>2019-11-23 17:41:54 +0100
committerErnestas Kulik <ekulik@redhat.com>2019-11-25 13:09:25 +0100
commit58ba7d78fbd7ecb4c0df2dc7e251627ebbffb9d5 (patch)
tree94dd8cf21770d162617dc843f161be7d71b72828 /glib/glist.c
parent8555aeec8db4449de8bccea5b7da965e1d7ee7bf (diff)
downloadglib-58ba7d78fbd7ecb4c0df2dc7e251627ebbffb9d5.tar.gz
list, slist: Add g_clear_{s,}list()
Although not quite as often-occurring, this should help with constructs like this: if (list) { g_list_free_full (list, foo); list = NULL; } Closes https://gitlab.gnome.org/GNOME/glib/issues/1943
Diffstat (limited to 'glib/glist.c')
-rw-r--r--glib/glist.c29
1 files changed, 29 insertions, 0 deletions
diff --git a/glib/glist.c b/glib/glist.c
index 39143fa7e..7b64ba1d6 100644
--- a/glib/glist.c
+++ b/glib/glist.c
@@ -1313,3 +1313,32 @@ g_list_sort_with_data (GList *list,
{
return g_list_sort_real (list, (GFunc) compare_func, user_data);
}
+
+/**
+ * g_clear_list: (skip)
+ * @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)
+ {
+ *list_ptr = NULL;
+
+ if (destroy)
+ g_list_free_full (list, destroy);
+ else
+ g_list_free (list);
+ }
+}