[pulseaudio-discuss] [PATCH next v0 7/8] bluetooth: Use transport array instead of hashmap for devices

Mikel Astiz mikel.astiz.oss at gmail.com
Wed Dec 5 08:23:17 PST 2012


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

Devices will have zero or one transports per profile, and besides the
typical lookup is also profile-based. Therefore, replace the old hashmap
(which used the transport path as key) with a simple array which holds
a transport pointer per profile.
---
 src/modules/bluetooth/bluetooth-util.c | 31 +++++++++++++++++--------------
 src/modules/bluetooth/bluetooth-util.h |  4 +++-
 2 files changed, 20 insertions(+), 15 deletions(-)

diff --git a/src/modules/bluetooth/bluetooth-util.c b/src/modules/bluetooth/bluetooth-util.c
index 8ca6fae..2f7d753 100644
--- a/src/modules/bluetooth/bluetooth-util.c
+++ b/src/modules/bluetooth/bluetooth-util.c
@@ -117,7 +117,7 @@ static pa_bluetooth_device* device_new(pa_bluetooth_discovery *discovery, const
     pa_assert(discovery);
     pa_assert(path);
 
-    d = pa_xnew(pa_bluetooth_device, 1);
+    d = pa_xnew0(pa_bluetooth_device, 1);
 
     d->discovery = discovery;
     d->dead = FALSE;
@@ -126,7 +126,6 @@ static pa_bluetooth_device* device_new(pa_bluetooth_discovery *discovery, const
 
     d->name = NULL;
     d->path = pa_xstrdup(path);
-    d->transports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     d->paired = -1;
     d->alias = NULL;
     PA_LLIST_HEAD_INIT(pa_bluetooth_uuid, d->uuids);
@@ -169,13 +168,15 @@ static void device_free(pa_bluetooth_device *d) {
 
     pa_assert(d);
 
-    while ((t = pa_hashmap_steal_first(d->transports))) {
+    for (i = 0; i < PA_BLUETOOTH_PROFILE_COUNT; i ++) {
+        if ((t = d->transports[i]) == NULL)
+            continue;
+
+        d->transports[i] = NULL;
         pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
         transport_free(t);
     }
 
-    pa_hashmap_free(d->transports, NULL, NULL);
-
     for (i = 0; i < PA_BLUETOOTH_DEVICE_HOOK_MAX; i++)
         pa_hook_done(&d->hooks[i]);
 
@@ -975,16 +976,12 @@ pa_bluetooth_device* pa_bluetooth_discovery_get_by_path(pa_bluetooth_discovery *
 }
 
 pa_bluetooth_transport* pa_bluetooth_device_get_transport(pa_bluetooth_device *d, enum profile profile) {
-    pa_bluetooth_transport *t;
-    void *state = NULL;
-
     pa_assert(d);
 
-    while ((t = pa_hashmap_iterate(d->transports, &state, NULL)))
-        if (t->profile == profile)
-            return t;
+    if (profile == PROFILE_OFF)
+        return NULL;
 
-    return NULL;
+    return d->transports[profile];
 }
 
 bool pa_bluetooth_device_any_audio_connected(const pa_bluetooth_device *d) {
@@ -1186,10 +1183,16 @@ static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage
 
     sender = dbus_message_get_sender(m);
 
+    if (d->transports[p] != NULL) {
+        pa_log_warn("Cannot configure transport %s because profile %d is already used", path, p);
+        goto fail;
+    }
+
     t = transport_new(d, sender, path, p, config, size);
     if (nrec)
         t->nrec = nrec;
-    pa_hashmap_put(d->transports, t->path, t);
+
+    d->transports[p] = t;
     pa_hashmap_put(y->transports, t->path, t);
 
     pa_log_debug("Transport %s profile %d available", t->path, t->profile);
@@ -1222,7 +1225,7 @@ static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage
 
     if ((t = pa_hashmap_get(y->transports, path)) != NULL) {
         pa_log_debug("Clearing transport %s profile %d", t->path, t->profile);
-        pa_hashmap_remove(t->device->transports, t->path);
+        t->device->transports[t->profile] = NULL;
         pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
         transport_free(t);
     }
diff --git a/src/modules/bluetooth/bluetooth-util.h b/src/modules/bluetooth/bluetooth-util.h
index 89e8390..f6ffece 100644
--- a/src/modules/bluetooth/bluetooth-util.h
+++ b/src/modules/bluetooth/bluetooth-util.h
@@ -63,6 +63,8 @@ enum profile {
     PROFILE_OFF
 };
 
+#define PA_BLUETOOTH_PROFILE_COUNT PROFILE_OFF
+
 /* Hook data: pa_bluetooth_transport pointer. */
 typedef enum pa_bluetooth_transport_hook {
     PA_BLUETOOTH_TRANSPORT_HOOK_NREC_CHANGED, /* Call data: NULL. */
@@ -108,7 +110,7 @@ struct pa_bluetooth_device {
     /* Device information */
     char *name;
     char *path;
-    pa_hashmap *transports;
+    pa_bluetooth_transport *transports[PA_BLUETOOTH_PROFILE_COUNT];
     int paired;
     char *alias;
     PA_LLIST_HEAD(pa_bluetooth_uuid, uuids);
-- 
1.7.11.7



More information about the pulseaudio-discuss mailing list