]> code.delx.au - pulseaudio/blobdiff - src/pulsecore/hashmap.c
hashmap: Add pa_hashmap_remove_and_free()
[pulseaudio] / src / pulsecore / hashmap.c
index 57607b69e251c65772e31dc52287dc7682157b0d..2cc03cbab91f26206462acaaf61a01d12a517e2d 100644 (file)
@@ -5,7 +5,7 @@
 
   PulseAudio is free software; you can redistribute it and/or modify
   it under the terms of the GNU Lesser General Public License as published
-  by the Free Software Foundation; either version 2 of the License,
+  by the Free Software Foundation; either version 2.1 of the License,
   or (at your option) any later version.
 
   PulseAudio is distributed in the hope that it will be useful, but
 #endif
 
 #include <stdlib.h>
-#include <string.h>
 
 #include <pulse/xmalloc.h>
 #include <pulsecore/idxset.h>
-#include <pulsecore/log.h>
 #include <pulsecore/flist.h>
 #include <pulsecore/macro.h>
 
@@ -37,7 +35,7 @@
 #define NBUCKETS 127
 
 struct hashmap_entry {
-    const void *key;
+    void *key;
     void *value;
 
     struct hashmap_entry *bucket_next, *bucket_previous;
@@ -48,6 +46,9 @@ struct pa_hashmap {
     pa_hash_func_t hash_func;
     pa_compare_func_t compare_func;
 
+    pa_free_cb_t key_free_func;
+    pa_free_cb_t value_free_func;
+
     struct hashmap_entry *iterate_list_head, *iterate_list_tail;
     unsigned n_entries;
 };
@@ -56,7 +57,7 @@ struct pa_hashmap {
 
 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
 
-pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
+pa_hashmap *pa_hashmap_new_full(pa_hash_func_t hash_func, pa_compare_func_t compare_func, pa_free_cb_t key_free_func, pa_free_cb_t value_free_func) {
     pa_hashmap *h;
 
     h = pa_xmalloc0(PA_ALIGN(sizeof(pa_hashmap)) + NBUCKETS*sizeof(struct hashmap_entry*));
@@ -64,12 +65,19 @@ pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_f
     h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
     h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
 
+    h->key_free_func = key_free_func;
+    h->value_free_func = value_free_func;
+
     h->n_entries = 0;
     h->iterate_list_head = h->iterate_list_tail = NULL;
 
     return h;
 }
 
+pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
+    return pa_hashmap_new_full(hash_func, compare_func, NULL, NULL);
+}
+
 static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
     pa_assert(h);
     pa_assert(e);
@@ -96,6 +104,9 @@ static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
         BY_HASH(h)[hash] = e->bucket_next;
     }
 
+    if (h->key_free_func)
+        h->key_free_func(e->key);
+
     if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
         pa_xfree(e);
 
@@ -103,18 +114,10 @@ static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
     h->n_entries--;
 }
 
-void pa_hashmap_free(pa_hashmap*h, pa_free2_cb_t free_cb, void *userdata) {
+void pa_hashmap_free(pa_hashmap *h) {
     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, userdata);
-    }
-
+    pa_hashmap_remove_all(h);
     pa_xfree(h);
 }
 
@@ -130,7 +133,7 @@ static struct hashmap_entry *hash_scan(pa_hashmap *h, unsigned hash, const void
     return NULL;
 }
 
-int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
+int pa_hashmap_put(pa_hashmap *h, void *key, void *value) {
     struct hashmap_entry *e;
     unsigned hash;
 
@@ -204,6 +207,32 @@ void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
     return data;
 }
 
+int pa_hashmap_remove_and_free(pa_hashmap *h, const void *key) {
+    void *data;
+
+    pa_assert(h);
+
+    data = pa_hashmap_remove(h, key);
+
+    if (data && h->value_free_func)
+        h->value_free_func(data);
+
+    return data ? 0 : -1;
+}
+
+void pa_hashmap_remove_all(pa_hashmap *h) {
+    pa_assert(h);
+
+    while (h->iterate_list_head) {
+        void *data;
+        data = h->iterate_list_head->value;
+        remove_entry(h, h->iterate_list_head);
+
+        if (h->value_free_func)
+            h->value_free_func(data);
+    }
+}
+
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
     struct hashmap_entry *e;
 
@@ -237,6 +266,39 @@ at_end:
     return NULL;
 }
 
+void *pa_hashmap_iterate_backwards(pa_hashmap *h, void **state, const void **key) {
+    struct hashmap_entry *e;
+
+    pa_assert(h);
+    pa_assert(state);
+
+    if (*state == (void*) -1)
+        goto at_beginning;
+
+    if (!*state && !h->iterate_list_tail)
+        goto at_beginning;
+
+    e = *state ? *state : h->iterate_list_tail;
+
+    if (e->iterate_previous)
+        *state = e->iterate_previous;
+    else
+        *state = (void*) -1;
+
+    if (key)
+        *key = e->key;
+
+    return e->value;
+
+at_beginning:
+    *state = (void *) -1;
+
+    if (key)
+        *key = NULL;
+
+    return NULL;
+}
+
 void* pa_hashmap_first(pa_hashmap *h) {
     pa_assert(h);
 
@@ -246,6 +308,15 @@ void* pa_hashmap_first(pa_hashmap *h) {
     return h->iterate_list_head->value;
 }
 
+void* pa_hashmap_last(pa_hashmap *h) {
+    pa_assert(h);
+
+    if (!h->iterate_list_tail)
+        return NULL;
+
+    return h->iterate_list_tail->value;
+}
+
 void* pa_hashmap_steal_first(pa_hashmap *h) {
     void *data;
 
@@ -266,7 +337,7 @@ unsigned pa_hashmap_size(pa_hashmap *h) {
     return h->n_entries;
 }
 
-pa_bool_t pa_hashmap_isempty(pa_hashmap *h) {
+bool pa_hashmap_isempty(pa_hashmap *h) {
     pa_assert(h);
 
     return h->n_entries == 0;