4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <avahi-common/llist.h>
30 #include <avahi-common/domain.h>
31 #include <avahi-common/malloc.h>
36 #define HASH_MAP_SIZE 123
38 typedef struct Entry Entry;
40 AvahiHashmap *hashmap;
44 AVAHI_LLIST_FIELDS(Entry, bucket);
45 AVAHI_LLIST_FIELDS(Entry, entries);
49 AvahiHashFunc hash_func;
50 AvahiEqualFunc equal_func;
51 AvahiFreeFunc key_free_func, value_free_func;
53 Entry *entries[HASH_MAP_SIZE];
54 AVAHI_LLIST_HEAD(Entry, entries_list);
57 static Entry* entry_get(AvahiHashmap *m, const void *key) {
61 idx = m->hash_func(key) % HASH_MAP_SIZE;
63 for (e = m->entries[idx]; e; e = e->bucket_next)
64 if (m->equal_func(key, e->key))
70 static void entry_free(AvahiHashmap *m, Entry *e, int stolen) {
75 idx = m->hash_func(e->key) % HASH_MAP_SIZE;
77 AVAHI_LLIST_REMOVE(Entry, bucket, m->entries[idx], e);
78 AVAHI_LLIST_REMOVE(Entry, entries, m->entries_list, e);
81 m->key_free_func(e->key);
82 if (m->value_free_func && !stolen)
83 m->value_free_func(e->value);
88 AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_func, AvahiFreeFunc key_free_func, AvahiFreeFunc value_free_func) {
94 if (!(m = avahi_new(AvahiHashmap, 1)))
97 m->hash_func = hash_func;
98 m->equal_func = equal_func;
99 m->key_free_func = key_free_func;
100 m->value_free_func = value_free_func;
102 memset(m->entries, 0, sizeof(m->entries));
104 AVAHI_LLIST_HEAD_INIT(Entry, m->entries_list);
109 void avahi_hashmap_free(AvahiHashmap *m) {
112 while (m->entries_list)
113 entry_free(m, m->entries_list, 0);
118 void* avahi_hashmap_lookup(AvahiHashmap *m, const void *key) {
123 if (!(e = entry_get(m, key)))
129 void* avahi_hashmap_steal(AvahiHashmap *m, const void *key) {
135 if (!(e = entry_get(m, key)))
143 int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) {
149 if ((e = entry_get(m, key))) {
150 if (m->key_free_func)
151 m->key_free_func(key);
152 if (m->value_free_func)
153 m->value_free_func(value);
158 if (!(e = avahi_new(Entry, 1)))
165 AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e);
167 idx = m->hash_func(key) % HASH_MAP_SIZE;
168 AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e);
174 int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) {
180 if ((e = entry_get(m, key))) {
181 if (m->key_free_func)
182 m->key_free_func(e->key);
183 if (m->value_free_func)
184 m->value_free_func(e->value);
192 if (!(e = avahi_new(Entry, 1)))
199 AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e);
201 idx = m->hash_func(key) % HASH_MAP_SIZE;
202 AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e);
207 void avahi_hashmap_remove(AvahiHashmap *m, const void *key) {
212 if (!(e = entry_get(m, key)))
218 void avahi_hashmap_foreach(AvahiHashmap *m, AvahiHashmapForeachCallback callback, void *userdata) {
223 for (e = m->entries_list; e; e = next) {
224 next = e->entries_next;
226 callback(e->key, e->value, userdata);
230 unsigned avahi_string_hash(const void *data) {
231 const char *p = data;
235 hash = 31 * hash + *p;
240 int avahi_string_equal(const void *a, const void *b) {
241 const char *p = a, *q = b;
243 return strcmp(p, q) == 0;
246 unsigned avahi_int_hash(const void *data) {
249 return (unsigned) *i;
252 int avahi_int_equal(const void *a, const void *b) {
253 const int *_a = a, *_b = b;