]> git.meshlink.io Git - catta/blobdiff - avahi-core/hashmap.c
forgot to pull the publish_no_reverse change to the example.
[catta] / avahi-core / hashmap.c
index 0c44d4ff5920d2439744bc6f04e437f459015f4b..9b55bd316bcc258604443d902918f2f5b29c383a 100644 (file)
@@ -1,18 +1,16 @@
-/* $Id$ */
-
 /***
   This file is part of avahi.
+
   avahi 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.1 of the
   License, or (at your option) any later version.
+
   avahi is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
   or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
   Public License for more details.
+
   You should have received a copy of the GNU Lesser General Public
   License along with avahi; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
@@ -49,7 +47,7 @@ struct AvahiHashmap {
     AvahiHashFunc hash_func;
     AvahiEqualFunc equal_func;
     AvahiFreeFunc key_free_func, value_free_func;
-    
+
     Entry *entries[HASH_MAP_SIZE];
     AVAHI_LLIST_HEAD(Entry, entries_list);
 };
@@ -57,9 +55,9 @@ struct AvahiHashmap {
 static Entry* entry_get(AvahiHashmap *m, const void *key) {
     unsigned idx;
     Entry *e;
-    
+
     idx = m->hash_func(key) % HASH_MAP_SIZE;
-    
+
     for (e = m->entries[idx]; e; e = e->bucket_next)
         if (m->equal_func(key, e->key))
             return e;
@@ -69,6 +67,7 @@ static Entry* entry_get(AvahiHashmap *m, const void *key) {
 
 static void entry_free(AvahiHashmap *m, Entry *e, int stolen) {
     unsigned idx;
+    assert(m);
     assert(e);
 
     idx = m->hash_func(e->key) % HASH_MAP_SIZE;
@@ -86,11 +85,11 @@ static void entry_free(AvahiHashmap *m, Entry *e, int stolen) {
 
 AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_func, AvahiFreeFunc key_free_func, AvahiFreeFunc value_free_func) {
     AvahiHashmap *m;
-    
+
     assert(hash_func);
     assert(equal_func);
 
-    if (!(m = avahi_new(AvahiHashmap, 1)))
+    if (!(m = avahi_new0(AvahiHashmap, 1)))
         return NULL;
 
     m->hash_func = hash_func;
@@ -98,10 +97,8 @@ AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_fu
     m->key_free_func = key_free_func;
     m->value_free_func = value_free_func;
 
-    memset(m->entries, 0, sizeof(m->entries));
-
     AVAHI_LLIST_HEAD_INIT(Entry, m->entries_list);
-    
+
     return m;
 }
 
@@ -110,33 +107,19 @@ void avahi_hashmap_free(AvahiHashmap *m) {
 
     while (m->entries_list)
         entry_free(m, m->entries_list, 0);
-    
+
     avahi_free(m);
 }
 
 void* avahi_hashmap_lookup(AvahiHashmap *m, const void *key) {
     Entry *e;
-    
-    assert(m);
-
-    if (!(e = entry_get(m, key)))
-        return NULL;
 
-    return e->value;
-}
-
-void* avahi_hashmap_steal(AvahiHashmap *m, const void *key) {
-    Entry *e;
-    void *v;
-    
     assert(m);
 
     if (!(e = entry_get(m, key)))
         return NULL;
 
-    v = e->value;
-    entry_free(m, e, 1);
-    return v;
+    return e->value;
 }
 
 int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) {
@@ -150,7 +133,7 @@ int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) {
             m->key_free_func(key);
         if (m->value_free_func)
             m->value_free_func(value);
-        
+
         return 1;
     }
 
@@ -165,7 +148,7 @@ int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) {
 
     idx = m->hash_func(key) % HASH_MAP_SIZE;
     AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e);
-        
+
     return 0;
 }
 
@@ -184,7 +167,7 @@ int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) {
 
         e->key = key;
         e->value = value;
-            
+
         return 1;
     }
 
@@ -199,14 +182,13 @@ int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) {
 
     idx = m->hash_func(key) % HASH_MAP_SIZE;
     AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e);
-        
+
     return 0;
 }
 
-
 void avahi_hashmap_remove(AvahiHashmap *m, const void *key) {
     Entry *e;
-    
+
     assert(m);
 
     if (!(e = entry_get(m, key)))
@@ -215,10 +197,24 @@ void avahi_hashmap_remove(AvahiHashmap *m, const void *key) {
     entry_free(m, e, 0);
 }
 
+void avahi_hashmap_foreach(AvahiHashmap *m, AvahiHashmapForeachCallback callback, void *userdata) {
+    Entry *e, *next;
+    assert(m);
+    assert(callback);
+
+    for (e = m->entries_list; e; e = next) {
+        next = e->entries_next;
+
+        callback(e->key, e->value, userdata);
+    }
+}
+
 unsigned avahi_string_hash(const void *data) {
     const char *p = data;
     unsigned hash = 0;
 
+    assert(p);
+
     for (; *p; p++)
         hash = 31 * hash + *p;
 
@@ -228,22 +224,25 @@ unsigned avahi_string_hash(const void *data) {
 int avahi_string_equal(const void *a, const void *b) {
     const char *p = a, *q = b;
 
+    assert(p);
+    assert(q);
+
     return strcmp(p, q) == 0;
 }
 
-unsigned avahi_domain_hash(const void *data) {
-    unsigned hash = 0;
-    const char *s;
-    
-    for (;;) {
-        char c[65];
-
-        if (!avahi_unescape_label(&s, c, sizeof(c)))
-            return hash;
-
-        if (!c[0])
-            continue;
-        
-        hash += avahi_string_hash(avahi_strdown(c));
-    }
+unsigned avahi_int_hash(const void *data) {
+    const int *i = data;
+
+    assert(i);
+
+    return (unsigned) *i;
+}
+
+int avahi_int_equal(const void *a, const void *b) {
+    const int *_a = a, *_b = b;
+
+    assert(_a);
+    assert(_b);
+
+    return *_a == *_b;
 }