From ad39aef8a6358845de55fda7801906d4928bfd13 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Tue, 2 Aug 2011 13:00:06 +0300 Subject: [PATCH] obexd: Make use of g_slist_free_full when elements are dynamically-allocated This avoid having to iterate twice in the list to free its elements. --- obexd/plugins/bluetooth.c | 9 ++++----- obexd/plugins/pbap.c | 8 +++++--- obexd/plugins/phonebook-dummy.c | 4 ++-- obexd/plugins/phonebook-ebook.c | 1 + obexd/plugins/phonebook-tracker.c | 6 +++--- obexd/plugins/vcard.c | 22 ++++++++++------------ obexd/src/glib-helper.h | 30 ++++++++++++++++++++++++++++++ 7 files changed, 55 insertions(+), 25 deletions(-) create mode 100644 obexd/src/glib-helper.h diff --git a/obexd/plugins/bluetooth.c b/obexd/plugins/bluetooth.c index b1267178c..1fccf3e33 100644 --- a/obexd/plugins/bluetooth.c +++ b/obexd/plugins/bluetooth.c @@ -42,6 +42,7 @@ #include "service.h" #include "log.h" #include "btio.h" +#include "glib-helper.h" #define BT_RX_MTU 32767 #define BT_TX_MTU 32767 @@ -546,7 +547,7 @@ static void *bluetooth_start(struct obex_server *server, int *err) return ios; } -static void stop(gpointer data, gpointer user_data) +static void stop(gpointer data) { GIOChannel *io = data; @@ -558,8 +559,7 @@ static void bluetooth_stop(void *data) { GSList *ios = data; - g_slist_foreach(ios, stop, NULL); - g_slist_free(ios); + g_slist_free_full(ios, stop); } static struct obex_transport_driver driver = { @@ -589,8 +589,7 @@ static void bluetooth_exit(void) g_dbus_remove_watch(connection, listener_id); if (any) { - g_slist_foreach(any->services, (GFunc) g_free, NULL); - g_slist_free(any->services); + g_slist_free_full(any->services, g_free); g_free(any->path); g_free(any); } diff --git a/obexd/plugins/pbap.c b/obexd/plugins/pbap.c index 4892d7a9b..e10fa8cb6 100644 --- a/obexd/plugins/pbap.c +++ b/obexd/plugins/pbap.c @@ -48,6 +48,7 @@ #include "mimetype.h" #include "filesystem.h" #include "dbus.h" +#include "glib-helper.h" #define PHONEBOOK_TYPE "x-bt/phonebook" #define VCARDLISTING_TYPE "x-bt/vcard-listing" @@ -160,8 +161,10 @@ static const uint8_t PBAP_TARGET[TARGET_SIZE] = { typedef int (*cache_entry_find_f) (const struct cache_entry *entry, const char *value); -static void cache_entry_free(struct cache_entry *entry) +static void cache_entry_free(void *data) { + struct cache_entry *entry = data; + g_free(entry->id); g_free(entry->name); g_free(entry->sound); @@ -222,8 +225,7 @@ static const char *cache_find(struct cache *cache, uint32_t handle) static void cache_clear(struct cache *cache) { - g_slist_foreach(cache->entries, (GFunc) cache_entry_free, NULL); - g_slist_free(cache->entries); + g_slist_free_full(cache->entries, cache_entry_free); cache->entries = NULL; } diff --git a/obexd/plugins/phonebook-dummy.c b/obexd/plugins/phonebook-dummy.c index ede4643fe..d026c5a96 100644 --- a/obexd/plugins/phonebook-dummy.c +++ b/obexd/plugins/phonebook-dummy.c @@ -43,6 +43,7 @@ #include "log.h" #include "phonebook.h" +#include "glib-helper.h" typedef void (*vcard_func_t) (const char *file, VObject *vo, void *user_data); @@ -186,8 +187,7 @@ static int foreach_vcard(DIR *dp, vcard_func_t func, uint16_t offset, close(fd); } - g_slist_foreach(sorted, (GFunc) g_free, NULL); - g_slist_free(sorted); + g_slist_free_full(sorted, g_free); if (count) *count = n; diff --git a/obexd/plugins/phonebook-ebook.c b/obexd/plugins/phonebook-ebook.c index 683037ab2..e53da12d8 100644 --- a/obexd/plugins/phonebook-ebook.c +++ b/obexd/plugins/phonebook-ebook.c @@ -40,6 +40,7 @@ #include "obex.h" #include "service.h" #include "phonebook.h" +#include "glib-helper.h" #define QUERY_FN "(contains \"family_name\" \"%s\")" #define QUERY_NAME "(contains \"given_name\" \"%s\")" diff --git a/obexd/plugins/phonebook-tracker.c b/obexd/plugins/phonebook-tracker.c index 29dd05cb2..3ac1c4410 100644 --- a/obexd/plugins/phonebook-tracker.c +++ b/obexd/plugins/phonebook-tracker.c @@ -38,6 +38,7 @@ #include "phonebook.h" #include "dbus.h" #include "vcard.h" +#include "glib-helper.h" #define TRACKER_SERVICE "org.freedesktop.Tracker1" #define TRACKER_RESOURCES_PATH "/org/freedesktop/Tracker1/Resources" @@ -1440,15 +1441,14 @@ static gboolean find_checked_number(GSList *numbers, const char *number) return FALSE; } -static void gstring_free_helper(gpointer data, gpointer user_data) +static void gstring_free_helper(gpointer data) { g_string_free(data, TRUE); } static void free_data_numbers(struct phonebook_data *data) { - g_slist_foreach(data->numbers, gstring_free_helper, NULL); - g_slist_free(data->numbers); + g_slist_free_full(data->numbers, gstring_free_helper); data->numbers = NULL; } diff --git a/obexd/plugins/vcard.c b/obexd/plugins/vcard.c index 30841b7ba..4d126873d 100644 --- a/obexd/plugins/vcard.c +++ b/obexd/plugins/vcard.c @@ -19,6 +19,10 @@ * */ +#ifdef HAVE_CONFIG_H +#include +#endif + #include #include #include @@ -30,6 +34,7 @@ #include #include "vcard.h" +#include "glib-helper.h" #define ADDR_FIELD_AMOUNT 7 #define LEN_MAX 128 @@ -614,7 +619,7 @@ void phonebook_add_contact(GString *vcards, struct phonebook_contact *contact, } -static void field_free(gpointer data, gpointer user_data) +static void field_free(gpointer data) { struct phonebook_field *field = data; @@ -627,17 +632,10 @@ void phonebook_contact_free(struct phonebook_contact *contact) if (contact == NULL) return; - g_slist_foreach(contact->numbers, field_free, NULL); - g_slist_free(contact->numbers); - - g_slist_foreach(contact->emails, field_free, NULL); - g_slist_free(contact->emails); - - g_slist_foreach(contact->addresses, field_free, NULL); - g_slist_free(contact->addresses); - - g_slist_foreach(contact->urls, field_free, NULL); - g_slist_free(contact->urls); + g_slist_free_full(contact->numbers, field_free); + g_slist_free_full(contact->emails, field_free); + g_slist_free_full(contact->addresses, field_free); + g_slist_free_full(contact->urls, field_free); g_free(contact->uid); g_free(contact->fullname); diff --git a/obexd/src/glib-helper.h b/obexd/src/glib-helper.h new file mode 100644 index 000000000..695d719c3 --- /dev/null +++ b/obexd/src/glib-helper.h @@ -0,0 +1,30 @@ +/* + * + * BlueZ - Bluetooth protocol stack for Linux + * + * Copyright (C) 2004-2010 Marcel Holtmann + * + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifdef NEED_G_SLIST_FREE_FULL +static inline void g_slist_free_full(GSList *list, GDestroyNotify free_func) +{ + g_slist_foreach(list, (GFunc) free_func, NULL); + g_slist_free(list); +} +#endif -- 2.47.3