[pulseaudio-discuss] [PATCH 06/13] hashmap: Add pa_hashmap_remove_all()

Tanu Kaskinen tanuk at iki.fi
Tue Feb 12 11:36:56 PST 2013


Slightly nicer than using pa_hashmap_steal_first() in a loop.
---
 src/modules/alsa/alsa-mixer.c      |    2 +-
 src/modules/module-systemd-login.c |    3 +--
 src/pulse/proplist.c               |    4 +---
 src/pulsecore/database-simple.c    |    4 +---
 src/pulsecore/hashmap.c            |   23 ++++++++++++++---------
 src/pulsecore/hashmap.h            |    3 +++
 src/pulsecore/protocol-dbus.c      |    6 ++----
 7 files changed, 23 insertions(+), 22 deletions(-)

diff --git a/src/modules/alsa/alsa-mixer.c b/src/modules/alsa/alsa-mixer.c
index 881d13d..97f25af 100644
--- a/src/modules/alsa/alsa-mixer.c
+++ b/src/modules/alsa/alsa-mixer.c
@@ -3740,7 +3740,7 @@ static void mapping_paths_probe(pa_alsa_mapping *m, pa_alsa_profile *profile,
     mixer_handle = pa_alsa_open_mixer_for_pcm(pcm_handle, NULL, &hctl_handle);
     if (!mixer_handle || !hctl_handle) {
          /* Cannot open mixer, remove all entries */
-        while (pa_hashmap_steal_first(ps->paths));
+        pa_hashmap_remove_all(ps->paths, NULL);
         return;
     }
 
diff --git a/src/modules/module-systemd-login.c b/src/modules/module-systemd-login.c
index 2f24711..8ad836e 100644
--- a/src/modules/module-systemd-login.c
+++ b/src/modules/module-systemd-login.c
@@ -141,8 +141,7 @@ static int get_session_list(struct userdata *u) {
         free(sessions);
     }
 
-    while ((o = pa_hashmap_steal_first(u->previous_sessions)))
-        free_session(o);
+    pa_hashmap_remove_all(u->previous_sessions, (pa_free_cb_t) free_session);
 
     return 0;
 }
diff --git a/src/pulse/proplist.c b/src/pulse/proplist.c
index 33b150c..abd551b 100644
--- a/src/pulse/proplist.c
+++ b/src/pulse/proplist.c
@@ -652,11 +652,9 @@ int pa_proplist_contains(pa_proplist *p, const char *key) {
 }
 
 void pa_proplist_clear(pa_proplist *p) {
-    struct property *prop;
     pa_assert(p);
 
-    while ((prop = pa_hashmap_steal_first(MAKE_HASHMAP(p))))
-        property_free(prop);
+    pa_hashmap_remove_all(MAKE_HASHMAP(p), (pa_free_cb_t) property_free);
 }
 
 pa_proplist* pa_proplist_copy(const pa_proplist *p) {
diff --git a/src/pulsecore/database-simple.c b/src/pulsecore/database-simple.c
index 5dd3ac9..91c1b45 100644
--- a/src/pulsecore/database-simple.c
+++ b/src/pulsecore/database-simple.c
@@ -339,12 +339,10 @@ int pa_database_unset(pa_database *database, const pa_datum *key) {
 
 int pa_database_clear(pa_database *database) {
     simple_data *db = (simple_data*)database;
-    entry *e;
 
     pa_assert(db);
 
-    while ((e = pa_hashmap_steal_first(db->map)))
-        free_entry(e);
+    pa_hashmap_remove_all(db->map, (pa_free_cb_t) free_entry);
 
     return 0;
 }
diff --git a/src/pulsecore/hashmap.c b/src/pulsecore/hashmap.c
index cfd08b7..3e1d9f1 100644
--- a/src/pulsecore/hashmap.c
+++ b/src/pulsecore/hashmap.c
@@ -104,15 +104,7 @@ static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
 void pa_hashmap_free(pa_hashmap *h, pa_free_cb_t free_cb) {
     pa_assert(h);
 
-    while (h->iterate_list_head) {
-        void *data;
-        data = h->iterate_list_head->value;
-        remove_entry(h, h->iterate_list_head);
-
-        if (free_cb)
-            free_cb(data);
-    }
-
+    pa_hashmap_remove_all(h, free_cb);
     pa_xfree(h);
 }
 
@@ -202,6 +194,19 @@ void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
     return data;
 }
 
+void pa_hashmap_remove_all(pa_hashmap *h, pa_free_cb_t free_cb) {
+    pa_assert(h);
+
+    while (h->iterate_list_head) {
+        void *data;
+        data = h->iterate_list_head->value;
+        remove_entry(h, h->iterate_list_head);
+
+        if (free_cb)
+            free_cb(data);
+    }
+}
+
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
     struct hashmap_entry *e;
 
diff --git a/src/pulsecore/hashmap.h b/src/pulsecore/hashmap.h
index 8d892b8..59ff12e 100644
--- a/src/pulsecore/hashmap.h
+++ b/src/pulsecore/hashmap.h
@@ -48,6 +48,9 @@ void* pa_hashmap_get(pa_hashmap *h, const void *key);
 /* Returns the data of the entry while removing */
 void* pa_hashmap_remove(pa_hashmap *h, const void *key);
 
+/* If free_cb is not NULL, it's called for each entry. */
+void pa_hashmap_remove_all(pa_hashmap *h, pa_free_cb_t free_cb);
+
 /* Return the current number of entries of the hashmap */
 unsigned pa_hashmap_size(pa_hashmap *h);
 
diff --git a/src/pulsecore/protocol-dbus.c b/src/pulsecore/protocol-dbus.c
index ffd8ea2..bda2346 100644
--- a/src/pulsecore/protocol-dbus.c
+++ b/src/pulsecore/protocol-dbus.c
@@ -1008,8 +1008,7 @@ void pa_dbus_protocol_add_signal_listener(
 
         /* We're not interested in individual signals anymore, so let's empty
          * listening_signals. */
-        while ((signal_paths_entry = pa_hashmap_steal_first(conn_entry->listening_signals)))
-            signal_paths_entry_free(signal_paths_entry);
+        pa_hashmap_remove_all(conn_entry->listening_signals, (pa_free_cb_t) signal_paths_entry_free);
 
         for (i = 0; i < n_objects; ++i)
             pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL);
@@ -1037,8 +1036,7 @@ void pa_dbus_protocol_remove_signal_listener(pa_dbus_protocol *p, DBusConnection
         while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
             pa_xfree(object_path);
 
-        while ((signal_paths_entry = pa_hashmap_steal_first(conn_entry->listening_signals)))
-            signal_paths_entry_free(signal_paths_entry);
+        pa_hashmap_remove_all(conn_entry->listening_signals, (pa_free_cb_t) signal_paths_entry_free);
     }
 }
 
-- 
1.7.10.4



More information about the pulseaudio-discuss mailing list