2 This file is part of PulseAudio.
4 Copyright 2004-2006 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 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>
31 #include <pulsecore/idxset.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/flist.h>
34 #include <pulsecore/macro.h>
40 struct hashmap_entry
{
41 struct hashmap_entry
*next
, *previous
, *bucket_next
, *bucket_previous
;
49 struct hashmap_entry
**data
;
50 struct hashmap_entry
*first_entry
;
53 pa_hash_func_t hash_func
;
54 pa_compare_func_t compare_func
;
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_xnew(pa_hashmap
, 1);
63 h
->data
= pa_xnew0(struct hashmap_entry
*, h
->size
= BUCKETS
);
64 h
->first_entry
= NULL
;
66 h
->hash_func
= hash_func
? hash_func
: pa_idxset_trivial_hash_func
;
67 h
->compare_func
= compare_func
? compare_func
: pa_idxset_trivial_compare_func
;
72 static void remove_entry(pa_hashmap
*h
, struct hashmap_entry
*e
) {
77 e
->next
->previous
= e
->previous
;
79 e
->previous
->next
= e
->next
;
81 h
->first_entry
= e
->next
;
84 e
->bucket_next
->bucket_previous
= e
->bucket_previous
;
85 if (e
->bucket_previous
)
86 e
->bucket_previous
->bucket_next
= e
->bucket_next
;
88 pa_assert(e
->hash
< h
->size
);
89 h
->data
[e
->hash
] = e
->bucket_next
;
92 if (pa_flist_push(PA_STATIC_FLIST_GET(entries
), e
) < 0)
98 void pa_hashmap_free(pa_hashmap
*h
, void (*free_func
)(void *p
, void *userdata
), void *userdata
) {
101 while (h
->first_entry
) {
103 free_func(h
->first_entry
->value
, userdata
);
104 remove_entry(h
, h
->first_entry
);
111 static struct hashmap_entry
*get(pa_hashmap
*h
, unsigned hash
, const void *key
) {
112 struct hashmap_entry
*e
;
114 pa_assert(hash
< h
->size
);
116 for (e
= h
->data
[hash
]; e
; e
= e
->bucket_next
)
117 if (h
->compare_func(e
->key
, key
) == 0)
123 int pa_hashmap_put(pa_hashmap
*h
, const void *key
, void *value
) {
124 struct hashmap_entry
*e
;
128 hash
= h
->hash_func(key
) % h
->size
;
130 if ((e
= get(h
, hash
, key
)))
133 if (!(e
= pa_flist_pop(PA_STATIC_FLIST_GET(entries
))))
134 e
= pa_xnew(struct hashmap_entry
, 1);
141 e
->next
= h
->first_entry
;
143 h
->first_entry
->previous
= e
;
146 e
->bucket_previous
= NULL
;
147 e
->bucket_next
= h
->data
[hash
];
149 h
->data
[hash
]->bucket_previous
= e
;
156 void* pa_hashmap_get(pa_hashmap
*h
, const void *key
) {
158 struct hashmap_entry
*e
;
162 hash
= h
->hash_func(key
) % h
->size
;
164 if (!(e
= get(h
, hash
, key
)))
170 void* pa_hashmap_remove(pa_hashmap
*h
, const void *key
) {
171 struct hashmap_entry
*e
;
177 hash
= h
->hash_func(key
) % h
->size
;
179 if (!(e
= get(h
, hash
, key
)))
187 unsigned pa_hashmap_size(pa_hashmap
*h
) {
191 void *pa_hashmap_iterate(pa_hashmap
*h
, void **state
, const void **key
) {
192 struct hashmap_entry
*e
;
197 if (*state
== (void*) -1)
200 if ((!*state
&& !h
->first_entry
))
203 e
= *state
? *state
: h
->first_entry
;
216 *state
= (void *) -1;
224 void* pa_hashmap_steal_first(pa_hashmap
*h
) {
232 data
= h
->first_entry
->value
;
233 remove_entry(h
, h
->first_entry
);
237 void *pa_hashmap_get_first(pa_hashmap
*h
) {
243 return h
->first_entry
->value
;