2 This file is part of PulseAudio.
4 Copyright 2004-2008 Lennart Poettering
5 Copyright 2006 Pierre Ossman <ossman@cendio.se> for Cendio AB
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as
9 published by the Free Software Foundation; either version 2.1 of the
10 License, or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public
18 License along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <pulse/xmalloc.h>
32 #include <pulsecore/log.h>
33 #include <pulsecore/flist.h>
34 #include <pulsecore/macro.h>
44 struct idxset_entry *data_next, *data_previous;
45 struct idxset_entry *index_next, *index_previous;
46 struct idxset_entry *iterate_next, *iterate_previous;
50 pa_hash_func_t hash_func;
51 pa_compare_func_t compare_func;
53 uint32_t current_index;
55 struct idxset_entry *iterate_list_head, *iterate_list_tail;
59 #define BY_DATA(i) ((struct idxset_entry**) ((uint8_t*) (i) + PA_ALIGN(sizeof(pa_idxset))))
60 #define BY_INDEX(i) (BY_DATA(i) + NBUCKETS)
62 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
64 unsigned pa_idxset_string_hash_func(const void *p) {
69 hash = 31 * hash + (unsigned) *c;
74 int pa_idxset_string_compare_func(const void *a, const void *b) {
78 unsigned pa_idxset_trivial_hash_func(const void *p) {
79 return PA_PTR_TO_UINT(p);
82 int pa_idxset_trivial_compare_func(const void *a, const void *b) {
83 return a < b ? -1 : (a > b ? 1 : 0);
86 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
89 s = pa_xmalloc0(PA_ALIGN(sizeof(pa_idxset)) + NBUCKETS*2*sizeof(struct idxset_entry*));
91 s->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
92 s->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
96 s->iterate_list_head = s->iterate_list_tail = NULL;
101 static void remove_entry(pa_idxset *s, struct idxset_entry *e) {
105 /* Remove from iteration linked list */
107 e->iterate_next->iterate_previous = e->iterate_previous;
109 s->iterate_list_tail = e->iterate_previous;
111 if (e->iterate_previous)
112 e->iterate_previous->iterate_next = e->iterate_next;
114 s->iterate_list_head = e->iterate_next;
116 /* Remove from data hash table */
118 e->data_next->data_previous = e->data_previous;
120 if (e->data_previous)
121 e->data_previous->data_next = e->data_next;
123 unsigned hash = s->hash_func(e->data) % NBUCKETS;
124 BY_DATA(s)[hash] = e->data_next;
127 /* Remove from index hash table */
129 e->index_next->index_previous = e->index_previous;
131 if (e->index_previous)
132 e->index_previous->index_next = e->index_next;
134 BY_INDEX(s)[e->idx % NBUCKETS] = e->index_next;
136 if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
139 pa_assert(s->n_entries >= 1);
143 void pa_idxset_free(pa_idxset *s, pa_free2_cb_t free_cb, void *userdata) {
146 while (s->iterate_list_head) {
147 void *data = s->iterate_list_head->data;
149 remove_entry(s, s->iterate_list_head);
152 free_cb(data, userdata);
158 static struct idxset_entry* data_scan(pa_idxset *s, unsigned hash, const void *p) {
159 struct idxset_entry *e;
161 pa_assert(hash < NBUCKETS);
164 for (e = BY_DATA(s)[hash]; e; e = e->data_next)
165 if (s->compare_func(e->data, p) == 0)
171 static struct idxset_entry* index_scan(pa_idxset *s, unsigned hash, uint32_t idx) {
172 struct idxset_entry *e;
174 pa_assert(hash < NBUCKETS);
176 for (e = BY_INDEX(s)[hash]; e; e = e->index_next)
183 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx) {
185 struct idxset_entry *e;
189 hash = s->hash_func(p) % NBUCKETS;
191 if ((e = data_scan(s, hash, p))) {
198 if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
199 e = pa_xnew(struct idxset_entry, 1);
202 e->idx = s->current_index++;
204 /* Insert into data hash table */
205 e->data_next = BY_DATA(s)[hash];
206 e->data_previous = NULL;
207 if (BY_DATA(s)[hash])
208 BY_DATA(s)[hash]->data_previous = e;
209 BY_DATA(s)[hash] = e;
211 hash = e->idx % NBUCKETS;
213 /* Insert into index hash table */
214 e->index_next = BY_INDEX(s)[hash];
215 e->index_previous = NULL;
216 if (BY_INDEX(s)[hash])
217 BY_INDEX(s)[hash]->index_previous = e;
218 BY_INDEX(s)[hash] = e;
220 /* Insert into iteration list */
221 e->iterate_previous = s->iterate_list_tail;
222 e->iterate_next = NULL;
223 if (s->iterate_list_tail) {
224 pa_assert(s->iterate_list_head);
225 s->iterate_list_tail->iterate_next = e;
227 pa_assert(!s->iterate_list_head);
228 s->iterate_list_head = e;
230 s->iterate_list_tail = e;
233 pa_assert(s->n_entries >= 1);
241 void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx) {
243 struct idxset_entry *e;
247 hash = idx % NBUCKETS;
249 if (!(e = index_scan(s, hash, idx)))
255 void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx) {
257 struct idxset_entry *e;
261 hash = s->hash_func(p) % NBUCKETS;
263 if (!(e = data_scan(s, hash, p)))
272 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx) {
273 struct idxset_entry *e;
279 hash = idx % NBUCKETS;
281 if (!(e = index_scan(s, hash, idx)))
290 void* pa_idxset_remove_by_data(pa_idxset*s, const void *data, uint32_t *idx) {
291 struct idxset_entry *e;
297 hash = s->hash_func(data) % NBUCKETS;
299 if (!(e = data_scan(s, hash, data)))
312 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx) {
314 struct idxset_entry *e;
319 hash = *idx % NBUCKETS;
321 e = index_scan(s, hash, *idx);
323 if (e && e->iterate_next)
326 e = s->iterate_list_head;
335 void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx) {
336 struct idxset_entry *e;
341 if (*state == (void*) -1)
344 if ((!*state && !s->iterate_list_head))
347 e = *state ? *state : s->iterate_list_head;
350 *state = e->iterate_next;
360 *state = (void *) -1;
363 *idx = PA_IDXSET_INVALID;
368 void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx) {
373 if (!s->iterate_list_head)
376 data = s->iterate_list_head->data;
379 *idx = s->iterate_list_head->idx;
381 remove_entry(s, s->iterate_list_head);
386 void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
389 if (!s->iterate_list_head) {
391 *idx = PA_IDXSET_INVALID;
396 *idx = s->iterate_list_head->idx;
398 return s->iterate_list_head->data;
401 void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
402 struct idxset_entry *e;
408 if (*idx == PA_IDXSET_INVALID)
411 hash = *idx % NBUCKETS;
413 if ((e = index_scan(s, hash, *idx))) {
421 *idx = PA_IDXSET_INVALID;
427 /* If the entry passed doesn't exist anymore we try to find
428 * the next following */
430 for ((*idx)++; *idx < s->current_index; (*idx)++) {
432 hash = *idx % NBUCKETS;
434 if ((e = index_scan(s, hash, *idx))) {
440 *idx = PA_IDXSET_INVALID;
445 unsigned pa_idxset_size(pa_idxset*s) {
451 pa_bool_t pa_idxset_isempty(pa_idxset *s) {
454 return s->n_entries == 0;
457 pa_idxset *pa_idxset_copy(pa_idxset *s) {
459 struct idxset_entry *i;
463 copy = pa_idxset_new(s->hash_func, s->compare_func);
465 for (i = s->iterate_list_head; i; i = i->iterate_next)
466 pa_idxset_put(copy, i->data, NULL);