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
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/idxset.h>
31 #include <pulsecore/log.h>
32 #include <pulsecore/flist.h>
33 #include <pulsecore/macro.h>
39 struct hashmap_entry {
43 struct hashmap_entry *bucket_next, *bucket_previous;
44 struct hashmap_entry *iterate_next, *iterate_previous;
48 pa_hash_func_t hash_func;
49 pa_compare_func_t compare_func;
51 struct hashmap_entry *iterate_list_head, *iterate_list_tail;
55 #define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + PA_ALIGN(sizeof(pa_hashmap))))
57 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
59 pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
62 h = pa_xmalloc0(PA_ALIGN(sizeof(pa_hashmap)) + NBUCKETS*sizeof(struct hashmap_entry*));
64 h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
65 h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
68 h->iterate_list_head = h->iterate_list_tail = NULL;
73 static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
77 /* Remove from iteration list */
79 e->iterate_next->iterate_previous = e->iterate_previous;
81 h->iterate_list_tail = e->iterate_previous;
83 if (e->iterate_previous)
84 e->iterate_previous->iterate_next = e->iterate_next;
86 h->iterate_list_head = e->iterate_next;
88 /* Remove from hash table bucket list */
90 e->bucket_next->bucket_previous = e->bucket_previous;
92 if (e->bucket_previous)
93 e->bucket_previous->bucket_next = e->bucket_next;
95 unsigned hash = h->hash_func(e->key) % NBUCKETS;
96 BY_HASH(h)[hash] = e->bucket_next;
99 if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
102 pa_assert(h->n_entries >= 1);
106 void pa_hashmap_free(pa_hashmap*h, pa_free2_cb_t free_cb, void *userdata) {
109 while (h->iterate_list_head) {
111 data = h->iterate_list_head->value;
112 remove_entry(h, h->iterate_list_head);
115 free_cb(data, userdata);
121 static struct hashmap_entry *hash_scan(pa_hashmap *h, unsigned hash, const void *key) {
122 struct hashmap_entry *e;
124 pa_assert(hash < NBUCKETS);
126 for (e = BY_HASH(h)[hash]; e; e = e->bucket_next)
127 if (h->compare_func(e->key, key) == 0)
133 int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
134 struct hashmap_entry *e;
139 hash = h->hash_func(key) % NBUCKETS;
141 if (hash_scan(h, hash, key))
144 if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
145 e = pa_xnew(struct hashmap_entry, 1);
150 /* Insert into hash table */
151 e->bucket_next = BY_HASH(h)[hash];
152 e->bucket_previous = NULL;
153 if (BY_HASH(h)[hash])
154 BY_HASH(h)[hash]->bucket_previous = e;
155 BY_HASH(h)[hash] = e;
157 /* Insert into iteration list */
158 e->iterate_previous = h->iterate_list_tail;
159 e->iterate_next = NULL;
160 if (h->iterate_list_tail) {
161 pa_assert(h->iterate_list_head);
162 h->iterate_list_tail->iterate_next = e;
164 pa_assert(!h->iterate_list_head);
165 h->iterate_list_head = e;
167 h->iterate_list_tail = e;
170 pa_assert(h->n_entries >= 1);
175 void* pa_hashmap_get(pa_hashmap *h, const void *key) {
177 struct hashmap_entry *e;
181 hash = h->hash_func(key) % NBUCKETS;
183 if (!(e = hash_scan(h, hash, key)))
189 void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
190 struct hashmap_entry *e;
196 hash = h->hash_func(key) % NBUCKETS;
198 if (!(e = hash_scan(h, hash, key)))
207 void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
208 struct hashmap_entry *e;
213 if (*state == (void*) -1)
216 if (!*state && !h->iterate_list_head)
219 e = *state ? *state : h->iterate_list_head;
222 *state = e->iterate_next;
232 *state = (void *) -1;
240 void *pa_hashmap_iterate_backwards(pa_hashmap *h, void **state, const void **key) {
241 struct hashmap_entry *e;
246 if (*state == (void*) -1)
249 if (!*state && !h->iterate_list_tail)
252 e = *state ? *state : h->iterate_list_tail;
254 if (e->iterate_previous)
255 *state = e->iterate_previous;
265 *state = (void *) -1;
273 void* pa_hashmap_first(pa_hashmap *h) {
276 if (!h->iterate_list_head)
279 return h->iterate_list_head->value;
282 void* pa_hashmap_last(pa_hashmap *h) {
285 if (!h->iterate_list_tail)
288 return h->iterate_list_tail->value;
291 void* pa_hashmap_steal_first(pa_hashmap *h) {
296 if (!h->iterate_list_head)
299 data = h->iterate_list_head->value;
300 remove_entry(h, h->iterate_list_head);
305 unsigned pa_hashmap_size(pa_hashmap *h) {
311 pa_bool_t pa_hashmap_isempty(pa_hashmap *h) {
314 return h->n_entries == 0;