2011-06-29 08:52:48

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 1/6] Make use of g_slist_free_full when elements are dynamically-allocated

From: Luiz Augusto von Dentz <[email protected]>

This avoid having to iterate twice in the list to free its elements.
---
acinclude.m4 | 4 ++--
src/adapter.c | 33 ++++++++++++++-------------------
src/attrib-server.c | 18 ++++++------------
src/device.c | 14 ++++----------
src/eir.c | 3 +--
src/manager.c | 3 +--
src/storage.c | 6 ++----
7 files changed, 30 insertions(+), 51 deletions(-)

diff --git a/acinclude.m4 b/acinclude.m4
index 10e648d..a37959a 100644
--- a/acinclude.m4
+++ b/acinclude.m4
@@ -106,8 +106,8 @@ AC_DEFUN([AC_PATH_DBUS], [
])

AC_DEFUN([AC_PATH_GLIB], [
- PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.16, dummy=yes,
- AC_MSG_ERROR(GLib library version 2.16 or later is required))
+ PKG_CHECK_MODULES(GLIB, glib-2.0 >= 2.28, dummy=yes,
+ AC_MSG_ERROR(GLib library version 2.28 or later is required))
AC_SUBST(GLIB_CFLAGS)
AC_SUBST(GLIB_LIBS)
])
diff --git a/src/adapter.c b/src/adapter.c
index 99d3eee..0909a22 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -173,12 +173,13 @@ static int found_device_cmp(const struct remote_dev_info *d1,
return 0;
}

-static void dev_info_free(struct remote_dev_info *dev)
+static void dev_info_free(void *data)
{
+ struct remote_dev_info *dev = data;
+
g_free(dev->name);
g_free(dev->alias);
- g_slist_foreach(dev->services, (GFunc) g_free, NULL);
- g_slist_free(dev->services);
+ g_slist_free_full(dev->services, g_free);
g_strfreev(dev->uuids);
g_free(dev);
}
@@ -698,8 +699,10 @@ static void session_remove(struct session_req *req)
}
}

-static void session_free(struct session_req *req)
+static void session_free(void *data)
{
+ struct session_req *req = data;
+
if (req->id)
g_dbus_remove_watch(req->conn, req->id);

@@ -1181,8 +1184,7 @@ static DBusMessage *adapter_start_discovery(DBusConnection *conn,
if (adapter->disc_sessions)
goto done;

- g_slist_foreach(adapter->found_devices, (GFunc) dev_info_free, NULL);
- g_slist_free(adapter->found_devices);
+ g_slist_free_full(adapter->found_devices, dev_info_free);
adapter->found_devices = NULL;

g_slist_free(adapter->oor_devices);
@@ -1879,8 +1881,7 @@ static void create_stored_device_from_profiles(char *key, char *value,
if (list)
device_register_services(connection, device, list, ATT_PSM);

- g_slist_foreach(uuids, (GFunc) g_free, NULL);
- g_slist_free(uuids);
+ g_slist_free_full(uuids, g_free);
}

struct adapter_keys {
@@ -2081,8 +2082,7 @@ static void load_devices(struct btd_adapter *adapter)
if (err < 0) {
error("Unable to load keys to adapter_ops: %s (%d)",
strerror(-err), -err);
- g_slist_foreach(keys.keys, (GFunc) g_free, NULL);
- g_slist_free(keys.keys);
+ g_slist_free_full(keys.keys, g_free);
}

create_name(filename, PATH_MAX, STORAGEDIR, srcaddr, "blocked");
@@ -2166,8 +2166,7 @@ static void load_connections(struct btd_adapter *adapter)
adapter_add_connection(adapter, device);
}

- g_slist_foreach(conns, (GFunc) g_free, NULL);
- g_slist_free(conns);
+ g_slist_free_full(conns, g_free);
}

static int get_discoverable_timeout(const char *src)
@@ -2222,8 +2221,7 @@ static void emit_device_disappeared(gpointer data, gpointer user_data)
static void update_oor_devices(struct btd_adapter *adapter)
{
g_slist_foreach(adapter->oor_devices, emit_device_disappeared, adapter);
- g_slist_foreach(adapter->oor_devices, (GFunc) dev_info_free, NULL);
- g_slist_free(adapter->oor_devices);
+ g_slist_free_full(adapter->oor_devices, dev_info_free);
adapter->oor_devices = g_slist_copy(adapter->found_devices);
}

@@ -2400,9 +2398,7 @@ int btd_adapter_stop(struct btd_adapter *adapter)
stop_discovery(adapter);

if (adapter->disc_sessions) {
- g_slist_foreach(adapter->disc_sessions, (GFunc) session_free,
- NULL);
- g_slist_free(adapter->disc_sessions);
+ g_slist_free_full(adapter->disc_sessions, session_free);
adapter->disc_sessions = NULL;
}

@@ -2467,8 +2463,7 @@ static void adapter_free(gpointer user_data)

sdp_list_free(adapter->services, NULL);

- g_slist_foreach(adapter->found_devices, (GFunc) dev_info_free, NULL);
- g_slist_free(adapter->found_devices);
+ g_slist_free_full(adapter->found_devices, dev_info_free);

g_slist_free(adapter->oor_devices);

diff --git a/src/attrib-server.c b/src/attrib-server.c
index c55167f..c02d575 100644
--- a/src/attrib-server.c
+++ b/src/attrib-server.c
@@ -351,8 +351,7 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start,
status = a->read_cb(a, a->cb_user_data);

if (status) {
- g_slist_foreach(groups, (GFunc) g_free, NULL);
- g_slist_free(groups);
+ g_slist_free_full(groups, g_free);
return enc_error_resp(ATT_OP_READ_BY_GROUP_REQ,
a->handle, status, pdu, len);
}
@@ -399,8 +398,7 @@ static uint16_t read_by_group(struct gatt_channel *channel, uint16_t start,
length = enc_read_by_grp_resp(adl, pdu, len);

att_data_list_free(adl);
- g_slist_foreach(groups, (GFunc) g_free, NULL);
- g_slist_free(groups);
+ g_slist_free_full(groups, g_free);

return length;
}
@@ -614,8 +612,7 @@ static int find_by_type(uint16_t start, uint16_t end, bt_uuid_t *uuid,

len = enc_find_by_type_resp(matches, opdu, mtu);

- g_slist_foreach(matches, (GFunc) g_free, NULL);
- g_slist_free(matches);
+ g_slist_free_full(matches, g_free);

return len;
}
@@ -786,8 +783,7 @@ static void channel_disconnect(void *user_data)

g_slist_free(channel->notify);
g_slist_free(channel->indicate);
- g_slist_foreach(channel->configs, (GFunc) g_free, NULL);
- g_slist_free(channel->configs);
+ g_slist_free_full(channel->configs, g_free);

g_free(channel);
}
@@ -1132,8 +1128,7 @@ void attrib_server_exit(void)
{
GSList *l;

- g_slist_foreach(database, (GFunc) g_free, NULL);
- g_slist_free(database);
+ g_slist_free_full(database, g_free);

if (l2cap_io) {
g_io_channel_unref(l2cap_io);
@@ -1150,8 +1145,7 @@ void attrib_server_exit(void)

g_slist_free(channel->notify);
g_slist_free(channel->indicate);
- g_slist_foreach(channel->configs, (GFunc) g_free, NULL);
- g_slist_free(channel->configs);
+ g_slist_free_full(channel->configs, g_free);

g_attrib_unref(channel->attrib);
g_free(channel);
diff --git a/src/device.c b/src/device.c
index ae6a0d5..82759a4 100644
--- a/src/device.c
+++ b/src/device.c
@@ -156,8 +156,7 @@ static void browse_request_free(struct browse_req *req)
dbus_connection_unref(req->conn);
if (req->device)
btd_device_unref(req->device);
- g_slist_foreach(req->profiles_added, (GFunc) g_free, NULL);
- g_slist_free(req->profiles_added);
+ g_slist_free_full(req->profiles_added, g_free);
g_slist_free(req->profiles_removed);
if (req->records)
sdp_list_free(req->records, (sdp_free_func_t) sdp_record_free);
@@ -198,14 +197,9 @@ static void device_free(gpointer user_data)
agent_is_busy(agent, device->authr)))
agent_cancel(agent);

- g_slist_foreach(device->services, (GFunc) g_free, NULL);
- g_slist_free(device->services);
-
- g_slist_foreach(device->uuids, (GFunc) g_free, NULL);
- g_slist_free(device->uuids);
-
- g_slist_foreach(device->primaries, (GFunc) g_free, NULL);
- g_slist_free(device->primaries);
+ g_slist_free_full(device->services, g_free);
+ g_slist_free_full(device->uuids, g_free);
+ g_slist_free_full(device->primaries, g_free);

if (device->tmp_records)
sdp_list_free(device->tmp_records,
diff --git a/src/eir.c b/src/eir.c
index 01b6ac5..ac23064 100644
--- a/src/eir.c
+++ b/src/eir.c
@@ -47,8 +47,7 @@

void eir_data_free(struct eir_data *eir)
{
- g_slist_foreach(eir->services, (GFunc) g_free, NULL);
- g_slist_free(eir->services);
+ g_slist_free_full(eir->services, g_free);
g_free(eir->name);
}

diff --git a/src/manager.c b/src/manager.c
index 254ace4..dd8cb50 100644
--- a/src/manager.c
+++ b/src/manager.c
@@ -295,8 +295,7 @@ static void manager_remove_adapter(struct btd_adapter *adapter)

void manager_cleanup(DBusConnection *conn, const char *path)
{
- g_slist_foreach(adapters, (GFunc) manager_remove_adapter, NULL);
- g_slist_free(adapters);
+ g_slist_free_full(adapters, (GDestroyNotify) manager_remove_adapter);

g_dbus_unregister_interface(conn, "/", MANAGER_INTERFACE);
}
diff --git a/src/storage.c b/src/storage.c
index d416d75..73bbc36 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -1211,8 +1211,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba)
textfile_del(filename, key);
}

- g_slist_foreach(match.keys, (GFunc) g_free, NULL);
- g_slist_free(match.keys);
+ g_slist_free_full(match.keys, g_free);

/* Deleting all attributes values of a given address */
memset(&match, 0, sizeof(match));
@@ -1228,8 +1227,7 @@ int delete_device_service(const bdaddr_t *sba, const bdaddr_t *dba)
textfile_del(filename, key);
}

- g_slist_foreach(match.keys, (GFunc) g_free, NULL);
- g_slist_free(match.keys);
+ g_slist_free_full(match.keys, g_free);

return 0;
}
--
1.7.5.4



2011-06-29 20:18:33

by Johan Hedberg

[permalink] [raw]
Subject: Re: [PATCH BlueZ 1/6] Make use of g_slist_free_full when elements are dynamically-allocated

Hi Lucas,

On Wed, Jun 29, 2011, Lucas De Marchi wrote:
> Another thing that I think it'd be nice to use is
> g_io_channel_set_close_on_unref() instead of unref() + shutdown() in
> several places. What do you think?

You need to be careful with that since those two methods are not
identical: unref() + shutdown() will close the link immediately, whereas
close_on_unref() + unref() will not do anything (except the unref) if
there are any other references around. E.g. all IO callbacks own a
reference to the GIOChannel so doing a simple unref() on the GIOChannel
elsewhere will not actually close the link even if close_on_unref is
TRUE.

There are several places which expect the IO callback to check for
G_IO_NVAL and return FALSE if set (thereby removing the IO callback and
its GIOChannel reference). The only way you'd be able to convert such
cases is to change the unref() + shutdown() to a unref() +
g_source_remove(). However, this would eliminate most/some of the
simplification gain you're trying to accomplish.

Johan

2011-06-29 18:57:07

by Lucas De Marchi

[permalink] [raw]
Subject: Re: [PATCH BlueZ 1/6] Make use of g_slist_free_full when elements are dynamically-allocated

Hi Luiz,

On Wed, Jun 29, 2011 at 5:52 AM, Luiz Augusto von Dentz
<[email protected]> wrote:
> From: Luiz Augusto von Dentz <[email protected]>
>
> This avoid having to iterate twice in the list to free its elements.

I like this. Another thing that I think it'd be nice to use is
g_io_channel_set_close_on_unref() instead of unref() + shutdown() in
several places. What do you think?


Lucas De Marchi

2011-06-29 08:52:53

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 6/6] Make use of g_slist_free_full on hciops plugin

From: Luiz Augusto von Dentz <[email protected]>

---
plugins/hciops.c | 11 +++--------
1 files changed, 3 insertions(+), 8 deletions(-)

diff --git a/plugins/hciops.c b/plugins/hciops.c
index 147f7ea..92f976a 100644
--- a/plugins/hciops.c
+++ b/plugins/hciops.c
@@ -2236,14 +2236,9 @@ static void stop_hci_dev(int index)

hci_close_dev(dev->sk);

- g_slist_foreach(dev->keys, (GFunc) g_free, NULL);
- g_slist_free(dev->keys);
-
- g_slist_foreach(dev->uuids, (GFunc) g_free, NULL);
- g_slist_free(dev->uuids);
-
- g_slist_foreach(dev->connections, (GFunc) conn_free, NULL);
- g_slist_free(dev->connections);
+ g_slist_free_full(dev->keys, g_free);
+ g_slist_free_full(dev->uuids, g_free);
+ g_slist_free_full(dev->connections, g_free);

init_dev_info(index, -1, dev->registered, dev->already_up);
}
--
1.7.5.4


2011-06-29 08:52:52

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 5/6] Make use of g_slist_free_full on serial plugin

From: Luiz Augusto von Dentz <[email protected]>

---
serial/port.c | 13 +++++++------
1 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/serial/port.c b/serial/port.c
index 7d56faa..d011084 100644
--- a/serial/port.c
+++ b/serial/port.c
@@ -189,8 +189,9 @@ static int port_release(struct serial_port *port)
return -err;
}

-static void serial_port_free(struct serial_port *port)
+static void serial_port_free(void *data)
{
+ struct serial_port *port = data;
struct serial_device *device = port->device;

if (device && port->listener_id > 0)
@@ -202,8 +203,10 @@ static void serial_port_free(struct serial_port *port)
g_free(port);
}

-static void serial_device_free(struct serial_device *device)
+static void serial_device_free(void *data)
{
+ struct serial_device *device = data;
+
g_free(device->path);
if (device->conn)
dbus_connection_unref(device->conn);
@@ -232,8 +235,7 @@ static void path_unregister(void *data)

void port_release_all(void)
{
- g_slist_foreach(devices, (GFunc) serial_device_free, NULL);
- g_slist_free(devices);
+ g_slist_free_full(devices, serial_device_free);
}

static void open_notify(int fd, int err, struct serial_port *port)
@@ -613,8 +615,7 @@ int port_unregister(const char *path)
if (!device)
return -ENOENT;

- g_slist_foreach(device->ports, (GFunc) serial_port_free, NULL);
- g_slist_free(device->ports);
+ g_slist_free_full(device->ports, serial_port_free);

g_dbus_unregister_interface(device->conn, path, SERIAL_PORT_INTERFACE);

--
1.7.5.4


2011-06-29 08:52:51

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 4/6] Make use of g_slist_free_full on network plugin

From: Luiz Augusto von Dentz <[email protected]>

---
network/connection.c | 7 ++++---
network/server.c | 5 +----
2 files changed, 5 insertions(+), 7 deletions(-)

diff --git a/network/connection.c b/network/connection.c
index 181274a..ac27cf2 100644
--- a/network/connection.c
+++ b/network/connection.c
@@ -477,8 +477,10 @@ static DBusMessage *connection_get_properties(DBusConnection *conn,
return reply;
}

-static void connection_free(struct network_conn *nc)
+static void connection_free(void *data)
{
+ struct network_conn *nc = data;
+
if (nc->dc_id)
device_remove_disconnect_watch(nc->peer->device, nc->dc_id);

@@ -490,8 +492,7 @@ static void connection_free(struct network_conn *nc)

static void peer_free(struct network_peer *peer)
{
- g_slist_foreach(peer->connections, (GFunc) connection_free, NULL);
- g_slist_free(peer->connections);
+ g_slist_free_full(peer->connections, connection_free);
btd_device_unref(peer->device);
g_free(peer->path);
g_free(peer);
diff --git a/network/server.c b/network/server.c
index 5feb9d0..7865f39 100644
--- a/network/server.c
+++ b/network/server.c
@@ -677,10 +677,7 @@ static void server_free(struct network_server *ns)
g_free(ns->name);
g_free(ns->bridge);

- if (ns->sessions) {
- g_slist_foreach(ns->sessions, (GFunc) session_free, NULL);
- g_slist_free(ns->sessions);
- }
+ g_slist_free_full(ns->sessions, session_free);

g_free(ns);
}
--
1.7.5.4


2011-06-29 08:52:50

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 3/6] Make use of g_slist_free_full on audio plugin

From: Luiz Augusto von Dentz <[email protected]>

---
audio/a2dp.c | 16 +++++-----------
audio/avdtp.c | 13 ++++---------
audio/headset.c | 6 ++----
audio/media.c | 5 ++---
audio/telephony-maemo6.c | 17 +++++++++--------
audio/telephony-ofono.c | 24 ++++++++++++++++--------
audio/unix.c | 19 +++++++------------
7 files changed, 45 insertions(+), 55 deletions(-)

diff --git a/audio/a2dp.c b/audio/a2dp.c
index 03a18ab..01a378c 100644
--- a/audio/a2dp.c
+++ b/audio/a2dp.c
@@ -163,10 +163,8 @@ static void setup_free(struct a2dp_setup *s)
setups = g_slist_remove(setups, s);
if (s->session)
avdtp_unref(s->session);
- g_slist_foreach(s->cb, (GFunc) g_free, NULL);
- g_slist_free(s->cb);
- g_slist_foreach(s->caps, (GFunc) g_free, NULL);
- g_slist_free(s->caps);
+ g_slist_free_full(s->cb, g_free);
+ g_slist_free_full(s->caps, g_free);
g_free(s);
}

@@ -1601,11 +1599,8 @@ void a2dp_unregister(const bdaddr_t *src)
if (!server)
return;

- g_slist_foreach(server->sinks, (GFunc) a2dp_remove_sep, NULL);
- g_slist_free(server->sinks);
-
- g_slist_foreach(server->sources, (GFunc) a2dp_remove_sep, NULL);
- g_slist_free(server->sources);
+ g_slist_free_full(server->sinks, (GDestroyNotify) a2dp_remove_sep);
+ g_slist_free_full(server->sources, (GDestroyNotify) a2dp_remove_sep);

avdtp_exit(src);

@@ -2126,8 +2121,7 @@ unsigned int a2dp_config(struct avdtp *session, struct a2dp_sep *sep,

/* Copy given caps if they are different than current caps */
if (setup->caps != caps) {
- g_slist_foreach(setup->caps, (GFunc) g_free, NULL);
- g_slist_free(setup->caps);
+ g_slist_free_full(setup->caps, g_free);
setup->caps = g_slist_copy(caps);
}

diff --git a/audio/avdtp.c b/audio/avdtp.c
index fd388a8..d6a8b6e 100644
--- a/audio/avdtp.c
+++ b/audio/avdtp.c
@@ -800,11 +800,8 @@ static void stream_free(struct avdtp_stream *stream)
if (stream->io_id)
g_source_remove(stream->io_id);

- g_slist_foreach(stream->callbacks, (GFunc) g_free, NULL);
- g_slist_free(stream->callbacks);
-
- g_slist_foreach(stream->caps, (GFunc) g_free, NULL);
- g_slist_free(stream->caps);
+ g_slist_free_full(stream->callbacks, g_free);
+ g_slist_free_full(stream->callbacks, g_free);

g_free(stream);
}
@@ -1239,8 +1236,7 @@ void avdtp_unref(struct avdtp *session)
if (session->req)
pending_req_free(session->req);

- g_slist_foreach(session->seps, (GFunc) g_free, NULL);
- g_slist_free(session->seps);
+ g_slist_free_full(session->seps, g_free);

g_free(session->buf);

@@ -2869,8 +2865,7 @@ static gboolean avdtp_get_capabilities_resp(struct avdtp *session,
sep->type, sep->media_type);

if (sep->caps) {
- g_slist_foreach(sep->caps, (GFunc) g_free, NULL);
- g_slist_free(sep->caps);
+ g_slist_free_full(sep->caps, g_free);
sep->caps = NULL;
sep->codec = NULL;
sep->delay_reporting = FALSE;
diff --git a/audio/headset.c b/audio/headset.c
index ed913b9..72411d4 100644
--- a/audio/headset.c
+++ b/audio/headset.c
@@ -457,8 +457,7 @@ static void pending_connect_finalize(struct audio_device *dev)

g_slist_foreach(p->callbacks, (GFunc) pending_connect_complete, dev);

- g_slist_foreach(p->callbacks, (GFunc) g_free, NULL);
- g_slist_free(p->callbacks);
+ g_slist_free_full(p->callbacks, g_free);

if (p->io) {
g_io_channel_shutdown(p->io, TRUE, NULL);
@@ -2322,8 +2321,7 @@ static void headset_free(struct audio_device *dev)

headset_close_rfcomm(dev);

- g_slist_foreach(hs->nrec_cbs, (GFunc) g_free, NULL);
- g_slist_free(hs->nrec_cbs);
+ g_slist_free_full(hs->nrec_cbs, g_free);

agent_free(hs->agent);
g_free(hs);
diff --git a/audio/media.c b/audio/media.c
index bbb77cf..a58e96f 100644
--- a/audio/media.c
+++ b/audio/media.c
@@ -408,9 +408,8 @@ static void path_free(void *data)
{
struct media_adapter *adapter = data;

- g_slist_foreach(adapter->endpoints, (GFunc) media_endpoint_release,
- NULL);
- g_slist_free(adapter->endpoints);
+ g_slist_free_full(adapter->endpoints,
+ (GDestroyNotify) media_endpoint_release);

dbus_connection_unref(adapter->conn);

diff --git a/audio/telephony-maemo6.c b/audio/telephony-maemo6.c
index cad4475..14d1ead 100644
--- a/audio/telephony-maemo6.c
+++ b/audio/telephony-maemo6.c
@@ -448,8 +448,10 @@ void telephony_device_connected(void *telephony_device)
}
}

-static void pending_req_finalize(struct pending_req *req)
+static void pending_req_finalize(void *data)
{
+ struct pending_req *req = data;
+
if (!dbus_pending_call_get_completed(req->call))
dbus_pending_call_cancel(req->call);

@@ -1486,8 +1488,10 @@ static void handle_hal_property_modified(DBusMessage *msg)
}
}

-static void csd_call_free(struct csd_call *call)
+static void csd_call_free(void *data)
{
+ struct csd_call *call = data;
+
if (!call)
return;

@@ -1975,16 +1979,13 @@ void telephony_exit(void)
g_slist_free(active_calls);
active_calls = NULL;

- g_slist_foreach(calls, (GFunc) csd_call_free, NULL);
- g_slist_free(calls);
+ g_slist_free_full(calls, csd_call_free);
calls = NULL;

- g_slist_foreach(pending, (GFunc) pending_req_finalize, NULL);
- g_slist_free(pending);
+ g_slist_free_full(pending, pending_req_finalize);
pending = NULL;

- g_slist_foreach(watches, (GFunc) remove_watch, NULL);
- g_slist_free(watches);
+ g_slist_free_full(pending, remove_watch);
watches = NULL;

dbus_connection_unref(connection);
diff --git a/audio/telephony-ofono.c b/audio/telephony-ofono.c
index 20dc907..e72734c 100644
--- a/audio/telephony-ofono.c
+++ b/audio/telephony-ofono.c
@@ -638,8 +638,10 @@ static gboolean iter_get_basic_args(DBusMessageIter *iter,
return type == DBUS_TYPE_INVALID ? TRUE : FALSE;
}

-static void call_free(struct voice_call *vc)
+static void call_free(void *data)
{
+ struct voice_call *vc = data;
+
DBG("%s", vc->obj_path);

if (vc->status == CALL_STATUS_ACTIVE)
@@ -1024,8 +1026,7 @@ static void modem_removed(const char *path)

DBG("%s", path);

- g_slist_foreach(calls, (GFunc) call_free, NULL);
- g_slist_free(calls);
+ g_slist_free_full(calls, call_free);
calls = NULL;

g_free(net.operator_name);
@@ -1601,6 +1602,16 @@ static void remove_watch(gpointer data)
g_dbus_remove_watch(connection, GPOINTER_TO_UINT(data));
}

+static void pending_free(void *data)
+{
+ DBusPendingCall *call = data;
+
+ if (!dbus_pending_call_get_completed(call))
+ dbus_pending_call_cancel(call);
+
+ dbus_pending_call_unref(call);
+}
+
void telephony_exit(void)
{
DBG("");
@@ -1611,13 +1622,10 @@ void telephony_exit(void)
if (modem_obj_path)
modem_removed(modem_obj_path);

- g_slist_foreach(watches, (GFunc) remove_watch, NULL);
- g_slist_free(watches);
+ g_slist_free_full(watches, remove_watch);
watches = NULL;

- g_slist_foreach(pending, (GFunc) dbus_pending_call_cancel, NULL);
- g_slist_foreach(pending, (GFunc) dbus_pending_call_unref, NULL);
- g_slist_free(pending);
+ g_slist_free_full(pending, pending_free);
pending = NULL;

dbus_connection_unref(connection);
diff --git a/audio/unix.c b/audio/unix.c
index b29f5f5..8ce50b0 100644
--- a/audio/unix.c
+++ b/audio/unix.c
@@ -97,8 +97,10 @@ static GSList *clients = NULL;

static int unix_sock = -1;

-static void client_free(struct unix_client *client)
+static void client_free(void *data)
{
+ struct unix_client *client = data;
+
DBG("client_free(%p)", client);

if (client->cancel && client->dev && client->req_id > 0)
@@ -107,10 +109,7 @@ static void client_free(struct unix_client *client)
if (client->sock >= 0)
close(client->sock);

- if (client->caps) {
- g_slist_foreach(client->caps, (GFunc) g_free, NULL);
- g_slist_free(client->caps);
- }
+ g_slist_free_full(client->caps, g_free);

g_free(client->interface);
g_free(client);
@@ -1493,11 +1492,8 @@ static int handle_a2dp_transport(struct unix_client *client,
!g_str_equal(client->interface, AUDIO_SOURCE_INTERFACE))
return -EIO;

- if (client->caps) {
- g_slist_foreach(client->caps, (GFunc) g_free, NULL);
- g_slist_free(client->caps);
- client->caps = NULL;
- }
+ g_slist_free_full(client->caps, g_free);
+ client->caps = NULL;

media_transport = avdtp_service_cap_new(AVDTP_MEDIA_TRANSPORT,
NULL, 0);
@@ -1907,8 +1903,7 @@ int unix_init(void)

void unix_exit(void)
{
- g_slist_foreach(clients, (GFunc) client_free, NULL);
- g_slist_free(clients);
+ g_slist_free_full(clients, client_free);
if (unix_sock >= 0) {
close(unix_sock);
unix_sock = -1;
--
1.7.5.4


2011-06-29 08:52:49

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ 2/6] Make use of g_slist_free_full on attrib

From: Luiz Augusto von Dentz <[email protected]>

---
attrib/client.c | 6 ++----
attrib/gatt.c | 3 +--
2 files changed, 3 insertions(+), 6 deletions(-)

diff --git a/attrib/client.c b/attrib/client.c
index 71df532..c9629c4 100644
--- a/attrib/client.c
+++ b/attrib/client.c
@@ -140,8 +140,7 @@ static void primary_free(void *user_data)
g_dbus_remove_watch(prim->gatt->conn, watcher->id);
}

- g_slist_foreach(prim->chars, (GFunc) characteristic_free, NULL);
- g_slist_free(prim->chars);
+ g_slist_free_full(prim->chars, characteristic_free);
g_free(prim->path);
g_free(prim);
}
@@ -150,8 +149,7 @@ static void gatt_service_free(void *user_data)
{
struct gatt_service *gatt = user_data;

- g_slist_foreach(gatt->primary, (GFunc) primary_free, NULL);
- g_slist_free(gatt->primary);
+ g_slist_free_full(gatt->primary, primary_free);
g_attrib_unref(gatt->attrib);
g_free(gatt->path);
btd_device_unref(gatt->dev);
diff --git a/attrib/gatt.c b/attrib/gatt.c
index 1ed78ec..a27d1af 100644
--- a/attrib/gatt.c
+++ b/attrib/gatt.c
@@ -59,8 +59,7 @@ static void discover_primary_free(struct discover_primary *dp)

static void discover_char_free(struct discover_char *dc)
{
- g_slist_foreach(dc->characteristics, (GFunc) g_free, NULL);
- g_slist_free(dc->characteristics);
+ g_slist_free_full(dc->characteristics, g_free);
g_attrib_unref(dc->attrib);
g_free(dc->uuid);
g_free(dc);
--
1.7.5.4