[pulseaudio-discuss] [PATCH v0 12/16] bluetooth: Add reference counting to transports

Mikel Astiz mikel.astiz.oss at gmail.com
Fri Sep 28 08:45:36 PDT 2012


From: Mikel Astiz <mikel.astiz at bmw-carit.de>

module-bluetooth-device can benefit from having a reference counted
transports objects, so it doesn't need to use functions like
pa_bluetooth_discovery_get_transport() every time it needs to access
this object.
---
 src/modules/bluetooth/bluetooth-util.c | 22 ++++++++++++++++++----
 src/modules/bluetooth/bluetooth-util.h |  6 +++++-
 2 files changed, 23 insertions(+), 5 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 245923d..803a24d 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -139,10 +139,23 @@ static pa_bluetooth_device* device_new(const char *path) {
     return d;
 }
 
-static void transport_free(pa_bluetooth_transport *t) {
+pa_bluetooth_transport* pa_bluetooth_transport_ref(pa_bluetooth_transport *t) {
+    pa_assert(t);
+    pa_assert(PA_REFCNT_VALUE(t) > 0);
+
+    PA_REFCNT_INC(t);
+
+    return t;
+}
+
+void pa_bluetooth_transport_unref(pa_bluetooth_transport *t) {
     unsigned i;
 
     pa_assert(t);
+    pa_assert(PA_REFCNT_VALUE(t) > 0);
+
+    if (PA_REFCNT_DEC(t) > 0)
+        return;
 
     for (i = 0; i < PA_BLUETOOTH_TRANSPORT_HOOK_MAX; i++)
         pa_hook_done(&t->hooks[i]);
@@ -159,7 +172,7 @@ static void device_free(pa_bluetooth_device *d) {
     pa_assert(d);
 
     while ((t = pa_hashmap_steal_first(d->transports)))
-        transport_free(t);
+        pa_bluetooth_transport_unref(t);
 
     pa_hashmap_free(d->transports, NULL, NULL);
 
@@ -969,7 +982,7 @@ pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discov
     return NULL;
 }
 
-const pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile) {
+pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile) {
     pa_bluetooth_transport *t;
     void *state = NULL;
 
@@ -1061,6 +1074,7 @@ static pa_bluetooth_transport *transport_new(pa_bluetooth_discovery *y, const ch
     unsigned i;
 
     t = pa_xnew0(pa_bluetooth_transport, 1);
+    PA_REFCNT_INIT(t);
     t->y = y;
     t->path = pa_xstrdup(path);
     t->profile = p;
@@ -1189,7 +1203,7 @@ static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage
         if ((t = pa_hashmap_get(d->transports, path))) {
             pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
             pa_hashmap_remove(d->transports, t->path);
-            transport_free(t);
+            pa_bluetooth_transport_unref(t);
             break;
         }
     }
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
index a2ad9aa..e102521 100644
--- a/src/modules/bluetooth/bluetooth-util.h
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -70,6 +70,8 @@ typedef enum pa_bluetooth_transport_hook {
 } pa_bluetooth_transport_hook_t;
 
 struct pa_bluetooth_transport {
+    PA_REFCNT_DECLARE;
+
     pa_bluetooth_discovery *y;
     char *path;
     enum profile profile;
@@ -133,7 +135,9 @@ const pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_disco
 const pa_bluetooth_device* pa_bluetooth_discovery_get_by_address(pa_bluetooth_discovery *d, const char* address);
 
 pa_bluetooth_transport* pa_bluetooth_discovery_get_transport(pa_bluetooth_discovery *y, const char *path);
-const pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile);
+pa_bluetooth_transport* pa_bluetooth_device_get_transport(const pa_bluetooth_device *d, enum profile profile);
+pa_bluetooth_transport* pa_bluetooth_transport_ref(pa_bluetooth_transport *t);
+void pa_bluetooth_transport_unref(pa_bluetooth_transport *t);
 
 int pa_bluetooth_transport_acquire(const pa_bluetooth_transport *t, const char *accesstype, size_t *imtu, size_t *omtu);
 void pa_bluetooth_transport_release(const pa_bluetooth_transport *t, const char *accesstype);
-- 
1.7.11.4



More information about the pulseaudio-discuss mailing list