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 OrderedHashmap OrderedHashmap;
38 typedef struct _IteratorStruct _IteratorStruct;
39 typedef _IteratorStruct* Iterator;
41 #define ITERATOR_FIRST ((Iterator) 0)
42 #define ITERATOR_LAST ((Iterator) -1)
44 typedef unsigned long (*hash_func_t)(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]);
45 typedef int (*compare_func_t)(const void *a, const void *b);
49 compare_func_t compare;
52 unsigned long string_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
53 int string_compare_func(const void *a, const void *b) _pure_;
54 extern const struct hash_ops string_hash_ops;
56 /* This will compare the passed pointers directly, and will not
57 * dereference them. This is hence not useful for strings or
59 unsigned long trivial_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
60 int trivial_compare_func(const void *a, const void *b) _const_;
61 extern const struct hash_ops trivial_hash_ops;
63 /* 32bit values we can always just embedd in the pointer itself, but
64 * in order to support 32bit archs we need store 64bit values
65 * indirectly, since they don't fit in a pointer. */
66 unsigned long uint64_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
67 int uint64_compare_func(const void *a, const void *b) _pure_;
68 extern const struct hash_ops uint64_hash_ops;
70 /* On some archs dev_t is 32bit, and on others 64bit. And sometimes
71 * it's 64bit on 32bit archs, and sometimes 32bit on 64bit archs. Yuck! */
73 unsigned long devt_hash_func(const void *p, const uint8_t hash_key[HASH_KEY_SIZE]) _pure_;
74 int devt_compare_func(const void *a, const void *b) _pure_;
75 extern const struct hash_ops devt_hash_ops = {
76 .hash = devt_hash_func,
77 .compare = devt_compare_func
80 #define devt_hash_func uint64_hash_func
81 #define devt_compare_func uint64_compare_func
82 #define devt_hash_ops uint64_hash_ops
85 Hashmap *hashmap_new(const struct hash_ops *hash_ops);
86 static inline OrderedHashmap *ordered_hashmap_new(const struct hash_ops *hash_ops) {
87 return (OrderedHashmap*) hashmap_new(hash_ops);
89 void hashmap_free(Hashmap *h);
90 static inline void ordered_hashmap_free(OrderedHashmap *h) {
91 hashmap_free((Hashmap*) h);
93 void hashmap_free_free(Hashmap *h);
94 static inline void ordered_hashmap_free_free(OrderedHashmap *h) {
95 hashmap_free_free((Hashmap*) h);
97 void hashmap_free_free_free(Hashmap *h);
98 static inline void ordered_hashmap_free_free_free(OrderedHashmap *h) {
99 hashmap_free_free_free((Hashmap*) h);
101 Hashmap *hashmap_copy(Hashmap *h);
102 static inline OrderedHashmap *ordered_hashmap_copy(OrderedHashmap *h) {
103 return (OrderedHashmap*) hashmap_copy((Hashmap*) h);
105 int hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops);
106 static inline int ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops) {
107 return hashmap_ensure_allocated((Hashmap**) h, hash_ops);
110 int hashmap_put(Hashmap *h, const void *key, void *value);
111 static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void *value) {
112 return hashmap_put((Hashmap*) h, key, value);
114 int hashmap_update(Hashmap *h, const void *key, void *value);
115 static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) {
116 return hashmap_update((Hashmap*) h, key, value);
118 int hashmap_replace(Hashmap *h, const void *key, void *value);
119 static inline int ordered_hashmap_replace(OrderedHashmap *h, const void *key, void *value) {
120 return hashmap_replace((Hashmap*) h, key, value);
122 void *hashmap_get(Hashmap *h, const void *key);
123 static inline void *ordered_hashmap_get(OrderedHashmap *h, const void *key) {
124 return hashmap_get((Hashmap*) h, key);
126 void *hashmap_get2(Hashmap *h, const void *key, void **rkey);
127 static inline void *ordered_hashmap_get2(OrderedHashmap *h, const void *key, void **rkey) {
128 return hashmap_get2((Hashmap*) h, key, rkey);
130 bool hashmap_contains(Hashmap *h, const void *key);
131 static inline bool ordered_hashmap_contains(OrderedHashmap *h, const void *key) {
132 return hashmap_contains((Hashmap*) h, key);
134 void *hashmap_remove(Hashmap *h, const void *key);
135 static inline void *ordered_hashmap_remove(OrderedHashmap *h, const void *key) {
136 return hashmap_remove((Hashmap*) h, key);
138 void *hashmap_remove2(Hashmap *h, const void *key, void **rkey);
139 static inline void *ordered_hashmap_remove2(OrderedHashmap *h, const void *key, void **rkey) {
140 return hashmap_remove2((Hashmap*) h, key, rkey);
142 void *hashmap_remove_value(Hashmap *h, const void *key, void *value);
143 static inline void *ordered_hashmap_remove_value(OrderedHashmap *h, const void *key, void *value) {
144 return hashmap_remove_value((Hashmap*) h, key, value);
146 int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
147 static inline int ordered_hashmap_remove_and_put(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
148 return hashmap_remove_and_put((Hashmap*) h, old_key, new_key, value);
150 int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
151 static inline int ordered_hashmap_remove_and_replace(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
152 return hashmap_remove_and_replace((Hashmap*) h, old_key, new_key, value);
155 int hashmap_merge(Hashmap *h, Hashmap *other);
156 static inline int ordered_hashmap_merge(OrderedHashmap *h, OrderedHashmap *other) {
157 return hashmap_merge((Hashmap*) h, (Hashmap*) other);
159 int hashmap_reserve(Hashmap *h, unsigned entries_add);
160 static inline int ordered_hashmap_reserve(OrderedHashmap *h, unsigned entries_add) {
161 return hashmap_reserve((Hashmap*) h, entries_add);
163 int hashmap_move(Hashmap *h, Hashmap *other);
164 static inline int ordered_hashmap_move(OrderedHashmap *h, OrderedHashmap *other) {
165 return hashmap_move((Hashmap*) h, (Hashmap*) other);
167 int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key);
168 static inline int ordered_hashmap_move_one(OrderedHashmap *h, OrderedHashmap *other, const void *key) {
169 return hashmap_move_one((Hashmap*) h, (Hashmap*) other, key);
172 unsigned hashmap_size(Hashmap *h) _pure_;
173 static inline unsigned ordered_hashmap_size(OrderedHashmap *h) {
174 return hashmap_size((Hashmap*) h);
176 bool hashmap_isempty(Hashmap *h) _pure_;
177 static inline bool ordered_hashmap_isempty(OrderedHashmap *h) {
178 return hashmap_isempty((Hashmap*) h);
180 unsigned hashmap_buckets(Hashmap *h) _pure_;
181 static inline unsigned ordered_hashmap_buckets(OrderedHashmap *h) {
182 return hashmap_buckets((Hashmap*) h);
185 void *hashmap_iterate(Hashmap *h, Iterator *i, const void **key);
186 static inline void *ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, const void **key) {
187 return hashmap_iterate((Hashmap*) h, i, key);
190 void hashmap_clear(Hashmap *h);
191 static inline void ordered_hashmap_clear(OrderedHashmap *h) {
192 hashmap_clear((Hashmap*) h);
194 void hashmap_clear_free(Hashmap *h);
195 static inline void ordered_hashmap_clear_free(OrderedHashmap *h) {
196 hashmap_clear_free((Hashmap*) h);
198 void hashmap_clear_free_free(Hashmap *h);
199 static inline void ordered_hashmap_clear_free_free(OrderedHashmap *h) {
200 hashmap_clear_free_free((Hashmap*) h);
203 void *hashmap_steal_first(Hashmap *h);
204 static inline void *ordered_hashmap_steal_first(OrderedHashmap *h) {
205 return hashmap_steal_first((Hashmap*) h);
207 void *hashmap_steal_first_key(Hashmap *h);
208 static inline void *ordered_hashmap_steal_first_key(OrderedHashmap *h) {
209 return hashmap_steal_first_key((Hashmap*) h);
211 void *hashmap_first(Hashmap *h) _pure_;
212 static inline void *ordered_hashmap_first(OrderedHashmap *h) {
213 return hashmap_first((Hashmap*) h);
215 void *hashmap_first_key(Hashmap *h) _pure_;
216 static inline void *ordered_hashmap_first_key(OrderedHashmap *h) {
217 return hashmap_first_key((Hashmap*) h);
220 void *hashmap_next(Hashmap *h, const void *key);
221 static inline void *ordered_hashmap_next(OrderedHashmap *h, const void *key) {
222 return hashmap_next((Hashmap*) h, key);
225 char **hashmap_get_strv(Hashmap *h);
226 static inline char **ordered_hashmap_get_strv(OrderedHashmap *h) {
227 return hashmap_get_strv((Hashmap*) h);
230 #define HASHMAP_FOREACH(e, h, i) \
231 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), NULL); (e); (e) = hashmap_iterate((h), &(i), NULL))
233 #define ORDERED_HASHMAP_FOREACH(e, h, i) \
234 for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), NULL); \
236 (e) = ordered_hashmap_iterate((h), &(i), NULL))
238 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
239 for ((i) = ITERATOR_FIRST, (e) = hashmap_iterate((h), &(i), (const void**) &(k)); (e); (e) = hashmap_iterate((h), &(i), (const void**) &(k)))
241 #define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
242 for ((i) = ITERATOR_FIRST, (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)); \
244 (e) = ordered_hashmap_iterate((h), &(i), (const void**) &(k)))
246 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
247 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
248 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free);
249 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free);
250 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free);
251 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free_free);
252 #define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
253 #define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
254 #define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)
255 #define _cleanup_ordered_hashmap_free_ _cleanup_(ordered_hashmap_freep)
256 #define _cleanup_ordered_hashmap_free_free_ _cleanup_(ordered_hashmap_free_freep)
257 #define _cleanup_ordered_hashmap_free_free_free_ _cleanup_(ordered_hashmap_free_free_freep)