1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 /* Pretty straightforward hash table implementation. As a minor
30 * optimization a NULL hashmap object will be treated as empty hashmap
31 * for all read operations. That way it is not necessary to
32 * instantiate an object for each Hashmap use. */
34 #define HASH_KEY_SIZE 16
36 typedef struct Hashmap Hashmap;
37 typedef struct _IteratorStruct _IteratorStruct;
38 typedef _IteratorStruct* Iterator;
40 #define ITERATOR_FIRST ((Iterator) 0)
41 #define ITERATOR_LAST ((Iterator) -1)
43 typedef unsigned long (*hash_func_t)(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]);
44 typedef int (*compare_func_t)(const void *a, const void *b);
48 compare_func_t compare;
51 unsigned long string_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
52 int string_compare_func(const void *a, const void *b) _pure_;
53 extern const struct hash_ops string_hash_ops;
55 /* This will compare the passed pointers directly, and will not
56 * dereference them. This is hence not useful for strings or
58 unsigned long trivial_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
59 int trivial_compare_func(const void *a, const void *b) _const_;
60 extern const struct hash_ops trivial_hash_ops;
62 /* 32bit values we can always just embedd in the pointer itself, but
63 * in order to support 32bit archs we need store 64bit values
64 * indirectly, since they don't fit in a pointer. */
65 unsigned long uint64_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
66 int uint64_compare_func(const void *a, const void *b) _pure_;
67 extern const struct hash_ops uint64_hash_ops;
69 /* On some archs dev_t is 32bit, and on others 64bit. And sometimes
70 * it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */
72 unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
73 int devt_compare_func(const void *a, const void *b) _pure_;
74 extern const struct hash_ops devt_hash_ops = {
75 .hash = devt_hash_func,
76 .compare = devt_compare_func
79 #define devt_hash_func uint64_hash_func
80 #define devt_compare_func uint64_compare_func
81 #define devt_hash_ops uint64_hash_ops
84 Hashmap *hashmap_new(const struct hash_ops *hash_ops);
85 void hashmap_free(Hashmap *h);
86 void hashmap_free_free(Hashmap *h);
87 void hashmap_free_free_free(Hashmap *h);
88 Hashmap *hashmap_copy(Hashmap *h);
89 int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops);
91 int hashmap_put(Hashmap *h, const void *key, void *value);
92 int hashmap_update(Hashmap *h, const void *key, void *value);
93 int hashmap_replace(Hashmap *h, const void *key, void *value);
94 void *hashmap_get(Hashmap *h, const void *key);
95 void *hashmap_get2(Hashmap *h, const void *key, void **rkey);
96 bool hashmap_contains(Hashmap *h, const void *key);
97 void *hashmap_remove(Hashmap *h, const void *key);
98 void *hashmap_remove2(Hashmap *h, const void *key, void **rkey);
99 void *hashmap_remove_value(Hashmap *h, const void *key, void *value);
100 int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
101 int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
103 int hashmap_merge(Hashmap *h, Hashmap *other);
104 void hashmap_move(Hashmap *h, Hashmap *other);
105 int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
107 unsigned hashmap_size(Hashmap *h) _pure_;
108 bool hashmap_isempty(Hashmap *h) _pure_;
109 unsigned hashmap_buckets(Hashmap *h) _pure_;
111 void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
113 void hashmap_clear(Hashmap *h);
114 void hashmap_clear_free(Hashmap *h);
115 void hashmap_clear_free_free(Hashmap *h);
117 void *hashmap_steal_first(Hashmap *h);
118 void *hashmap_steal_first_key(Hashmap *h);
119 void *hashmap_first(Hashmap *h) _pure_;
120 void *hashmap_first_key(Hashmap *h) _pure_;
122 void *hashmap_next(Hashmap *h, const void *key);
124 char **hashmap_get_strv(Hashmap *h);
126 #define HASHMAP_FOREACH(e, h, i) \
127 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
129 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
130 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
132 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
133 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
134 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free);
135 #define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
136 #define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
137 #define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)