aboutsummaryrefslogtreecommitdiff
path: root/health
diff options
context:
space:
mode:
authorJose Antonio Santos Cadenas <santoscadenas@gmail.com>2010-11-11 14:50:12 +0100
committerJohan Hedberg <johan.hedberg@nokia.com>2010-11-18 16:56:26 +0200
commitfda9a5a1bc6e81e70a6b139f94abbcf24d7e4e9c (patch)
tree3f5d3e08919448207e61b947d17f02b3c096f3ca /health
parentb0d886549332013daec34aa1fa6eeec2eb031aaf (diff)
downloadbluez-fda9a5a1bc6e81e70a6b139f94abbcf24d7e4e9c.tar.gz
Add reference counter to hdp_application
Diffstat (limited to 'health')
-rw-r--r--health/hdp.c26
-rw-r--r--health/hdp_types.h2
-rw-r--r--health/hdp_util.c46
-rw-r--r--health/hdp_util.h3
4 files changed, 58 insertions, 19 deletions
diff --git a/health/hdp.c b/health/hdp.c
index e4e3cdef..69ac5b55 100644
--- a/health/hdp.c
+++ b/health/hdp.c
@@ -92,6 +92,7 @@ static void free_hdp_create_dc(struct hdp_create_dc *dc_data)
{
dbus_message_unref(dc_data->msg);
dbus_connection_unref(dc_data->conn);
+ hdp_application_unref(dc_data->app);
g_free(dc_data);
}
@@ -282,21 +283,10 @@ static void free_health_device(struct hdp_device *device)
g_free(device);
}
-static void free_application(struct hdp_application *app)
-{
- if (app->dbus_watcher)
- g_dbus_remove_watch(connection, app->dbus_watcher);
-
- g_free(app->oname);
- g_free(app->description);
- g_free(app->path);
- g_free(app);
-}
-
static void remove_application(struct hdp_application *app)
{
DBG("Application %s deleted", app->path);
- free_application(app);
+ hdp_application_unref(app);
g_slist_foreach(adapters, (GFunc) update_adapter, NULL);
}
@@ -334,20 +324,21 @@ static DBusMessage *manager_create_application(DBusConnection *conn,
name = dbus_message_get_sender(msg);
if (!name) {
- free_application(app);
+ hdp_application_unref(app);
return g_dbus_create_error(msg,
ERROR_INTERFACE ".HealthError",
"Can't get sender name");
}
if (!set_app_path(app)) {
- free_application(app);
+ hdp_application_unref(app);
return g_dbus_create_error(msg,
ERROR_INTERFACE ".HealthError",
"Can't get a valid id for the application");
}
app->oname = g_strdup(name);
+ app->conn = dbus_connection_ref(conn);
applications = g_slist_prepend(applications, app);
@@ -393,7 +384,7 @@ static DBusMessage *manager_destroy_application(DBusConnection *conn,
static void manager_path_unregister(gpointer data)
{
- g_slist_foreach(applications, (GFunc) free_application, NULL);
+ g_slist_foreach(applications, (GFunc) hdp_application_unref, NULL);
g_slist_free(applications);
applications = NULL;
@@ -708,6 +699,7 @@ static void health_channel_destroy(void *data)
if (hdp_chan->mdep == HDP_MDEP_ECHO)
free_echo_data(hdp_chan->edata);
+ hdp_application_unref(hdp_chan->app);
g_free(hdp_chan->path);
g_free(hdp_chan);
}
@@ -734,7 +726,7 @@ static struct hdp_channel *create_channel(struct hdp_device *dev,
hdp_chann->dev = dev;
hdp_chann->mdl = mdl;
hdp_chann->mdlid = mdlid;
- hdp_chann->app = app;
+ hdp_chann->app = hdp_application_ref(app);
if (app)
hdp_chann->mdep = app->id;
@@ -1852,7 +1844,7 @@ static DBusMessage *device_create_channel(DBusConnection *conn,
data = g_new0(struct hdp_create_dc, 1);
data->dev = device;
data->config = config;
- data->app = app;
+ data->app = hdp_application_ref(app);
data->msg = dbus_message_ref(msg);
data->conn = dbus_connection_ref(conn);
data->cb = hdp_mdl_conn_cb;
diff --git a/health/hdp_types.h b/health/hdp_types.h
index edfeeac5..a9937235 100644
--- a/health/hdp_types.h
+++ b/health/hdp_types.h
@@ -73,6 +73,7 @@ enum data_specs {
};
struct hdp_application {
+ DBusConnection *conn; /* For dbus watcher */
char *path; /* The path of the application */
uint16_t data_type; /* Data type handled for this application */
gboolean data_type_set; /* Flag for dictionary parsing */
@@ -84,6 +85,7 @@ struct hdp_application {
uint8_t id; /* The identification is also the mdepid */
char *oname; /* Name of the owner application */
int dbus_watcher; /* Watch for clients disconnection */
+ gint ref; /* Reference counter */
};
struct hdp_adapter {
diff --git a/health/hdp_util.c b/health/hdp_util.c
index fefb6d33..a766be42 100644
--- a/health/hdp_util.c
+++ b/health/hdp_util.c
@@ -40,6 +40,8 @@
#include <btio.h>
#include <mcap_lib.h>
+#include <log.h>
+
typedef gboolean (*parse_item_f)(DBusMessageIter *iter, gpointer user_data,
GError **err);
@@ -292,6 +294,7 @@ struct hdp_application *hdp_get_app_config(DBusMessageIter *iter, GError **err)
struct hdp_application *app;
app = g_new0(struct hdp_application, 1);
+ app->ref = 1;
if (!parse_dict(dict_parser, iter, err, app))
goto fail;
if (!app->data_type_set || !app->role_set) {
@@ -302,7 +305,7 @@ struct hdp_application *hdp_get_app_config(DBusMessageIter *iter, GError **err)
return app;
fail:
- g_free(app);
+ hdp_application_unref(app);
return NULL;
}
@@ -837,6 +840,7 @@ static void free_mdep_data(gpointer data)
if (mdep_data->destroy)
mdep_data->destroy(mdep_data->data);
+ hdp_application_unref(mdep_data->app);
g_free(mdep_data);
}
@@ -853,7 +857,7 @@ gboolean hdp_get_mdep(struct hdp_device *device, struct hdp_application *app,
adapter_get_address(device_get_adapter(device->dev), &src);
mdep_data = g_new0(struct get_mdep_data, 1);
- mdep_data->app = app;
+ mdep_data->app = hdp_application_ref(app);
mdep_data->func = func;
mdep_data->data = data;
mdep_data->destroy = destroy;
@@ -1160,3 +1164,41 @@ gboolean hdp_get_dcpsm(struct hdp_device *device, hdp_continue_dcpsm_f func,
return TRUE;
}
+
+static void hdp_free_application(struct hdp_application *app)
+{
+ if (app->dbus_watcher)
+ g_dbus_remove_watch(app->conn, app->dbus_watcher);
+
+ if (app->conn)
+ dbus_connection_unref(app->conn);
+ g_free(app->oname);
+ g_free(app->description);
+ g_free(app->path);
+ g_free(app);
+}
+
+struct hdp_application *hdp_application_ref(struct hdp_application *app)
+{
+ if (!app)
+ return NULL;
+
+ app->ref++;
+
+ DBG("health_application_ref(%p): ref=%d", app, app->ref);
+ return app;
+}
+
+void hdp_application_unref(struct hdp_application *app)
+{
+ if (!app)
+ return;
+
+ app->ref --;
+
+ DBG("health_application_unref(%p): ref=%d", app, app->ref);
+ if (app->ref > 0)
+ return;
+
+ hdp_free_application(app);
+}
diff --git a/health/hdp_util.h b/health/hdp_util.h
index f16ef324..c943ce2f 100644
--- a/health/hdp_util.h
+++ b/health/hdp_util.h
@@ -50,4 +50,7 @@ gboolean hdp_get_dcpsm(struct hdp_device *device, hdp_continue_dcpsm_f func,
GDestroyNotify destroy,
GError **err);
+struct hdp_application *hdp_application_ref(struct hdp_application *app);
+void hdp_application_unref(struct hdp_application *app);
+
#endif /* __HDP_UTIL_H__ */