hashmap: Add pa_hashmap_remove_all()

Slightly nicer than using pa_hashmap_steal_first() in a loop.
This commit is contained in:
Tanu Kaskinen 2013-02-12 21:36:56 +02:00
parent 061878b5a4
commit 31ee1a7d54
7 changed files with 23 additions and 22 deletions

View file

@ -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); mixer_handle = pa_alsa_open_mixer_for_pcm(pcm_handle, NULL, &hctl_handle);
if (!mixer_handle || !hctl_handle) { if (!mixer_handle || !hctl_handle) {
/* Cannot open mixer, remove all entries */ /* Cannot open mixer, remove all entries */
while (pa_hashmap_steal_first(ps->paths)); pa_hashmap_remove_all(ps->paths, NULL);
return; return;
} }

View file

@ -141,8 +141,7 @@ static int get_session_list(struct userdata *u) {
free(sessions); free(sessions);
} }
while ((o = pa_hashmap_steal_first(u->previous_sessions))) pa_hashmap_remove_all(u->previous_sessions, (pa_free_cb_t) free_session);
free_session(o);
return 0; return 0;
} }

View file

@ -652,11 +652,9 @@ int pa_proplist_contains(pa_proplist *p, const char *key) {
} }
void pa_proplist_clear(pa_proplist *p) { void pa_proplist_clear(pa_proplist *p) {
struct property *prop;
pa_assert(p); pa_assert(p);
while ((prop = pa_hashmap_steal_first(MAKE_HASHMAP(p)))) pa_hashmap_remove_all(MAKE_HASHMAP(p), (pa_free_cb_t) property_free);
property_free(prop);
} }
pa_proplist* pa_proplist_copy(const pa_proplist *p) { pa_proplist* pa_proplist_copy(const pa_proplist *p) {

View file

@ -339,12 +339,10 @@ int pa_database_unset(pa_database *database, const pa_datum *key) {
int pa_database_clear(pa_database *database) { int pa_database_clear(pa_database *database) {
simple_data *db = (simple_data*)database; simple_data *db = (simple_data*)database;
entry *e;
pa_assert(db); pa_assert(db);
while ((e = pa_hashmap_steal_first(db->map))) pa_hashmap_remove_all(db->map, (pa_free_cb_t) free_entry);
free_entry(e);
return 0; return 0;
} }

View file

@ -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) { void pa_hashmap_free(pa_hashmap *h, pa_free_cb_t free_cb) {
pa_assert(h); pa_assert(h);
while (h->iterate_list_head) { pa_hashmap_remove_all(h, free_cb);
void *data;
data = h->iterate_list_head->value;
remove_entry(h, h->iterate_list_head);
if (free_cb)
free_cb(data);
}
pa_xfree(h); pa_xfree(h);
} }
@ -202,6 +194,19 @@ void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
return data; 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) { void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
struct hashmap_entry *e; struct hashmap_entry *e;

View file

@ -48,6 +48,9 @@ void* pa_hashmap_get(pa_hashmap *h, const void *key);
/* Returns the data of the entry while removing */ /* Returns the data of the entry while removing */
void* pa_hashmap_remove(pa_hashmap *h, const void *key); 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 */ /* Return the current number of entries of the hashmap */
unsigned pa_hashmap_size(pa_hashmap *h); unsigned pa_hashmap_size(pa_hashmap *h);

View file

@ -1008,8 +1008,7 @@ void pa_dbus_protocol_add_signal_listener(
/* We're not interested in individual signals anymore, so let's empty /* We're not interested in individual signals anymore, so let's empty
* listening_signals. */ * listening_signals. */
while ((signal_paths_entry = pa_hashmap_steal_first(conn_entry->listening_signals))) pa_hashmap_remove_all(conn_entry->listening_signals, (pa_free_cb_t) signal_paths_entry_free);
signal_paths_entry_free(signal_paths_entry);
for (i = 0; i < n_objects; ++i) for (i = 0; i < n_objects; ++i)
pa_idxset_put(conn_entry->all_signals_objects, pa_xstrdup(objects[i]), NULL); 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))) while ((object_path = pa_idxset_steal_first(conn_entry->all_signals_objects, NULL)))
pa_xfree(object_path); pa_xfree(object_path);
while ((signal_paths_entry = pa_hashmap_steal_first(conn_entry->listening_signals))) pa_hashmap_remove_all(conn_entry->listening_signals, (pa_free_cb_t) signal_paths_entry_free);
signal_paths_entry_free(signal_paths_entry);
} }
} }