[pulseaudio-discuss] [PATCH v2 1/6] alsa: Add pa_alsa_jack_new() and pa_alsa_jack_free()

Tanu Kaskinen tanu.kaskinen at linux.intel.com
Mon May 4 11:03:40 PDT 2015


This reduces code duplication in alsa-mixer.c and alsa-ucm.c. No
functional changes.
---
 src/modules/alsa/alsa-mixer.c | 45 ++++++++++++++++++++++++++++---------------
 src/modules/alsa/alsa-mixer.h |  3 +++
 src/modules/alsa/alsa-ucm.c   | 16 ++++++---------
 3 files changed, 39 insertions(+), 25 deletions(-)

diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index 2314612..4771a2d 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -107,6 +107,34 @@ struct description_map {
     const char *description;
 };
 
+pa_alsa_jack *pa_alsa_jack_new(pa_alsa_path *path, const char *name, const char *alsa_name) {
+    pa_alsa_jack *jack;
+
+    pa_assert(name);
+
+    jack = pa_xnew0(pa_alsa_jack, 1);
+    jack->path = path;
+    jack->name = pa_xstrdup(name);
+
+    if (alsa_name)
+        jack->alsa_name = pa_xstrdup(alsa_name);
+    else
+        jack->alsa_name = pa_sprintf_malloc("%s Jack", name);
+
+    jack->state_unplugged = PA_AVAILABLE_NO;
+    jack->state_plugged = PA_AVAILABLE_YES;
+
+    return jack;
+}
+
+void pa_alsa_jack_free(pa_alsa_jack *jack) {
+    pa_assert(jack);
+
+    pa_xfree(jack->alsa_name);
+    pa_xfree(jack->name);
+    pa_xfree(jack);
+}
+
 static const char *lookup_description(const char *key, const struct description_map dm[], unsigned n) {
     unsigned i;
 
@@ -524,14 +552,6 @@ static void decibel_fix_free(pa_alsa_decibel_fix *db_fix) {
     pa_xfree(db_fix);
 }
 
-static void jack_free(pa_alsa_jack *j) {
-    pa_assert(j);
-
-    pa_xfree(j->alsa_name);
-    pa_xfree(j->name);
-    pa_xfree(j);
-}
-
 static void element_free(pa_alsa_element *e) {
     pa_alsa_option *o;
     pa_assert(e);
@@ -557,7 +577,7 @@ void pa_alsa_path_free(pa_alsa_path *p) {
 
     while ((j = p->jacks)) {
         PA_LLIST_REMOVE(pa_alsa_jack, p->jacks, j);
-        jack_free(j);
+        pa_alsa_jack_free(j);
     }
 
     while ((e = p->elements)) {
@@ -1793,12 +1813,7 @@ static pa_alsa_jack* jack_get(pa_alsa_path *p, const char *section) {
         if (pa_streq(j->name, section))
             goto finish;
 
-    j = pa_xnew0(pa_alsa_jack, 1);
-    j->state_unplugged = PA_AVAILABLE_NO;
-    j->state_plugged = PA_AVAILABLE_YES;
-    j->path = p;
-    j->name = pa_xstrdup(section);
-    j->alsa_name = pa_sprintf_malloc("%s Jack", section);
+    j = pa_alsa_jack_new(p, section, NULL);
     PA_LLIST_INSERT_AFTER(pa_alsa_jack, p->jacks, p->last_jack, j);
 
 finish:
diff --git a/src/modules/alsa/alsa-mixer.h b/src/modules/alsa/alsa-mixer.h
index ec39fab..835361b 100644
--- a/src/modules/alsa/alsa-mixer.h
+++ b/src/modules/alsa/alsa-mixer.h
@@ -170,6 +170,9 @@ struct pa_alsa_jack {
     pa_alsa_required_t required_absent;
 };
 
+pa_alsa_jack *pa_alsa_jack_new(pa_alsa_path *path, const char *name, const char *alsa_name);
+void pa_alsa_jack_free(pa_alsa_jack *jack);
+
 /* A path wraps a series of elements into a single entity which can be
  * used to control it as if it had a single volume slider, a single
  * mute switch and a single list of selectable options. */
diff --git a/src/modules/alsa/alsa-ucm.c b/src/modules/alsa/alsa-ucm.c
index ef6adcd..98d9a5d 100644
--- a/src/modules/alsa/alsa-ucm.c
+++ b/src/modules/alsa/alsa-ucm.c
@@ -1268,6 +1268,7 @@ static pa_alsa_jack* ucm_get_jack(pa_alsa_ucm_config *ucm, pa_alsa_ucm_device *d
     const char *device_name;
     char *name;
     const char *jack_control;
+    char *alsa_name;
 
     pa_assert(ucm);
     pa_assert(device);
@@ -1280,17 +1281,14 @@ static pa_alsa_jack* ucm_get_jack(pa_alsa_ucm_config *ucm, pa_alsa_ucm_device *d
         if (pa_streq(j->name, name))
             goto out;
 
-    j = pa_xnew0(pa_alsa_jack, 1);
-    j->state_unplugged = PA_AVAILABLE_NO;
-    j->state_plugged = PA_AVAILABLE_YES;
-    j->name = pa_xstrdup(name);
-
     jack_control = pa_proplist_gets(device->proplist, PA_ALSA_PROP_UCM_JACK_CONTROL);
     if (jack_control)
-        j->alsa_name = pa_xstrdup(jack_control);
+        alsa_name = pa_xstrdup(jack_control);
     else
-        j->alsa_name = pa_sprintf_malloc("%s Jack", device_name);
+        alsa_name = pa_sprintf_malloc("%s Jack", device_name);
 
+    j = pa_alsa_jack_new(NULL, name, alsa_name);
+    pa_xfree(alsa_name);
     PA_LLIST_PREPEND(pa_alsa_jack, ucm->jacks, j);
 
 out:
@@ -1597,9 +1595,7 @@ void pa_alsa_ucm_free(pa_alsa_ucm_config *ucm) {
     }
     PA_LLIST_FOREACH_SAFE(ji, jn, ucm->jacks) {
         PA_LLIST_REMOVE(pa_alsa_jack, ucm->jacks, ji);
-        pa_xfree(ji->alsa_name);
-        pa_xfree(ji->name);
-        pa_xfree(ji);
+        pa_alsa_jack_free(ji);
     }
     if (ucm->ucm_mgr) {
         snd_use_case_mgr_close(ucm->ucm_mgr);
-- 
1.9.3



More information about the pulseaudio-discuss mailing list