]> code.delx.au - pulseaudio/blobdiff - polyp/hashmap.c
Make the whole stuff LGPL only
[pulseaudio] / polyp / hashmap.c
index 51e3879b5b1e82624547e39098b1ffd2da8ec73d..43e4456dbd3daee08069ed7f1aed8b8951ba7907 100644 (file)
@@ -4,7 +4,7 @@
   This file is part of polypaudio.
  
   polypaudio is free software; you can redistribute it and/or modify
-  it under the terms of the GNU General Public License as published
+  it under the terms of the GNU Lesser General Public License as published
   by the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.
  
@@ -13,7 +13,7 @@
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   General Public License for more details.
  
-  You should have received a copy of the GNU General Public License
+  You should have received a copy of the GNU Lesser General Public License
   along with polypaudio; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.
@@ -29,6 +29,8 @@
 
 #include "hashmap.h"
 #include "idxset.h"
+#include "xmalloc.h"
+#include "log.h"
 
 struct hashmap_entry {
     struct hashmap_entry *next, *previous, *bucket_next, *bucket_previous;
@@ -49,11 +51,8 @@ struct pa_hashmap {
 
 struct pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) {
     struct pa_hashmap *h;
-    h = malloc(sizeof(struct pa_hashmap));
-    assert(h);
-    h->data = malloc(sizeof(struct hashmap_entry*)*(h->size = 1023));
-    assert(h->data);
-    memset(h->data, 0, sizeof(struct hashmap_entry*)*(h->size = 1023));
+    h = pa_xmalloc(sizeof(struct pa_hashmap));
+    h->data = pa_xmalloc0(sizeof(struct hashmap_entry*)*(h->size = 1023));
     h->first_entry = NULL;
     h->n_entries = 0;
     h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
@@ -78,7 +77,7 @@ static void remove(struct pa_hashmap *h, struct hashmap_entry *e) {
     else
         h->data[e->hash] = e->bucket_next;
 
-    free(e);
+    pa_xfree(e);
     h->n_entries--;
 }
 
@@ -91,8 +90,8 @@ void pa_hashmap_free(struct pa_hashmap*h, void (*free_func)(void *p, void *userd
         remove(h, h->first_entry);
     }
     
-    free(h->data);
-    free(h);
+    pa_xfree(h->data);
+    pa_xfree(h);
 }
 
 static struct hashmap_entry *get(struct pa_hashmap *h, unsigned hash, const void *key) {
@@ -115,9 +114,7 @@ int pa_hashmap_put(struct pa_hashmap *h, const void *key, void *value) {
     if ((e = get(h, hash, key)))
         return -1;
     
-    e = malloc(sizeof(struct hashmap_entry));
-    assert(e);
-    
+    e = pa_xmalloc(sizeof(struct hashmap_entry));
     e->hash = hash;
     e->key = key;
     e->value = value;
@@ -151,25 +148,27 @@ void* pa_hashmap_get(struct pa_hashmap *h, const void *key) {
     return e->value;
 }
 
-int pa_hashmap_remove(struct pa_hashmap *h, const void *key) {
+void* pa_hashmap_remove(struct pa_hashmap *h, const void *key) {
     struct hashmap_entry *e;
     unsigned hash;
+    void *data;
     assert(h && key);
 
     hash = h->hash_func(key) % h->size;
 
     if (!(e = get(h, hash, key)))
-        return 1;
+        return NULL;
 
+    data = e->value;
     remove(h, e);
-    return 0;
+    return data;
 }
 
 unsigned pa_hashmap_ncontents(struct pa_hashmap *h) {
     return h->n_entries;
 }
 
-void *pa_hashmap_iterate(struct pa_hashmap *h, void **state) {
+void *pa_hashmap_iterate(struct pa_hashmap *h, void **state, const void **key) {
     assert(h && state);
 
     if (!*state) {
@@ -177,8 +176,14 @@ void *pa_hashmap_iterate(struct pa_hashmap *h, void **state) {
     } else
         *state = ((struct hashmap_entry*) *state)->next;
 
-    if (!*state)
+    if (!*state) {
+        if (key)
+            *key = NULL;
         return NULL;
+    }
+
+    if (key)
+        *key = ((struct hashmap_entry*) *state)->key;
     
     return ((struct hashmap_entry*) *state)->value;
 }