]> code.delx.au - pulseaudio/commitdiff
allow on-the-fly deleting of hashmap entries wile we iterate through them
authorLennart Poettering <lennart@poettering.net>
Thu, 29 May 2008 15:16:58 +0000 (15:16 +0000)
committerLennart Poettering <lennart@poettering.net>
Thu, 29 May 2008 15:16:58 +0000 (15:16 +0000)
git-svn-id: file:///home/lennart/svn/public/pulseaudio/trunk@2491 fefdeb5f-60dc-0310-8127-8f9354f1896f

src/pulsecore/hashmap.c
src/pulsecore/hashmap.h

index c9d5632c545b93e9b632e43615e4e5fe9287abd2..7ca8d0e60339c7ebf21813ec57bb0e8945c60e69 100644 (file)
@@ -191,24 +191,36 @@ unsigned pa_hashmap_size(pa_hashmap *h) {
 }
 
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
+    struct hashmap_entry *e;
+
     pa_assert(h);
     pa_assert(state);
 
-    if (!*state)
-        *state = h->first_entry;
+    if (*state == (void*) -1)
+        goto at_end;
+
+    if ((!*state && !h->first_entry))
+        goto at_end;
+
+    e = *state ? *state : h->first_entry;
+
+    if (e->next)
+        *state = e->next;
     else
-        *state = ((struct hashmap_entry*) *state)->next;
+        *state = (void*) -1;
 
-    if (!*state) {
-        if (key)
-            *key = NULL;
-        return NULL;
-    }
+    if (key)
+        *key = e->key;
+
+    return e->value;
+
+at_end:
+    *state = (void *) -1;
 
     if (key)
-        *key = ((struct hashmap_entry*) *state)->key;
+        *key = NULL;
 
-    return ((struct hashmap_entry*) *state)->value;
+    return NULL;
 }
 
 void* pa_hashmap_steal_first(pa_hashmap *h) {
index 98df45021301c0c9bf3e06b464a077bf93ed908a..6a5e0f24ba05b4ee22388cdc515493c16fb6410b 100644 (file)
@@ -51,9 +51,10 @@ unsigned pa_hashmap_size(pa_hashmap *h);
 
 /* May be used to iterate through the hashmap. Initially the opaque
    pointer *state has to be set to NULL. The hashmap may not be
-   modified during iteration. The key of the entry is returned in
-   *key, if key is non-NULL. After the last entry in the hashmap NULL
-   is returned. */
+   modified during iteration -- except for deleting the current entry
+   via pa_hashmap_remove(). The key of the entry is returned in *key,
+   if key is non-NULL. After the last entry in the hashmap NULL is
+   returned. */
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void**key);
 
 void *pa_hashmap_steal_first(pa_hashmap *h);