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, see <http://www.gnu.org/licenses/>.
29 #include <pulse/xmalloc.h>
30 #include <pulsecore/flist.h>
31 #include <pulsecore/macro.h>
41 struct idxset_entry *data_next, *data_previous;
42 struct idxset_entry *index_next, *index_previous;
43 struct idxset_entry *iterate_next, *iterate_previous;
47 pa_hash_func_t hash_func;
48 pa_compare_func_t compare_func;
50 uint32_t current_index;
52 struct idxset_entry *iterate_list_head, *iterate_list_tail;
56 #define BY_DATA(i) ((struct idxset_entry**) ((uint8_t*) (i) + PA_ALIGN(sizeof(pa_idxset))))
57 #define BY_INDEX(i) (BY_DATA(i) + NBUCKETS)
59 PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
61 unsigned pa_idxset_string_hash_func(const void *p) {
66 hash = 31 * hash + (unsigned) *c;
71 int pa_idxset_string_compare_func(const void *a, const void *b) {
75 unsigned pa_idxset_trivial_hash_func(const void *p) {
76 return PA_PTR_TO_UINT(p);
79 int pa_idxset_trivial_compare_func(const void *a, const void *b) {
80 return a < b ? -1 : (a > b ? 1 : 0);
83 pa_idxset* pa_idxset_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
86 s = pa_xmalloc0(PA_ALIGN(sizeof(pa_idxset)) + NBUCKETS*2*sizeof(struct idxset_entry*));
88 s->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
89 s->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
93 s->iterate_list_head = s->iterate_list_tail = NULL;
98 static void remove_entry(pa_idxset *s, struct idxset_entry *e) {
102 /* Remove from iteration linked list */
104 e->iterate_next->iterate_previous = e->iterate_previous;
106 s->iterate_list_tail = e->iterate_previous;
108 if (e->iterate_previous)
109 e->iterate_previous->iterate_next = e->iterate_next;
111 s->iterate_list_head = e->iterate_next;
113 /* Remove from data hash table */
115 e->data_next->data_previous = e->data_previous;
117 if (e->data_previous)
118 e->data_previous->data_next = e->data_next;
120 unsigned hash = s->hash_func(e->data) % NBUCKETS;
121 BY_DATA(s)[hash] = e->data_next;
124 /* Remove from index hash table */
126 e->index_next->index_previous = e->index_previous;
128 if (e->index_previous)
129 e->index_previous->index_next = e->index_next;
131 BY_INDEX(s)[e->idx % NBUCKETS] = e->index_next;
133 if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
136 pa_assert(s->n_entries >= 1);
140 void pa_idxset_free(pa_idxset *s, pa_free_cb_t free_cb) {
143 pa_idxset_remove_all(s, free_cb);
147 static struct idxset_entry* data_scan(pa_idxset *s, unsigned hash, const void *p) {
148 struct idxset_entry *e;
150 pa_assert(hash < NBUCKETS);
153 for (e = BY_DATA(s)[hash]; e; e = e->data_next)
154 if (s->compare_func(e->data, p) == 0)
160 static struct idxset_entry* index_scan(pa_idxset *s, unsigned hash, uint32_t idx) {
161 struct idxset_entry *e;
163 pa_assert(hash < NBUCKETS);
165 for (e = BY_INDEX(s)[hash]; e; e = e->index_next)
172 int pa_idxset_put(pa_idxset*s, void *p, uint32_t *idx) {
174 struct idxset_entry *e;
178 hash = s->hash_func(p) % NBUCKETS;
180 if ((e = data_scan(s, hash, p))) {
187 if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
188 e = pa_xnew(struct idxset_entry, 1);
191 e->idx = s->current_index++;
193 /* Insert into data hash table */
194 e->data_next = BY_DATA(s)[hash];
195 e->data_previous = NULL;
196 if (BY_DATA(s)[hash])
197 BY_DATA(s)[hash]->data_previous = e;
198 BY_DATA(s)[hash] = e;
200 hash = e->idx % NBUCKETS;
202 /* Insert into index hash table */
203 e->index_next = BY_INDEX(s)[hash];
204 e->index_previous = NULL;
205 if (BY_INDEX(s)[hash])
206 BY_INDEX(s)[hash]->index_previous = e;
207 BY_INDEX(s)[hash] = e;
209 /* Insert into iteration list */
210 e->iterate_previous = s->iterate_list_tail;
211 e->iterate_next = NULL;
212 if (s->iterate_list_tail) {
213 pa_assert(s->iterate_list_head);
214 s->iterate_list_tail->iterate_next = e;
216 pa_assert(!s->iterate_list_head);
217 s->iterate_list_head = e;
219 s->iterate_list_tail = e;
222 pa_assert(s->n_entries >= 1);
230 void* pa_idxset_get_by_index(pa_idxset*s, uint32_t idx) {
232 struct idxset_entry *e;
236 hash = idx % NBUCKETS;
238 if (!(e = index_scan(s, hash, idx)))
244 void* pa_idxset_get_by_data(pa_idxset*s, const void *p, uint32_t *idx) {
246 struct idxset_entry *e;
250 hash = s->hash_func(p) % NBUCKETS;
252 if (!(e = data_scan(s, hash, p)))
261 void* pa_idxset_remove_by_index(pa_idxset*s, uint32_t idx) {
262 struct idxset_entry *e;
268 hash = idx % NBUCKETS;
270 if (!(e = index_scan(s, hash, idx)))
279 void* pa_idxset_remove_by_data(pa_idxset*s, const void *data, uint32_t *idx) {
280 struct idxset_entry *e;
286 hash = s->hash_func(data) % NBUCKETS;
288 if (!(e = data_scan(s, hash, data)))
301 void pa_idxset_remove_all(pa_idxset *s, pa_free_cb_t free_cb) {
304 while (s->iterate_list_head) {
305 void *data = s->iterate_list_head->data;
307 remove_entry(s, s->iterate_list_head);
314 void* pa_idxset_rrobin(pa_idxset *s, uint32_t *idx) {
316 struct idxset_entry *e;
321 hash = *idx % NBUCKETS;
323 e = index_scan(s, hash, *idx);
325 if (e && e->iterate_next)
328 e = s->iterate_list_head;
337 void *pa_idxset_iterate(pa_idxset *s, void **state, uint32_t *idx) {
338 struct idxset_entry *e;
343 if (*state == (void*) -1)
346 if ((!*state && !s->iterate_list_head))
349 e = *state ? *state : s->iterate_list_head;
352 *state = e->iterate_next;
362 *state = (void *) -1;
365 *idx = PA_IDXSET_INVALID;
370 void* pa_idxset_steal_first(pa_idxset *s, uint32_t *idx) {
375 if (!s->iterate_list_head)
378 data = s->iterate_list_head->data;
381 *idx = s->iterate_list_head->idx;
383 remove_entry(s, s->iterate_list_head);
388 void* pa_idxset_first(pa_idxset *s, uint32_t *idx) {
391 if (!s->iterate_list_head) {
393 *idx = PA_IDXSET_INVALID;
398 *idx = s->iterate_list_head->idx;
400 return s->iterate_list_head->data;
403 void *pa_idxset_next(pa_idxset *s, uint32_t *idx) {
404 struct idxset_entry *e;
410 if (*idx == PA_IDXSET_INVALID)
413 hash = *idx % NBUCKETS;
415 if ((e = index_scan(s, hash, *idx))) {
423 *idx = PA_IDXSET_INVALID;
429 /* If the entry passed doesn't exist anymore we try to find
430 * the next following */
432 for ((*idx)++; *idx < s->current_index; (*idx)++) {
434 hash = *idx % NBUCKETS;
436 if ((e = index_scan(s, hash, *idx))) {
442 *idx = PA_IDXSET_INVALID;
447 unsigned pa_idxset_size(pa_idxset*s) {
453 bool pa_idxset_isempty(pa_idxset *s) {
456 return s->n_entries == 0;
459 pa_idxset *pa_idxset_copy(pa_idxset *s, pa_copy_func_t copy_func) {
461 struct idxset_entry *i;
465 copy = pa_idxset_new(s->hash_func, s->compare_func);
467 for (i = s->iterate_list_head; i; i = i->iterate_next)
468 pa_idxset_put(copy, copy_func ? copy_func(i->data) : i->data, NULL);
474 pa_idxset *pa_idxset_filtered_copy(pa_idxset *s, pa_copy_func_t copy_func, pa_compare_func_t filter_func, const void *filter) {
476 struct idxset_entry *i;
480 copy = pa_idxset_new(s->hash_func, s->compare_func);
482 for (i = s->iterate_list_head; i; i = i->iterate_next) {
484 if (filter_func(i->data, filter))
485 pa_idxset_put(copy, copy_func ? copy_func(i->data) : i->data, NULL);
487 pa_idxset_put(copy, copy_func ? copy_func(i->data) : i->data, NULL);