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 link_entry(Hashmap
*h
, struct hashmap_entry
*e
, unsigned hash
) {
103 /* Insert into hash table */
104 e
->bucket_next
= BY_HASH(h
)[hash
];
105 e
->bucket_previous
= NULL
;
106 if (BY_HASH(h
)[hash
])
107 BY_HASH(h
)[hash
]->bucket_previous
= e
;
108 BY_HASH(h
)[hash
] = e
;
110 /* Insert into iteration list */
111 e
->iterate_previous
= h
->iterate_list_tail
;
112 e
->iterate_next
= NULL
;
113 if (h
->iterate_list_tail
) {
114 assert(h
->iterate_list_head
);
115 h
->iterate_list_tail
->iterate_next
= e
;
117 assert(!h
->iterate_list_head
);
118 h
->iterate_list_head
= e
;
120 h
->iterate_list_tail
= e
;
123 assert(h
->n_entries
>= 1);
126 static void unlink_entry(Hashmap
*h
, struct hashmap_entry
*e
, unsigned hash
) {
130 /* Remove from iteration list */
132 e
->iterate_next
->iterate_previous
= e
->iterate_previous
;
134 h
->iterate_list_tail
= e
->iterate_previous
;
136 if (e
->iterate_previous
)
137 e
->iterate_previous
->iterate_next
= e
->iterate_next
;
139 h
->iterate_list_head
= e
->iterate_next
;
141 /* Remove from hash table bucket list */
143 e
->bucket_next
->bucket_previous
= e
->bucket_previous
;
145 if (e
->bucket_previous
)
146 e
->bucket_previous
->bucket_next
= e
->bucket_next
;
148 BY_HASH(h
)[hash
] = e
->bucket_next
;
150 assert(h
->n_entries
>= 1);
154 static void remove_entry(Hashmap
*h
, struct hashmap_entry
*e
) {
160 hash
= h
->hash_func(e
->key
) % NBUCKETS
;
162 unlink_entry(h
, e
, hash
);
166 void hashmap_free(Hashmap
*h
) {
176 void hashmap_clear(Hashmap
*h
) {
180 while (h
->iterate_list_head
)
181 remove_entry(h
, h
->iterate_list_head
);
184 static struct hashmap_entry
*hash_scan(Hashmap
*h
, unsigned hash
, const void *key
) {
185 struct hashmap_entry
*e
;
187 assert(hash
< NBUCKETS
);
189 for (e
= BY_HASH(h
)[hash
]; e
; e
= e
->bucket_next
)
190 if (h
->compare_func(e
->key
, key
) == 0)
196 int hashmap_put(Hashmap
*h
, const void *key
, void *value
) {
197 struct hashmap_entry
*e
;
202 hash
= h
->hash_func(key
) % NBUCKETS
;
204 if ((e
= hash_scan(h
, hash
, key
))) {
206 if (e
->value
== value
)
212 if (!(e
= new(struct hashmap_entry
, 1)))
218 link_entry(h
, e
, hash
);
223 int hashmap_replace(Hashmap
*h
, const void *key
, void *value
) {
224 struct hashmap_entry
*e
;
229 hash
= h
->hash_func(key
) % NBUCKETS
;
231 if ((e
= hash_scan(h
, hash
, key
))) {
237 return hashmap_put(h
, key
, value
);
240 void* hashmap_get(Hashmap
*h
, const void *key
) {
242 struct hashmap_entry
*e
;
247 hash
= h
->hash_func(key
) % NBUCKETS
;
249 if (!(e
= hash_scan(h
, hash
, key
)))
255 void* hashmap_remove(Hashmap
*h
, const void *key
) {
256 struct hashmap_entry
*e
;
263 hash
= h
->hash_func(key
) % NBUCKETS
;
265 if (!(e
= hash_scan(h
, hash
, key
)))
274 int hashmap_remove_and_put(Hashmap
*h
, const void *old_key
, const void *new_key
, void *value
) {
275 struct hashmap_entry
*e
;
276 unsigned old_hash
, new_hash
;
281 old_hash
= h
->hash_func(old_key
) % NBUCKETS
;
282 if (!(e
= hash_scan(h
, old_hash
, old_key
)))
285 new_hash
= h
->hash_func(new_key
) % NBUCKETS
;
286 if (hash_scan(h
, new_hash
, new_key
))
289 unlink_entry(h
, e
, old_hash
);
294 link_entry(h
, e
, new_hash
);
299 void* hashmap_remove_value(Hashmap
*h
, const void *key
, void *value
) {
300 struct hashmap_entry
*e
;
306 hash
= h
->hash_func(key
) % NBUCKETS
;
308 if (!(e
= hash_scan(h
, hash
, key
)))
311 if (e
->value
!= value
)
319 void *hashmap_iterate(Hashmap
*h
, Iterator
*i
, const void **key
) {
320 struct hashmap_entry
*e
;
327 if (*i
== ITERATOR_LAST
)
330 if (*i
== ITERATOR_FIRST
&& !h
->iterate_list_head
)
333 e
= *i
== ITERATOR_FIRST
? h
->iterate_list_head
: (struct hashmap_entry
*) *i
;
336 *i
= (Iterator
) e
->iterate_next
;
354 void *hashmap_iterate_backwards(Hashmap
*h
, Iterator
*i
, const void **key
) {
355 struct hashmap_entry
*e
;
362 if (*i
== ITERATOR_FIRST
)
365 if (*i
== ITERATOR_LAST
&& !h
->iterate_list_tail
)
368 e
= *i
== ITERATOR_LAST
? h
->iterate_list_tail
: (struct hashmap_entry
*) *i
;
370 if (e
->iterate_previous
)
371 *i
= (Iterator
) e
->iterate_previous
;
389 void *hashmap_iterate_skip(Hashmap
*h
, const void *key
, Iterator
*i
) {
391 struct hashmap_entry
*e
;
396 hash
= h
->hash_func(key
) % NBUCKETS
;
398 if (!(e
= hash_scan(h
, hash
, key
)))
406 void* hashmap_first(Hashmap
*h
) {
411 if (!h
->iterate_list_head
)
414 return h
->iterate_list_head
->value
;
417 void* hashmap_last(Hashmap
*h
) {
422 if (!h
->iterate_list_tail
)
425 return h
->iterate_list_tail
->value
;
428 void* hashmap_steal_first(Hashmap
*h
) {
434 if (!h
->iterate_list_head
)
437 data
= h
->iterate_list_head
->value
;
438 remove_entry(h
, h
->iterate_list_head
);
443 unsigned hashmap_size(Hashmap
*h
) {
451 bool hashmap_isempty(Hashmap
*h
) {
456 return h
->n_entries
== 0;
459 int hashmap_merge(Hashmap
*h
, Hashmap
*other
) {
460 struct hashmap_entry
*e
;
467 for (e
= other
->iterate_list_head
; e
; e
= e
->iterate_next
) {
470 if ((r
= hashmap_put(h
, e
->key
, e
->value
)) < 0)
478 void hashmap_move(Hashmap
*h
, Hashmap
*other
) {
479 struct hashmap_entry
*e
, *n
;
483 /* The same as hashmap_merge(), but every new item from other
484 * is moved to h. This function is guaranteed to succeed. */
489 for (e
= other
->iterate_list_head
; e
; e
= n
) {
490 unsigned h_hash
, other_hash
;
494 h_hash
= h
->hash_func(e
->key
) % NBUCKETS
;
496 if (hash_scan(h
, h_hash
, e
->key
))
499 other_hash
= other
->hash_func(e
->key
) % NBUCKETS
;
501 unlink_entry(other
, e
, other_hash
);
502 link_entry(h
, e
, h_hash
);
506 int hashmap_move_one(Hashmap
*h
, Hashmap
*other
, const void *key
) {
507 unsigned h_hash
, other_hash
;
508 struct hashmap_entry
*e
;
515 h_hash
= h
->hash_func(key
) % NBUCKETS
;
516 if (hash_scan(h
, h_hash
, key
))
519 other_hash
= other
->hash_func(key
) % NBUCKETS
;
520 if (!(e
= hash_scan(other
, other_hash
, key
)))
523 unlink_entry(other
, e
, other_hash
);
524 link_entry(h
, e
, h_hash
);
529 Hashmap
*hashmap_copy(Hashmap
*h
) {
534 if (!(copy
= hashmap_new(h
->hash_func
, h
->compare_func
)))
537 if (hashmap_merge(copy
, h
) < 0) {