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) {
74 idx = m->hash_func(e->key) % HASH_MAP_SIZE;
76 AVAHI_LLIST_REMOVE(Entry, bucket, m->entries[idx], e);
77 AVAHI_LLIST_REMOVE(Entry, entries, m->entries_list, e);
80 m->key_free_func(e->key);
81 if (m->value_free_func && !stolen)
82 m->value_free_func(e->value);
87 AvahiHashmap* avahi_hashmap_new(AvahiHashFunc hash_func, AvahiEqualFunc equal_func, AvahiFreeFunc key_free_func, AvahiFreeFunc value_free_func) {
93 if (!(m = avahi_new(AvahiHashmap, 1)))
96 m->hash_func = hash_func;
97 m->equal_func = equal_func;
98 m->key_free_func = key_free_func;
99 m->value_free_func = value_free_func;
101 memset(m->entries, 0, sizeof(m->entries));
103 AVAHI_LLIST_HEAD_INIT(Entry, m->entries_list);
108 void avahi_hashmap_free(AvahiHashmap *m) {
111 while (m->entries_list)
112 entry_free(m, m->entries_list, 0);
117 void* avahi_hashmap_lookup(AvahiHashmap *m, const void *key) {
122 if (!(e = entry_get(m, key)))
128 void* avahi_hashmap_steal(AvahiHashmap *m, const void *key) {
134 if (!(e = entry_get(m, key)))
142 int avahi_hashmap_insert(AvahiHashmap *m, void *key, void *value) {
148 if ((e = entry_get(m, key))) {
149 if (m->key_free_func)
150 m->key_free_func(key);
151 if (m->value_free_func)
152 m->value_free_func(value);
157 if (!(e = avahi_new(Entry, 1)))
164 AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e);
166 idx = m->hash_func(key) % HASH_MAP_SIZE;
167 AVAHI_LLIST_PREPEND(Entry, bucket, m->entries[idx], e);
173 int avahi_hashmap_replace(AvahiHashmap *m, void *key, void *value) {
179 if ((e = entry_get(m, key))) {
180 if (m->key_free_func)
181 m->key_free_func(e->key);
182 if (m->value_free_func)
183 m->value_free_func(e->value);
191 if (!(e = avahi_new(Entry, 1)))
198 AVAHI_LLIST_PREPEND(Entry, entries, m->entries_list, e);
200 idx = m->hash_func(key) % HASH_MAP_SIZE;
201 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 unsigned avahi_string_hash(const void *data) {
219 const char *p = data;
223 hash = 31 * hash + *p;
228 int avahi_string_equal(const void *a, const void *b) {
229 const char *p = a, *q = b;
231 return strcmp(p, q) == 0;
234 unsigned avahi_domain_hash(const void *data) {
241 if (!avahi_unescape_label(&s, c, sizeof(c)))
247 hash += avahi_string_hash(avahi_strdown(c));