1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
33 struct hashmap_entry {
36 struct hashmap_entry *bucket_next, *bucket_previous;
37 struct hashmap_entry *iterate_next, *iterate_previous;
41 hash_func_t hash_func;
42 compare_func_t compare_func;
44 struct hashmap_entry *iterate_list_head, *iterate_list_tail;
48 #define BY_HASH(h) ((struct hashmap_entry**) ((uint8_t*) (h) + ALIGN(sizeof(Hashmap))))
50 unsigned string_hash_func(const void *p) {
55 hash = 31 * hash + (unsigned) *c;
60 int string_compare_func(const void *a, const void *b) {
64 unsigned trivial_hash_func(const void *p) {
65 return PTR_TO_UINT(p);
68 int trivial_compare_func(const void *a, const void *b) {
69 return a < b ? -1 : (a > b ? 1 : 0);
72 Hashmap *hashmap_new(hash_func_t hash_func, compare_func_t compare_func) {
75 if (!(h = malloc0(ALIGN(sizeof(Hashmap)) + NBUCKETS * ALIGN(sizeof(struct hashmap_entry*)))))
78 h->hash_func = hash_func ? hash_func : trivial_hash_func;
79 h->compare_func = compare_func ? compare_func : trivial_compare_func;
82 h->iterate_list_head = h->iterate_list_tail = NULL;
87 int hashmap_ensure_allocated(Hashmap **h, hash_func_t hash_func, compare_func_t compare_func) {
93 if (!(*h = hashmap_new(hash_func, compare_func)))
99 static void remove_entry(Hashmap *h, struct hashmap_entry *e) {
103 /* Remove from iteration list */
105 e->iterate_next->iterate_previous = e->iterate_previous;
107 h->iterate_list_tail = e->iterate_previous;
109 if (e->iterate_previous)
110 e->iterate_previous->iterate_next = e->iterate_next;
112 h->iterate_list_head = e->iterate_next;
114 /* Remove from hash table bucket list */
116 e->bucket_next->bucket_previous = e->bucket_previous;
118 if (e->bucket_previous)
119 e->bucket_previous->bucket_next = e->bucket_next;
121 unsigned hash = h->hash_func(e->key) % NBUCKETS;
122 BY_HASH(h)[hash] = e->bucket_next;
127 assert(h->n_entries >= 1);
131 void hashmap_free(Hashmap*h) {
141 void hashmap_clear(Hashmap *h) {
145 while (h->iterate_list_head)
146 remove_entry(h, h->iterate_list_head);
149 static struct hashmap_entry *hash_scan(Hashmap *h, unsigned hash, const void *key) {
150 struct hashmap_entry *e;
152 assert(hash < NBUCKETS);
154 for (e = BY_HASH(h)[hash]; e; e = e->bucket_next)
155 if (h->compare_func(e->key, key) == 0)
161 int hashmap_put(Hashmap *h, const void *key, void *value) {
162 struct hashmap_entry *e;
167 hash = h->hash_func(key) % NBUCKETS;
169 if ((e = hash_scan(h, hash, key))) {
171 if (e->value == value)
177 if (!(e = new(struct hashmap_entry, 1)))
183 /* Insert into hash table */
184 e->bucket_next = BY_HASH(h)[hash];
185 e->bucket_previous = NULL;
186 if (BY_HASH(h)[hash])
187 BY_HASH(h)[hash]->bucket_previous = e;
188 BY_HASH(h)[hash] = e;
190 /* Insert into iteration list */
191 e->iterate_previous = h->iterate_list_tail;
192 e->iterate_next = NULL;
193 if (h->iterate_list_tail) {
194 assert(h->iterate_list_head);
195 h->iterate_list_tail->iterate_next = e;
197 assert(!h->iterate_list_head);
198 h->iterate_list_head = e;
200 h->iterate_list_tail = e;
203 assert(h->n_entries >= 1);
208 int hashmap_replace(Hashmap *h, const void *key, void *value) {
209 struct hashmap_entry *e;
214 hash = h->hash_func(key) % NBUCKETS;
216 if ((e = hash_scan(h, hash, key))) {
221 return hashmap_put(h, key, value);
224 void* hashmap_get(Hashmap *h, const void *key) {
226 struct hashmap_entry *e;
231 hash = h->hash_func(key) % NBUCKETS;
233 if (!(e = hash_scan(h, hash, key)))
239 void* hashmap_remove(Hashmap *h, const void *key) {
240 struct hashmap_entry *e;
247 hash = h->hash_func(key) % NBUCKETS;
249 if (!(e = hash_scan(h, hash, key)))
258 void* hashmap_remove_value(Hashmap *h, const void *key, void *value) {
259 struct hashmap_entry *e;
265 hash = h->hash_func(key) % NBUCKETS;
267 if (!(e = hash_scan(h, hash, key)))
270 if (e->value != value)
278 void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key) {
279 struct hashmap_entry *e;
286 if (*i == ITERATOR_LAST)
289 if (*i == ITERATOR_FIRST && !h->iterate_list_head)
292 e = *i == ITERATOR_FIRST ? h->iterate_list_head : (struct hashmap_entry*) *i;
295 *i = (Iterator) e->iterate_next;
313 void *hashmap_iterate_backwards(Hashmap *h, Iterator *i, const void **key) {
314 struct hashmap_entry *e;
321 if (*i == ITERATOR_FIRST)
324 if (*i == ITERATOR_LAST && !h->iterate_list_tail)
327 e = *i == ITERATOR_LAST ? h->iterate_list_tail : (struct hashmap_entry*) *i;
329 if (e->iterate_previous)
330 *i = (Iterator) e->iterate_previous;
348 void *hashmap_iterate_skip(Hashmap *h, const void *key, Iterator *i) {
350 struct hashmap_entry *e;
355 hash = h->hash_func(key) % NBUCKETS;
357 if (!(e = hash_scan(h, hash, key)))
365 void* hashmap_first(Hashmap *h) {
370 if (!h->iterate_list_head)
373 return h->iterate_list_head->value;
376 void* hashmap_last(Hashmap *h) {
381 if (!h->iterate_list_tail)
384 return h->iterate_list_tail->value;
387 void* hashmap_steal_first(Hashmap *h) {
393 if (!h->iterate_list_head)
396 data = h->iterate_list_head->value;
397 remove_entry(h, h->iterate_list_head);
402 unsigned hashmap_size(Hashmap *h) {
410 bool hashmap_isempty(Hashmap *h) {
415 return h->n_entries == 0;
418 int hashmap_merge(Hashmap *h, Hashmap *other) {
419 struct hashmap_entry *e;
426 for (e = other->iterate_list_head; e; e = e->iterate_next) {
429 if ((r = hashmap_put(h, e->key, e->value)) < 0)
437 Hashmap *hashmap_copy(Hashmap *h) {
442 if (!(copy = hashmap_new(h->hash_func, h->compare_func)))
445 if (hashmap_merge(copy, h) < 0) {