default: esd is obsolete, hence don't load it anymore by default
[pulseaudio-mirror.git] / src / pulsecore / hashmap.c
blobe368512b24bf1fe823b0e780fbb3318a14e6a2db
1 /***
2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdlib.h>
28 #include <pulse/xmalloc.h>
29 #include <pulsecore/idxset.h>
30 #include <pulsecore/flist.h>
31 #include <pulsecore/macro.h>
33 #include "hashmap.h"
35 #define NBUCKETS 127
37 struct hashmap_entry {
38 const void *key;
39 void *value;
41 struct hashmap_entry *bucket_next, *bucket_previous;
42 struct hashmap_entry *iterate_next, *iterate_previous;
45 struct pa_hashmap {
46 pa_hash_func_t hash_func;
47 pa_compare_func_t compare_func;
49 struct hashmap_entry *iterate_list_head, *iterate_list_tail;
50 unsigned n_entries;
53 #define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + PA_ALIGN(sizeof(pa_hashmap))))
55 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
57 pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
58 pa_hashmap *h;
60 h = pa_xmalloc0(PA_ALIGN(sizeof(pa_hashmap)) + NBUCKETS*sizeof(struct hashmap_entry*));
62 h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
63 h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
65 h->n_entries = 0;
66 h->iterate_list_head = h->iterate_list_tail = NULL;
68 return h;
71 static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
72 pa_assert(h);
73 pa_assert(e);
75 /* Remove from iteration list */
76 if (e->iterate_next)
77 e->iterate_next->iterate_previous = e->iterate_previous;
78 else
79 h->iterate_list_tail = e->iterate_previous;
81 if (e->iterate_previous)
82 e->iterate_previous->iterate_next = e->iterate_next;
83 else
84 h->iterate_list_head = e->iterate_next;
86 /* Remove from hash table bucket list */
87 if (e->bucket_next)
88 e->bucket_next->bucket_previous = e->bucket_previous;
90 if (e->bucket_previous)
91 e->bucket_previous->bucket_next = e->bucket_next;
92 else {
93 unsigned hash = h->hash_func(e->key) % NBUCKETS;
94 BY_HASH(h)[hash] = e->bucket_next;
97 if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
98 pa_xfree(e);
100 pa_assert(h->n_entries >= 1);
101 h->n_entries--;
104 void pa_hashmap_free(pa_hashmap*h, pa_free2_cb_t free_cb, void *userdata) {
105 pa_assert(h);
107 while (h->iterate_list_head) {
108 void *data;
109 data = h->iterate_list_head->value;
110 remove_entry(h, h->iterate_list_head);
112 if (free_cb)
113 free_cb(data, userdata);
116 pa_xfree(h);
119 static struct hashmap_entry *hash_scan(pa_hashmap *h, unsigned hash, const void *key) {
120 struct hashmap_entry *e;
121 pa_assert(h);
122 pa_assert(hash < NBUCKETS);
124 for (e = BY_HASH(h)[hash]; e; e = e->bucket_next)
125 if (h->compare_func(e->key, key) == 0)
126 return e;
128 return NULL;
131 int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
132 struct hashmap_entry *e;
133 unsigned hash;
135 pa_assert(h);
137 hash = h->hash_func(key) % NBUCKETS;
139 if (hash_scan(h, hash, key))
140 return -1;
142 if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
143 e = pa_xnew(struct hashmap_entry, 1);
145 e->key = key;
146 e->value = value;
148 /* Insert into hash table */
149 e->bucket_next = BY_HASH(h)[hash];
150 e->bucket_previous = NULL;
151 if (BY_HASH(h)[hash])
152 BY_HASH(h)[hash]->bucket_previous = e;
153 BY_HASH(h)[hash] = e;
155 /* Insert into iteration list */
156 e->iterate_previous = h->iterate_list_tail;
157 e->iterate_next = NULL;
158 if (h->iterate_list_tail) {
159 pa_assert(h->iterate_list_head);
160 h->iterate_list_tail->iterate_next = e;
161 } else {
162 pa_assert(!h->iterate_list_head);
163 h->iterate_list_head = e;
165 h->iterate_list_tail = e;
167 h->n_entries++;
168 pa_assert(h->n_entries >= 1);
170 return 0;
173 void* pa_hashmap_get(pa_hashmap *h, const void *key) {
174 unsigned hash;
175 struct hashmap_entry *e;
177 pa_assert(h);
179 hash = h->hash_func(key) % NBUCKETS;
181 if (!(e = hash_scan(h, hash, key)))
182 return NULL;
184 return e->value;
187 void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
188 struct hashmap_entry *e;
189 unsigned hash;
190 void *data;
192 pa_assert(h);
194 hash = h->hash_func(key) % NBUCKETS;
196 if (!(e = hash_scan(h, hash, key)))
197 return NULL;
199 data = e->value;
200 remove_entry(h, e);
202 return data;
205 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
206 struct hashmap_entry *e;
208 pa_assert(h);
209 pa_assert(state);
211 if (*state == (void*) -1)
212 goto at_end;
214 if (!*state && !h->iterate_list_head)
215 goto at_end;
217 e = *state ? *state : h->iterate_list_head;
219 if (e->iterate_next)
220 *state = e->iterate_next;
221 else
222 *state = (void*) -1;
224 if (key)
225 *key = e->key;
227 return e->value;
229 at_end:
230 *state = (void *) -1;
232 if (key)
233 *key = NULL;
235 return NULL;
238 void *pa_hashmap_iterate_backwards(pa_hashmap *h, void **state, const void **key) {
239 struct hashmap_entry *e;
241 pa_assert(h);
242 pa_assert(state);
244 if (*state == (void*) -1)
245 goto at_beginning;
247 if (!*state && !h->iterate_list_tail)
248 goto at_beginning;
250 e = *state ? *state : h->iterate_list_tail;
252 if (e->iterate_previous)
253 *state = e->iterate_previous;
254 else
255 *state = (void*) -1;
257 if (key)
258 *key = e->key;
260 return e->value;
262 at_beginning:
263 *state = (void *) -1;
265 if (key)
266 *key = NULL;
268 return NULL;
271 void* pa_hashmap_first(pa_hashmap *h) {
272 pa_assert(h);
274 if (!h->iterate_list_head)
275 return NULL;
277 return h->iterate_list_head->value;
280 void* pa_hashmap_last(pa_hashmap *h) {
281 pa_assert(h);
283 if (!h->iterate_list_tail)
284 return NULL;
286 return h->iterate_list_tail->value;
289 void* pa_hashmap_steal_first(pa_hashmap *h) {
290 void *data;
292 pa_assert(h);
294 if (!h->iterate_list_head)
295 return NULL;
297 data = h->iterate_list_head->value;
298 remove_entry(h, h->iterate_list_head);
300 return data;
303 unsigned pa_hashmap_size(pa_hashmap *h) {
304 pa_assert(h);
306 return h->n_entries;
309 pa_bool_t pa_hashmap_isempty(pa_hashmap *h) {
310 pa_assert(h);
312 return h->n_entries == 0;