From f989bfd87514d7ff577ccd32dfcbcb72719048ea Mon Sep 17 00:00:00 2001 From: Johan Hedberg Date: Mon, 17 Dec 2012 10:10:54 +0200 Subject: [PATCH] core: Add reference counting for agent objects --- src/adapter.c | 4 ++-- src/agent.c | 22 ++++++++++++++++++---- src/agent.h | 3 ++- src/device.c | 6 +++--- 4 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/adapter.c b/src/adapter.c index 3dc788bb9..1d0c0ecd0 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -1435,7 +1435,7 @@ static DBusMessage *unregister_agent(DBusConnection *conn, DBusMessage *msg, if (!adapter->agent || !agent_matches(adapter->agent, name, path)) return btd_error_does_not_exist(msg); - agent_free(adapter->agent); + agent_unref(adapter->agent); adapter->agent = NULL; return dbus_message_new_method_return(msg); @@ -2067,7 +2067,7 @@ static void adapter_free(gpointer user_data) { struct btd_adapter *adapter = user_data; - agent_free(adapter->agent); + agent_unref(adapter->agent); adapter->agent = NULL; DBG("%p", adapter); diff --git a/src/agent.c b/src/agent.c index c2033a6a7..5085cea1c 100644 --- a/src/agent.c +++ b/src/agent.c @@ -69,6 +69,7 @@ typedef enum { } agent_request_type_t; struct agent { + int ref; struct btd_adapter *adapter; char *name; char *path; @@ -149,12 +150,25 @@ static void agent_exited(DBusConnection *conn, void *user_data) agent->exited = TRUE; - agent_free(agent); + agent_unref(agent); } -void agent_free(struct agent *agent) +struct agent *agent_ref(struct agent *agent) { - if (!agent) + agent->ref++; + + DBG("%p: ref=%d", agent, agent->ref); + + return agent; +} + +void agent_unref(struct agent *agent) +{ + agent->ref--; + + DBG("%p: ref=%d", agent, agent->ref); + + if (agent->ref > 0) return; if (agent->remove_cb) @@ -219,7 +233,7 @@ struct agent *agent_create(struct btd_adapter *adapter, const char *name, g_dbus_add_disconnect_watch(btd_get_dbus_connection(), name, agent_exited, agent, NULL); - return agent; + return agent_ref(agent); } static struct agent_request *agent_request_new(struct agent *agent, diff --git a/src/agent.h b/src/agent.h index f27459f5a..769cd8234 100644 --- a/src/agent.h +++ b/src/agent.h @@ -39,7 +39,8 @@ struct agent *agent_create(struct btd_adapter *adapter, const char *name, const char *path, uint8_t capability, agent_remove_cb cb, void *remove_cb_data); -void agent_free(struct agent *agent); +struct agent *agent_ref(struct agent *agent); +void agent_unref(struct agent *agent); int agent_authorize_service(struct agent *agent, const char *path, const char *uuid, agent_cb cb, diff --git a/src/device.c b/src/device.c index c49cb81d0..f65580077 100644 --- a/src/device.c +++ b/src/device.c @@ -388,7 +388,7 @@ static void device_free(gpointer user_data) struct agent *agent = adapter_get_agent(adapter); if (device->agent) - agent_free(device->agent); + agent_unref(device->agent); if (agent && (agent_is_busy(agent, device) || agent_is_busy(agent, device->authr))) @@ -2204,7 +2204,7 @@ void device_remove(struct btd_device *device, gboolean remove_stored) DBG("Removing device %s", device->path); if (device->agent) - agent_free(device->agent); + agent_unref(device->agent); if (device->bonding) { uint8_t status; @@ -3126,7 +3126,7 @@ static void bonding_request_free(struct bonding_req *bonding) return; agent_cancel(device->agent); - agent_free(device->agent); + agent_unref(device->agent); device->agent = NULL; } -- 2.47.3