[pulseaudio-commits] r1263 - in /trunk/src/pulsecore: hashmap.c hashmap.h

svnmailer-noreply at 0pointer.de svnmailer-noreply at 0pointer.de
Fri Aug 18 12:43:49 PDT 2006


Author: lennart
Date: Fri Aug 18 21:43:46 2006
New Revision: 1263

URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=1263&root=pulseaudio&view=rev
Log:
cleanup hashmap.[ch] a little: use hash/compare func prototypes defined in idxset.h, add pa_hashmpa_{get,steal}_first

Modified:
    trunk/src/pulsecore/hashmap.c
    trunk/src/pulsecore/hashmap.h

Modified: trunk/src/pulsecore/hashmap.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/hashmap.c?rev=1263&root=pulseaudio&r1=1262&r2=1263&view=diff
==============================================================================
--- trunk/src/pulsecore/hashmap.c (original)
+++ trunk/src/pulsecore/hashmap.c Fri Aug 18 21:43:46 2006
@@ -49,22 +49,25 @@
     struct hashmap_entry *first_entry;
     
     unsigned n_entries;
-    unsigned (*hash_func) (const void *p);
-    int (*compare_func) (const void*a, const void*b);
+    pa_hash_func_t hash_func;
+    pa_compare_func_t compare_func;
 };
 
-pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b)) {
+pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
     pa_hashmap *h;
-    h = pa_xmalloc(sizeof(pa_hashmap));
-    h->data = pa_xmalloc0(sizeof(struct hashmap_entry*)*(h->size = BUCKETS));
+    
+    h = pa_xnew(pa_hashmap, 1);
+    h->data = pa_xnew0(struct hashmap_entry*, h->size = BUCKETS);
     h->first_entry = NULL;
     h->n_entries = 0;
     h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
     h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
+    
     return h;
 }
 
 static void remove(pa_hashmap *h, struct hashmap_entry *e) {
+    assert(h);
     assert(e);
 
     if (e->next)
@@ -102,7 +105,8 @@
 
 static struct hashmap_entry *get(pa_hashmap *h, unsigned hash, const void *key) {
     struct hashmap_entry *e;
-    assert(h && hash < h->size);
+    assert(h);
+    assert(hash < h->size);
 
     for (e = h->data[hash]; e; e = e->bucket_next)
         if (h->compare_func(e->key, key) == 0)
@@ -121,7 +125,7 @@
     if ((e = get(h, hash, key)))
         return -1;
     
-    e = pa_xmalloc(sizeof(struct hashmap_entry));
+    e = pa_xnew(struct hashmap_entry, 1);
     e->hash = hash;
     e->key = key;
     e->value = value;
@@ -145,7 +149,8 @@
 void* pa_hashmap_get(pa_hashmap *h, const void *key) {
     unsigned hash;
     struct hashmap_entry *e;
-    assert(h && key);
+
+    assert(h);
 
     hash = h->hash_func(key) % h->size;
 
@@ -159,7 +164,8 @@
     struct hashmap_entry *e;
     unsigned hash;
     void *data;
-    assert(h && key);
+    
+    assert(h);
 
     hash = h->hash_func(key) % h->size;
 
@@ -176,7 +182,8 @@
 }
 
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
-    assert(h && state);
+    assert(h);
+    assert(state);
 
     if (!*state) 
         *state = h->first_entry;
@@ -194,3 +201,25 @@
     
     return ((struct hashmap_entry*) *state)->value;
 }
+
+void* pa_hashmap_steal_first(pa_hashmap *h) {
+    void *data;
+    
+    assert(h);
+
+    if (!h->first_entry)
+        return NULL;
+
+    data = h->first_entry->value;
+    remove(h, h->first_entry);
+    return data;
+}
+
+void *pa_hashmap_get_first(pa_hashmap *h) {
+    assert(h);
+
+    if (!h->first_entry)
+        return NULL;
+
+    return h->first_entry->value;
+}

Modified: trunk/src/pulsecore/hashmap.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/trunk/src/pulsecore/hashmap.h?rev=1263&root=pulseaudio&r1=1262&r2=1263&view=diff
==============================================================================
--- trunk/src/pulsecore/hashmap.h (original)
+++ trunk/src/pulsecore/hashmap.h Fri Aug 18 21:43:46 2006
@@ -22,6 +22,8 @@
   USA.
 ***/
 
+#include <pulsecore/idxset.h>
+
 /* Simple Implementation of a hash table. Memory management is the
  * user's job. It's a good idea to have the key pointer point to a
  * string in the value data. */
@@ -29,7 +31,7 @@
 typedef struct pa_hashmap pa_hashmap;
 
 /* Create a new hashmap. Use the specified functions for hashing and comparing objects in the map */
-pa_hashmap *pa_hashmap_new(unsigned (*hash_func) (const void *p), int (*compare_func) (const void*a, const void*b));
+pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func);
 
 /* Free the hash table. Calls the specified function for every value in the table. The function may be NULL */
 void pa_hashmap_free(pa_hashmap*, void (*free_func)(void *p, void *userdata), void *userdata);
@@ -50,4 +52,8 @@
    is returned. */
 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void**key);
 
+void *pa_hashmap_steal_first(pa_hashmap *h);
+
+void *pa_hashmap_get_first(pa_hashmap *h);
+
 #endif




More information about the pulseaudio-commits mailing list