Skip to content

Commit

Permalink
Add owl_ptr_array_free convenience function
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
davidben committed Jun 25, 2011
1 parent e4524da commit 3cdd6d2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
3 changes: 1 addition & 2 deletions buddylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
9 changes: 3 additions & 6 deletions functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand Down
3 changes: 1 addition & 2 deletions keymap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 10 additions & 4 deletions util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions zwrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3cdd6d2

Please sign in to comment.