From: Matthias Clasen Date: Mon, 11 Jul 2011 00:42:58 +0000 (-0400) Subject: Optimize g_[s]list_free_full a bit X-Git-Tag: 2.29.12~41 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=98b667d052b1274f80b8898a32d0753e9e2e5c1a;p=platform%2Fupstream%2Fglib.git Optimize g_[s]list_free_full a bit No need to iterate the list twice. Proposed by Luiz Augusto von Dentz, http://bugzilla.gnome.org/show_bug.cgi?id=653935 --- diff --git a/glib/glist.c b/glib/glist.c index 1e3dd86..7f5df44 100644 --- a/glib/glist.c +++ b/glib/glist.c @@ -212,10 +212,15 @@ g_list_free_1 (GList *list) */ void g_list_free_full (GList *list, - GDestroyNotify free_func) + GDestroyNotify free_func) { - g_list_foreach (list, (GFunc) free_func, NULL); - g_list_free (list); + while (list) + { + GList *next = list->next; + (*free_func) (list->data); + _g_list_free1 (list); + list = next; + } } /** diff --git a/glib/gslist.c b/glib/gslist.c index 1de9c57..96ba579 100644 --- a/glib/gslist.c +++ b/glib/gslist.c @@ -197,17 +197,22 @@ g_slist_free_1 (GSList *list) * @list: a pointer to a #GSList * @free_func: the function to be called to free each element's data * - * Convenience method, which frees all the memory used by a #GSList, and - * calls the specified destroy function on every element's data. + * Convenience method, which frees all the memory used by a #GSList, + * and calls the specified destroy function on every element's data. * * Since: 2.28 - **/ + */ void g_slist_free_full (GSList *list, - GDestroyNotify free_func) + GDestroyNotify free_func) { - g_slist_foreach (list, (GFunc) free_func, NULL); - g_slist_free (list); + while (list) + { + GSList *next = list->next; + (*free_func) (list->data); + _g_slist_free1 (list); + list = next; + } } /**