bluetooth: Use transport array instead of hashmap for devices

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.
This commit is contained in:
Mikel Astiz 2012-12-06 10:35:19 +01:00 committed by Tanu Kaskinen
parent 51d28e6331
commit faaf8cd39e
2 changed files with 20 additions and 15 deletions

View file

@ -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);
@ -167,14 +166,16 @@ 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]))
continue;
d->transports[i] = NULL;
pa_hashmap_remove(d->discovery->transports, t->path);
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]);
@ -974,16 +975,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) {
@ -1183,12 +1180,18 @@ static DBusMessage *endpoint_set_configuration(DBusConnection *conn, DBusMessage
else
p = PROFILE_A2DP_SOURCE;
if (d->transports[p] != NULL) {
pa_log("Cannot configure transport %s because profile %d is already used", path, p);
goto fail;
}
sender = dbus_message_get_sender(m);
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);
@ -1221,7 +1224,7 @@ static DBusMessage *endpoint_clear_configuration(DBusConnection *c, DBusMessage
if ((t = pa_hashmap_get(y->transports, path))) {
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_hashmap_remove(y->transports, t->path);
pa_hook_fire(&t->hooks[PA_BLUETOOTH_TRANSPORT_HOOK_REMOVED], NULL);
transport_free(t);

View file

@ -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);