[pulseaudio-discuss] [PATCH 1/5] sink, source: Always create a hashmap for ports.

Tanu Kaskinen tanuk at iki.fi
Fri Jun 8 11:49:09 PDT 2012


Having the hashmap sometimes NULL requires a lot of checking
here and there, so ensuring that the hashmap is always
non-NULL simplifies the code.
---
 src/modules/alsa/alsa-mixer.c                   |   16 +++++---
 src/modules/alsa/alsa-mixer.h                   |    2 +-
 src/modules/alsa/alsa-sink.c                    |    2 +-
 src/modules/alsa/alsa-source.c                  |    2 +-
 src/modules/bluetooth/module-bluetooth-device.c |    9 ++---
 src/modules/dbus/iface-device.c                 |   32 ++++++----------
 src/modules/module-switch-on-port-available.c   |    4 +-
 src/pulsecore/protocol-native.c                 |   45 ++++++++++-------------
 src/pulsecore/sink.c                            |    7 ++--
 src/pulsecore/source.c                          |    7 ++--
 10 files changed, 58 insertions(+), 68 deletions(-)

diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index 260573c..9a5a60d 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -4576,17 +4576,21 @@ void pa_alsa_path_set_add_ports(
     }
 }
 
-void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps, pa_card *card) {
+void pa_alsa_add_ports(void *sink_or_source_new_data, pa_alsa_path_set *ps, pa_card *card) {
+    pa_hashmap *ports;
 
-    pa_assert(p);
-    pa_assert(!*p);
+    pa_assert(sink_or_source_new_data);
     pa_assert(ps);
 
+    if (ps->direction == PA_ALSA_DIRECTION_OUTPUT)
+        ports = ((pa_sink_new_data *) sink_or_source_new_data)->ports;
+    else
+        ports = ((pa_source_new_data *) sink_or_source_new_data)->ports;
+
     if (ps->paths && pa_hashmap_size(ps->paths) > 0) {
         pa_assert(card);
-        *p = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-        pa_alsa_path_set_add_ports(ps, NULL, card->ports, *p, card->core);
+        pa_alsa_path_set_add_ports(ps, NULL, card->ports, ports, card->core);
     }
 
-    pa_log_debug("Added %u ports", *p ? pa_hashmap_size(*p) : 0);
+    pa_log_debug("Added %u ports", pa_hashmap_size(ports));
 }
diff --git a/src/modules/alsa/alsa-mixer.h b/src/modules/alsa/alsa-mixer.h
index fdcff76..db0d25e 100644
--- a/src/modules/alsa/alsa-mixer.h
+++ b/src/modules/alsa/alsa-mixer.h
@@ -339,7 +339,7 @@ struct pa_alsa_port_data {
     pa_alsa_setting *setting;
 };
 
-void pa_alsa_add_ports(pa_hashmap **p, pa_alsa_path_set *ps, pa_card *card);
+void pa_alsa_add_ports(void *sink_or_source_new_data, pa_alsa_path_set *ps, pa_card *card);
 void pa_alsa_path_set_add_ports(pa_alsa_path_set *ps, pa_card_profile *cp, pa_hashmap *ports, pa_hashmap *extra, pa_core *core);
 
 #endif
diff --git a/src/modules/alsa/alsa-sink.c b/src/modules/alsa/alsa-sink.c
index 1bee8f3..472d66a 100644
--- a/src/modules/alsa/alsa-sink.c
+++ b/src/modules/alsa/alsa-sink.c
@@ -2226,7 +2226,7 @@ pa_sink *pa_alsa_sink_new(pa_module *m, pa_modargs *ma, const char*driver, pa_ca
     }
 
     if (u->mixer_path_set)
-        pa_alsa_add_ports(&data.ports, u->mixer_path_set, card);
+        pa_alsa_add_ports(&data, u->mixer_path_set, card);
 
     u->sink = pa_sink_new(m->core, &data, PA_SINK_HARDWARE | PA_SINK_LATENCY | (u->use_tsched ? PA_SINK_DYNAMIC_LATENCY : 0) |
                           (set_formats ? PA_SINK_SET_FORMATS : 0));
diff --git a/src/modules/alsa/alsa-source.c b/src/modules/alsa/alsa-source.c
index dfad817..c27bbc2 100644
--- a/src/modules/alsa/alsa-source.c
+++ b/src/modules/alsa/alsa-source.c
@@ -1952,7 +1952,7 @@ pa_source *pa_alsa_source_new(pa_module *m, pa_modargs *ma, const char*driver, p
     }
 
     if (u->mixer_path_set)
-        pa_alsa_add_ports(&data.ports, u->mixer_path_set, card);
+        pa_alsa_add_ports(&data, u->mixer_path_set, card);
 
     u->source = pa_source_new(m->core, &data, PA_SOURCE_HARDWARE|PA_SOURCE_LATENCY|(u->use_tsched ? PA_SOURCE_DYNAMIC_LATENCY : 0));
     pa_source_new_data_done(&data);
diff --git a/src/modules/bluetooth/module-bluetooth-device.c b/src/modules/bluetooth/module-bluetooth-device.c
index 4901ef9..f687edd 100644
--- a/src/modules/bluetooth/module-bluetooth-device.c
+++ b/src/modules/bluetooth/module-bluetooth-device.c
@@ -2080,13 +2080,10 @@ static void connect_ports(struct userdata *u, void *sink_or_source_new_data, pa_
     } data;
     pa_device_port *port;
 
-    if (direction == PA_DIRECTION_OUTPUT) {
+    if (direction == PA_DIRECTION_OUTPUT)
         data.sink_new_data = sink_or_source_new_data;
-        data.sink_new_data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-    } else {
+    else
         data.source_new_data = sink_or_source_new_data;
-        data.source_new_data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
-    }
 
     switch (u->profile) {
         case PROFILE_A2DP:
@@ -2125,7 +2122,7 @@ static void connect_ports(struct userdata *u, void *sink_or_source_new_data, pa_
 
         default:
             pa_assert_not_reached();
-        }
+    }
 }
 
 /* Run from main thread */
diff --git a/src/modules/dbus/iface-device.c b/src/modules/dbus/iface-device.c
index a5af730..316aba5 100644
--- a/src/modules/dbus/iface-device.c
+++ b/src/modules/dbus/iface-device.c
@@ -1189,6 +1189,8 @@ static void subscription_cb(pa_core *c, pa_subscription_event_type_t t, uint32_t
 
 pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_sink *sink) {
     pa_dbusiface_device *d = NULL;
+    pa_device_port *port;
+    void *state;
 
     pa_assert(core);
     pa_assert(sink);
@@ -1203,20 +1205,14 @@ pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_si
     d->sink_state = pa_sink_get_state(sink);
     d->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     d->next_port_index = 0;
-    d->active_port = NULL;
+    d->active_port = sink->active_port;
     d->proplist = pa_proplist_copy(sink->proplist);
     d->dbus_protocol = pa_dbus_protocol_get(sink->core);
     d->subscription = pa_subscription_new(sink->core, PA_SUBSCRIPTION_MASK_SINK, subscription_cb, d);
 
-    if (sink->ports) {
-        pa_device_port *port;
-        void *state = NULL;
-
-        PA_HASHMAP_FOREACH(port, sink->ports, state) {
-            pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, sink->core, port, d->next_port_index++);
-            pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
-        }
-        pa_assert_se(d->active_port = sink->active_port);
+    PA_HASHMAP_FOREACH(port, sink->ports, state) {
+        pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, sink->core, port, d->next_port_index++);
+        pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
     }
 
     pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &device_interface_info, d) >= 0);
@@ -1227,6 +1223,8 @@ pa_dbusiface_device *pa_dbusiface_device_new_sink(pa_dbusiface_core *core, pa_si
 
 pa_dbusiface_device *pa_dbusiface_device_new_source(pa_dbusiface_core *core, pa_source *source) {
     pa_dbusiface_device *d = NULL;
+    pa_device_port *port;
+    void *state;
 
     pa_assert(core);
     pa_assert(source);
@@ -1241,20 +1239,14 @@ pa_dbusiface_device *pa_dbusiface_device_new_source(pa_dbusiface_core *core, pa_
     d->source_state = pa_source_get_state(source);
     d->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
     d->next_port_index = 0;
-    d->active_port = NULL;
+    d->active_port = source->active_port;
     d->proplist = pa_proplist_copy(source->proplist);
     d->dbus_protocol = pa_dbus_protocol_get(source->core);
     d->subscription = pa_subscription_new(source->core, PA_SUBSCRIPTION_MASK_SOURCE, subscription_cb, d);
 
-    if (source->ports) {
-        pa_device_port *port;
-        void *state = NULL;
-
-        PA_HASHMAP_FOREACH(port, source->ports, state) {
-            pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, source->core, port, d->next_port_index++);
-            pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
-        }
-        pa_assert_se(d->active_port = source->active_port);
+    PA_HASHMAP_FOREACH(port, source->ports, state) {
+        pa_dbusiface_device_port *p = pa_dbusiface_device_port_new(d, source->core, port, d->next_port_index++);
+        pa_hashmap_put(d->ports, pa_dbusiface_device_port_get_name(p), p);
     }
 
     pa_assert_se(pa_dbus_protocol_add_interface(d->dbus_protocol, d->path, &device_interface_info, d) >= 0);
diff --git a/src/modules/module-switch-on-port-available.c b/src/modules/module-switch-on-port-available.c
index f198e44..f60d11e 100644
--- a/src/modules/module-switch-on-port-available.c
+++ b/src/modules/module-switch-on-port-available.c
@@ -121,12 +121,12 @@ static void find_sink_and_source(pa_card *card, pa_device_port *port, pa_sink **
 
     if (port->is_output)
         PA_IDXSET_FOREACH(sink, card->sinks, state)
-            if (sink->ports && port == pa_hashmap_get(sink->ports, port->name))
+            if (port == pa_hashmap_get(sink->ports, port->name))
                 break;
 
     if (port->is_input)
         PA_IDXSET_FOREACH(source, card->sources, state)
-            if (source->ports && port == pa_hashmap_get(source->ports, port->name))
+            if (port == pa_hashmap_get(source->ports, port->name))
                 break;
 
     *si = sink;
diff --git a/src/pulsecore/protocol-native.c b/src/pulsecore/protocol-native.c
index a698f74..e81effd 100644
--- a/src/pulsecore/protocol-native.c
+++ b/src/pulsecore/protocol-native.c
@@ -3114,19 +3114,17 @@ static void sink_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_sin
     }
 
     if (c->version >= 16) {
-        pa_tagstruct_putu32(t, sink->ports ? pa_hashmap_size(sink->ports) : 0);
-
-        if (sink->ports) {
-            void *state;
-            pa_device_port *p;
-
-            PA_HASHMAP_FOREACH(p, sink->ports, state) {
-                pa_tagstruct_puts(t, p->name);
-                pa_tagstruct_puts(t, p->description);
-                pa_tagstruct_putu32(t, p->priority);
-                if (c->version >= 24)
-                    pa_tagstruct_putu32(t, p->available);
-            }
+        void *state;
+        pa_device_port *p;
+
+        pa_tagstruct_putu32(t, pa_hashmap_size(sink->ports));
+
+        PA_HASHMAP_FOREACH(p, sink->ports, state) {
+            pa_tagstruct_puts(t, p->name);
+            pa_tagstruct_puts(t, p->description);
+            pa_tagstruct_putu32(t, p->priority);
+            if (c->version >= 24)
+                pa_tagstruct_putu32(t, p->available);
         }
 
         pa_tagstruct_puts(t, sink->active_port ? sink->active_port->name : NULL);
@@ -3186,20 +3184,17 @@ static void source_fill_tagstruct(pa_native_connection *c, pa_tagstruct *t, pa_s
     }
 
     if (c->version >= 16) {
+        void *state;
+        pa_device_port *p;
 
-        pa_tagstruct_putu32(t, source->ports ? pa_hashmap_size(source->ports) : 0);
+        pa_tagstruct_putu32(t, pa_hashmap_size(source->ports));
 
-        if (source->ports) {
-            void *state;
-            pa_device_port *p;
-
-            PA_HASHMAP_FOREACH(p, source->ports, state) {
-                pa_tagstruct_puts(t, p->name);
-                pa_tagstruct_puts(t, p->description);
-                pa_tagstruct_putu32(t, p->priority);
-                if (c->version >= 24)
-                    pa_tagstruct_putu32(t, p->available);
-            }
+        PA_HASHMAP_FOREACH(p, source->ports, state) {
+            pa_tagstruct_puts(t, p->name);
+            pa_tagstruct_puts(t, p->description);
+            pa_tagstruct_putu32(t, p->priority);
+            if (c->version >= 24)
+                pa_tagstruct_putu32(t, p->available);
         }
 
         pa_tagstruct_puts(t, source->active_port ? source->active_port->name : NULL);
diff --git a/src/pulsecore/sink.c b/src/pulsecore/sink.c
index e4c343d..50d4013 100644
--- a/src/pulsecore/sink.c
+++ b/src/pulsecore/sink.c
@@ -81,6 +81,7 @@ pa_sink_new_data* pa_sink_new_data_init(pa_sink_new_data *data) {
 
     pa_zero(*data);
     data->proplist = pa_proplist_new();
+    data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     return data;
 }
@@ -295,11 +296,11 @@ pa_sink* pa_sink_new(
     s->active_port = NULL;
     s->save_port = FALSE;
 
-    if (data->active_port && s->ports)
+    if (data->active_port)
         if ((s->active_port = pa_hashmap_get(s->ports, data->active_port)))
             s->save_port = data->save_port;
 
-    if (!s->active_port && s->ports) {
+    if (!s->active_port) {
         void *state;
         pa_device_port *p;
 
@@ -3264,7 +3265,7 @@ int pa_sink_set_port(pa_sink *s, const char *name, pa_bool_t save) {
         return -PA_ERR_NOTIMPLEMENTED;
     }
 
-    if (!s->ports || !name)
+    if (!name)
         return -PA_ERR_NOENTITY;
 
     if (!(port = pa_hashmap_get(s->ports, name)))
diff --git a/src/pulsecore/source.c b/src/pulsecore/source.c
index 6308f54..63a0b89 100644
--- a/src/pulsecore/source.c
+++ b/src/pulsecore/source.c
@@ -73,6 +73,7 @@ pa_source_new_data* pa_source_new_data_init(pa_source_new_data *data) {
 
     pa_zero(*data);
     data->proplist = pa_proplist_new();
+    data->ports = pa_hashmap_new(pa_idxset_string_hash_func, pa_idxset_string_compare_func);
 
     return data;
 }
@@ -283,11 +284,11 @@ pa_source* pa_source_new(
     s->active_port = NULL;
     s->save_port = FALSE;
 
-    if (data->active_port && s->ports)
+    if (data->active_port)
         if ((s->active_port = pa_hashmap_get(s->ports, data->active_port)))
             s->save_port = data->save_port;
 
-    if (!s->active_port && s->ports) {
+    if (!s->active_port) {
         void *state;
         pa_device_port *p;
 
@@ -2534,7 +2535,7 @@ int pa_source_set_port(pa_source *s, const char *name, pa_bool_t save) {
         return -PA_ERR_NOTIMPLEMENTED;
     }
 
-    if (!s->ports || !name)
+    if (!name)
         return -PA_ERR_NOENTITY;
 
     if (!(port = pa_hashmap_get(s->ports, name)))
-- 
1.7.10



More information about the pulseaudio-discuss mailing list