From 3cdd6d22718b22f0f669d34777233912c1688ed6 Mon Sep 17 00:00:00 2001 From: David Benjamin Date: Sat, 11 Jun 2011 16:49:43 -0700 Subject: [PATCH] Add owl_ptr_array_free convenience function Unfortunately, most uses of GPtrArray here require a two-step chant which is really annoying. Until we require glib 2.22 and get g_ptr_array_new_with_free_func, use this helper function. --- buddylist.c | 3 +-- functions.c | 9 +++------ keymap.c | 3 +-- util.c | 14 ++++++++++---- zwrite.c | 3 +-- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/buddylist.c b/buddylist.c index 333424160..3977bfb1b 100644 --- a/buddylist.c +++ b/buddylist.c @@ -133,6 +133,5 @@ void owl_buddylist_clear(owl_buddylist *bl) void owl_buddylist_cleanup(owl_buddylist *bl) { - g_ptr_array_foreach(bl->buddies, (GFunc)owl_buddy_delete, NULL); - g_ptr_array_free(bl->buddies, true); + owl_ptr_array_free(bl->buddies, (GDestroyNotify)owl_buddy_delete); } diff --git a/functions.c b/functions.c index 5ce0a2a8e..ba72797b9 100644 --- a/functions.c +++ b/functions.c @@ -2778,8 +2778,7 @@ void owl_function_zpunt(const char *class, const char *inst, const char *recip, } owl_function_punt(argv->len, (const char *const*) argv->pdata, direction); - g_ptr_array_foreach(argv, (GFunc)g_free, NULL); - g_ptr_array_free(argv, true); + owl_ptr_array_free(argv, g_free); } void owl_function_punt(int argc, const char *const *argv, int direction) @@ -3072,8 +3071,7 @@ void owl_function_buddylist(int aim, int zephyr, const char *filename) } } } - g_ptr_array_foreach(anyone, (GFunc)g_free, NULL); - g_ptr_array_free(anyone, true); + owl_ptr_array_free(anyone, g_free); } } #endif @@ -3417,8 +3415,7 @@ void owl_function_zephyr_buddy_check(int notify) } } - g_ptr_array_foreach(anyone, (GFunc)g_free, NULL); - g_ptr_array_free(anyone, true); + owl_ptr_array_free(anyone, g_free); #endif } diff --git a/keymap.c b/keymap.c index f1a4675c7..5122f04e4 100644 --- a/keymap.c +++ b/keymap.c @@ -23,8 +23,7 @@ void owl_keymap_cleanup(owl_keymap *km) { g_free(km->name); g_free(km->desc); - g_ptr_array_foreach(km->bindings, (GFunc)owl_keybinding_delete, NULL); - g_ptr_array_free(km->bindings, true); + owl_ptr_array_free(km->bindings, (GDestroyNotify)owl_keybinding_delete); } void owl_keymap_set_parent(owl_keymap *km, const owl_keymap *parent) diff --git a/util.c b/util.c index be8a0221f..b7445dcef 100644 --- a/util.c +++ b/util.c @@ -97,6 +97,15 @@ CALLER_OWN char *owl_util_makepath(const char *in) return(out); } +void owl_ptr_array_free(GPtrArray *array, GDestroyNotify element_free_func) +{ + /* TODO: when we move to requiring glib 2.22+, use + * g_ptr_array_new_with_free_func instead. */ + if (element_free_func) + g_ptr_array_foreach(array, (GFunc)element_free_func, NULL); + g_ptr_array_free(array, true); +} + /* Break a command line up into argv, argc. The caller must free the returned values with g_strfreev. If there is an error argc will be set to -1, argv will be NULL and the caller does not need to free anything. The @@ -169,10 +178,7 @@ CALLER_OWN char **owl_parseline(const char *line, int *argc) /* check for unbalanced quotes */ if (quote!='\0') { - /* TODO: when we move to requiring glib 2.22+, use - * g_ptr_array_new_with_free_func. */ - g_ptr_array_foreach(argv, (GFunc)g_free, NULL); - g_ptr_array_free(argv, true); + owl_ptr_array_free(argv, g_free); if (argc) *argc = -1; return(NULL); } diff --git a/zwrite.c b/zwrite.c index 9dceac348..6c2004f2a 100644 --- a/zwrite.c +++ b/zwrite.c @@ -347,8 +347,7 @@ void owl_zwrite_delete(owl_zwrite *z) void owl_zwrite_cleanup(owl_zwrite *z) { - g_ptr_array_foreach(z->recips, (GFunc)g_free, NULL); - g_ptr_array_free(z->recips, true); + owl_ptr_array_free(z->recips, g_free); g_free(z->cmd); g_free(z->zwriteline); g_free(z->class);