2004-07-16 19:56:36 +00:00
|
|
|
/***
|
2006-06-19 21:53:48 +00:00
|
|
|
This file is part of PulseAudio.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
Copyright 2004-2008 Lennart Poettering
|
2007-02-13 15:35:19 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is free software; you can redistribute it and/or modify
|
2004-11-14 14:58:54 +00:00
|
|
|
it under the terms of the GNU Lesser General Public License as published
|
2004-07-16 19:56:36 +00:00
|
|
|
by the Free Software Foundation; either version 2 of the License,
|
|
|
|
|
or (at your option) any later version.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
PulseAudio is distributed in the hope that it will be useful, but
|
2004-07-16 19:56:36 +00:00
|
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
|
General Public License for more details.
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-11-14 14:58:54 +00:00
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
2006-06-19 21:53:48 +00:00
|
|
|
along with PulseAudio; if not, write to the Free Software
|
2004-07-16 19:56:36 +00:00
|
|
|
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
|
|
|
|
|
USA.
|
|
|
|
|
***/
|
|
|
|
|
|
2004-07-16 19:16:42 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-06-19 21:53:48 +00:00
|
|
|
#include <pulse/xmalloc.h>
|
|
|
|
|
#include <pulsecore/idxset.h>
|
|
|
|
|
#include <pulsecore/log.h>
|
2007-10-28 19:13:50 +00:00
|
|
|
#include <pulsecore/flist.h>
|
|
|
|
|
#include <pulsecore/macro.h>
|
2006-02-17 12:10:58 +00:00
|
|
|
|
2004-07-11 16:59:22 +00:00
|
|
|
#include "hashmap.h"
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
#define NBUCKETS 127
|
2004-11-21 02:43:05 +00:00
|
|
|
|
2004-07-11 16:59:22 +00:00
|
|
|
struct hashmap_entry {
|
2004-06-27 22:42:17 +00:00
|
|
|
const void *key;
|
|
|
|
|
void *value;
|
2008-06-27 20:12:24 +02:00
|
|
|
|
|
|
|
|
struct hashmap_entry *bucket_next, *bucket_previous;
|
|
|
|
|
struct hashmap_entry *iterate_next, *iterate_previous;
|
2004-06-27 22:42:17 +00:00
|
|
|
};
|
|
|
|
|
|
2004-07-11 16:59:22 +00:00
|
|
|
struct pa_hashmap {
|
2006-08-18 19:43:46 +00:00
|
|
|
pa_hash_func_t hash_func;
|
|
|
|
|
pa_compare_func_t compare_func;
|
2008-06-27 20:12:24 +02:00
|
|
|
|
|
|
|
|
struct hashmap_entry *iterate_list_head, *iterate_list_tail;
|
|
|
|
|
unsigned n_entries;
|
2004-06-27 22:42:17 +00:00
|
|
|
};
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
#define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + PA_ALIGN(sizeof(pa_hashmap))))
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
PA_STATIC_FLIST_DECLARE(entries, 0, pa_xfree);
|
|
|
|
|
|
2006-08-18 19:43:46 +00:00
|
|
|
pa_hashmap *pa_hashmap_new(pa_hash_func_t hash_func, pa_compare_func_t compare_func) {
|
2006-01-11 01:17:39 +00:00
|
|
|
pa_hashmap *h;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
h = pa_xmalloc0(PA_ALIGN(sizeof(pa_hashmap)) + NBUCKETS*sizeof(struct hashmap_entry*));
|
|
|
|
|
|
2004-07-03 23:35:12 +00:00
|
|
|
h->hash_func = hash_func ? hash_func : pa_idxset_trivial_hash_func;
|
|
|
|
|
h->compare_func = compare_func ? compare_func : pa_idxset_trivial_compare_func;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
h->n_entries = 0;
|
|
|
|
|
h->iterate_list_head = h->iterate_list_tail = NULL;
|
|
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
return h;
|
|
|
|
|
}
|
|
|
|
|
|
2008-03-31 22:11:06 +00:00
|
|
|
static void remove_entry(pa_hashmap *h, struct hashmap_entry *e) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
|
|
|
|
pa_assert(e);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
/* Remove from iteration list */
|
|
|
|
|
if (e->iterate_next)
|
|
|
|
|
e->iterate_next->iterate_previous = e->iterate_previous;
|
2004-06-27 22:42:17 +00:00
|
|
|
else
|
2008-06-27 20:12:24 +02:00
|
|
|
h->iterate_list_tail = e->iterate_previous;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (e->iterate_previous)
|
|
|
|
|
e->iterate_previous->iterate_next = e->iterate_next;
|
|
|
|
|
else
|
|
|
|
|
h->iterate_list_head = e->iterate_next;
|
|
|
|
|
|
|
|
|
|
/* Remove from hash table bucket list */
|
2004-06-27 22:42:17 +00:00
|
|
|
if (e->bucket_next)
|
|
|
|
|
e->bucket_next->bucket_previous = e->bucket_previous;
|
2008-06-27 20:12:24 +02:00
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
if (e->bucket_previous)
|
|
|
|
|
e->bucket_previous->bucket_next = e->bucket_next;
|
2004-11-21 02:43:05 +00:00
|
|
|
else {
|
2008-06-27 20:12:24 +02:00
|
|
|
unsigned hash = h->hash_func(e->key) % NBUCKETS;
|
|
|
|
|
BY_HASH(h)[hash] = e->bucket_next;
|
2004-11-21 02:43:05 +00:00
|
|
|
}
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (pa_flist_push(PA_STATIC_FLIST_GET(entries), e) < 0)
|
|
|
|
|
pa_xfree(e);
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
pa_assert(h->n_entries >= 1);
|
2004-06-27 22:42:17 +00:00
|
|
|
h->n_entries--;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
void pa_hashmap_free(pa_hashmap*h, pa_free2_cb_t free_cb, void *userdata) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
while (h->iterate_list_head) {
|
|
|
|
|
void *data;
|
|
|
|
|
data = h->iterate_list_head->value;
|
|
|
|
|
remove_entry(h, h->iterate_list_head);
|
|
|
|
|
|
|
|
|
|
if (free_cb)
|
|
|
|
|
free_cb(data, userdata);
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-08-04 16:39:30 +00:00
|
|
|
pa_xfree(h);
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
static struct hashmap_entry *hash_scan(pa_hashmap *h, unsigned hash, const void *key) {
|
2004-07-11 16:59:22 +00:00
|
|
|
struct hashmap_entry *e;
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2008-06-27 20:12:24 +02:00
|
|
|
pa_assert(hash < NBUCKETS);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
for (e = BY_HASH(h)[hash]; e; e = e->bucket_next)
|
2004-06-27 22:42:17 +00:00
|
|
|
if (h->compare_func(e->key, key) == 0)
|
|
|
|
|
return e;
|
|
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
int pa_hashmap_put(pa_hashmap *h, const void *key, void *value) {
|
2004-07-11 16:59:22 +00:00
|
|
|
struct hashmap_entry *e;
|
2004-06-27 22:42:17 +00:00
|
|
|
unsigned hash;
|
2008-06-27 20:12:24 +02:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
hash = h->hash_func(key) % NBUCKETS;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if ((e = hash_scan(h, hash, key)))
|
2004-06-27 22:42:17 +00:00
|
|
|
return -1;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
if (!(e = pa_flist_pop(PA_STATIC_FLIST_GET(entries))))
|
|
|
|
|
e = pa_xnew(struct hashmap_entry, 1);
|
|
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
e->key = key;
|
|
|
|
|
e->value = value;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
/* Insert into hash table */
|
|
|
|
|
e->bucket_next = BY_HASH(h)[hash];
|
2004-06-27 22:42:17 +00:00
|
|
|
e->bucket_previous = NULL;
|
2008-06-27 20:12:24 +02:00
|
|
|
if (BY_HASH(h)[hash])
|
|
|
|
|
BY_HASH(h)[hash]->bucket_previous = e;
|
|
|
|
|
BY_HASH(h)[hash] = e;
|
|
|
|
|
|
|
|
|
|
/* Insert into iteration list */
|
|
|
|
|
e->iterate_previous = h->iterate_list_tail;
|
|
|
|
|
e->iterate_next = NULL;
|
|
|
|
|
if (h->iterate_list_tail) {
|
|
|
|
|
pa_assert(h->iterate_list_head);
|
|
|
|
|
h->iterate_list_tail->iterate_next = e;
|
|
|
|
|
} else {
|
|
|
|
|
pa_assert(!h->iterate_list_head);
|
|
|
|
|
h->iterate_list_head = e;
|
|
|
|
|
}
|
|
|
|
|
h->iterate_list_tail = e;
|
|
|
|
|
|
|
|
|
|
h->n_entries++;
|
|
|
|
|
pa_assert(h->n_entries >= 1);
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2004-06-27 22:42:17 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void* pa_hashmap_get(pa_hashmap *h, const void *key) {
|
2004-06-27 22:42:17 +00:00
|
|
|
unsigned hash;
|
2004-07-11 16:59:22 +00:00
|
|
|
struct hashmap_entry *e;
|
2006-08-18 19:43:46 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
hash = h->hash_func(key) % NBUCKETS;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (!(e = hash_scan(h, hash, key)))
|
2004-06-27 22:42:17 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return e->value;
|
|
|
|
|
}
|
|
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void* pa_hashmap_remove(pa_hashmap *h, const void *key) {
|
2004-07-11 16:59:22 +00:00
|
|
|
struct hashmap_entry *e;
|
2004-06-27 22:42:17 +00:00
|
|
|
unsigned hash;
|
2004-10-30 01:55:16 +00:00
|
|
|
void *data;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
hash = h->hash_func(key) % NBUCKETS;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (!(e = hash_scan(h, hash, key)))
|
2004-10-30 01:55:16 +00:00
|
|
|
return NULL;
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2004-10-30 01:55:16 +00:00
|
|
|
data = e->value;
|
2008-03-31 22:11:06 +00:00
|
|
|
remove_entry(h, e);
|
2004-06-27 22:42:17 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
return data;
|
2004-06-27 22:42:17 +00:00
|
|
|
}
|
2004-07-20 01:07:06 +00:00
|
|
|
|
2006-01-11 01:17:39 +00:00
|
|
|
void *pa_hashmap_iterate(pa_hashmap *h, void **state, const void **key) {
|
2008-05-29 15:16:58 +00:00
|
|
|
struct hashmap_entry *e;
|
|
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
|
|
|
|
pa_assert(state);
|
2004-07-20 01:07:06 +00:00
|
|
|
|
2008-05-29 15:16:58 +00:00
|
|
|
if (*state == (void*) -1)
|
|
|
|
|
goto at_end;
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (!*state && !h->iterate_list_head)
|
2008-05-29 15:16:58 +00:00
|
|
|
goto at_end;
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
e = *state ? *state : h->iterate_list_head;
|
2008-05-29 15:16:58 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (e->iterate_next)
|
|
|
|
|
*state = e->iterate_next;
|
2004-11-21 02:43:05 +00:00
|
|
|
else
|
2008-05-29 15:16:58 +00:00
|
|
|
*state = (void*) -1;
|
2004-07-20 01:07:06 +00:00
|
|
|
|
2008-05-29 15:16:58 +00:00
|
|
|
if (key)
|
|
|
|
|
*key = e->key;
|
|
|
|
|
|
|
|
|
|
return e->value;
|
|
|
|
|
|
|
|
|
|
at_end:
|
|
|
|
|
*state = (void *) -1;
|
2004-10-30 01:55:16 +00:00
|
|
|
|
|
|
|
|
if (key)
|
2008-05-29 15:16:58 +00:00
|
|
|
*key = NULL;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2008-05-29 15:16:58 +00:00
|
|
|
return NULL;
|
2004-07-20 01:07:06 +00:00
|
|
|
}
|
2006-08-18 19:43:46 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
void* pa_hashmap_first(pa_hashmap *h) {
|
|
|
|
|
pa_assert(h);
|
|
|
|
|
|
|
|
|
|
if (!h->iterate_list_head)
|
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
|
|
return h->iterate_list_head->value;
|
|
|
|
|
}
|
|
|
|
|
|
2006-08-18 19:43:46 +00:00
|
|
|
void* pa_hashmap_steal_first(pa_hashmap *h) {
|
|
|
|
|
void *data;
|
2007-01-04 13:43:45 +00:00
|
|
|
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2006-08-18 19:43:46 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
if (!h->iterate_list_head)
|
2006-08-18 19:43:46 +00:00
|
|
|
return NULL;
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
data = h->iterate_list_head->value;
|
|
|
|
|
remove_entry(h, h->iterate_list_head);
|
|
|
|
|
|
2006-08-18 19:43:46 +00:00
|
|
|
return data;
|
|
|
|
|
}
|
|
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
unsigned pa_hashmap_size(pa_hashmap *h) {
|
2007-10-28 19:13:50 +00:00
|
|
|
pa_assert(h);
|
2006-08-18 19:43:46 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
return h->n_entries;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pa_bool_t pa_hashmap_isempty(pa_hashmap *h) {
|
|
|
|
|
pa_assert(h);
|
2006-08-18 19:43:46 +00:00
|
|
|
|
2008-06-27 20:12:24 +02:00
|
|
|
return h->n_entries == 0;
|
2006-08-18 19:43:46 +00:00
|
|
|
}
|