]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/hashmap.h
blockdev-util: add correct API for detecting if block device has partition scanning...
[thirdparty/systemd.git] / src / basic / hashmap.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 #include <limits.h>
5 #include <stdbool.h>
6 #include <stddef.h>
7
8 #include "hash-funcs.h"
9 #include "macro.h"
10 #include "util.h"
11
12 /*
13 * A hash table implementation. As a minor optimization a NULL hashmap object
14 * will be treated as empty hashmap for all read operations. That way it is not
15 * necessary to instantiate an object for each Hashmap use.
16 *
17 * If ENABLE_DEBUG_HASHMAP is defined (by configuring with -Ddebug-extra=hashmap),
18 * the implementation will:
19 * - store extra data for debugging and statistics (see tools/gdb-sd_dump_hashmaps.py)
20 * - perform extra checks for invalid use of iterators
21 */
22
23 #define HASH_KEY_SIZE 16
24
25 typedef void* (*hashmap_destroy_t)(void *p);
26
27 /* The base type for all hashmap and set types. Many functions in the implementation take (HashmapBase*)
28 * parameters and are run-time polymorphic, though the API is not meant to be polymorphic (do not call
29 * underscore-prefixed functions directly). */
30 typedef struct HashmapBase HashmapBase;
31
32 /* Specific hashmap/set types */
33 typedef struct Hashmap Hashmap; /* Maps keys to values */
34 typedef struct OrderedHashmap OrderedHashmap; /* Like Hashmap, but also remembers entry insertion order */
35 typedef struct Set Set; /* Stores just keys */
36
37 typedef struct IteratedCache IteratedCache; /* Caches the iterated order of one of the above */
38
39 /* Ideally the Iterator would be an opaque struct, but it is instantiated
40 * by hashmap users, so the definition has to be here. Do not use its fields
41 * directly. */
42 typedef struct {
43 unsigned idx; /* index of an entry to be iterated next */
44 const void *next_key; /* expected value of that entry's key pointer */
45 #if ENABLE_DEBUG_HASHMAP
46 unsigned put_count; /* hashmap's put_count recorded at start of iteration */
47 unsigned rem_count; /* hashmap's rem_count in previous iteration */
48 unsigned prev_idx; /* idx in previous iteration */
49 #endif
50 } Iterator;
51
52 #define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
53 #define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
54
55 /* Macros for type checking */
56 #define PTR_COMPATIBLE_WITH_HASHMAP_BASE(h) \
57 (__builtin_types_compatible_p(typeof(h), HashmapBase*) || \
58 __builtin_types_compatible_p(typeof(h), Hashmap*) || \
59 __builtin_types_compatible_p(typeof(h), OrderedHashmap*) || \
60 __builtin_types_compatible_p(typeof(h), Set*))
61
62 #define PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h) \
63 (__builtin_types_compatible_p(typeof(h), Hashmap*) || \
64 __builtin_types_compatible_p(typeof(h), OrderedHashmap*)) \
65
66 #define HASHMAP_BASE(h) \
67 __builtin_choose_expr(PTR_COMPATIBLE_WITH_HASHMAP_BASE(h), \
68 (HashmapBase*)(h), \
69 (void)0)
70
71 #define PLAIN_HASHMAP(h) \
72 __builtin_choose_expr(PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h), \
73 (Hashmap*)(h), \
74 (void)0)
75
76 #if ENABLE_DEBUG_HASHMAP
77 # define HASHMAP_DEBUG_PARAMS , const char *func, const char *file, int line
78 # define HASHMAP_DEBUG_SRC_ARGS , __func__, PROJECT_FILE, __LINE__
79 # define HASHMAP_DEBUG_PASS_ARGS , func, file, line
80 #else
81 # define HASHMAP_DEBUG_PARAMS
82 # define HASHMAP_DEBUG_SRC_ARGS
83 # define HASHMAP_DEBUG_PASS_ARGS
84 #endif
85
86 Hashmap *_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
87 OrderedHashmap *_ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
88 #define hashmap_new(ops) _hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
89 #define ordered_hashmap_new(ops) _ordered_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
90
91 #define hashmap_free_and_replace(a, b) \
92 ({ \
93 hashmap_free(a); \
94 (a) = (b); \
95 (b) = NULL; \
96 0; \
97 })
98
99 HashmapBase *_hashmap_free(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value);
100 static inline Hashmap *hashmap_free(Hashmap *h) {
101 return (void*) _hashmap_free(HASHMAP_BASE(h), NULL, NULL);
102 }
103 static inline OrderedHashmap *ordered_hashmap_free(OrderedHashmap *h) {
104 return (void*) _hashmap_free(HASHMAP_BASE(h), NULL, NULL);
105 }
106
107 static inline Hashmap *hashmap_free_free(Hashmap *h) {
108 return (void*) _hashmap_free(HASHMAP_BASE(h), NULL, free);
109 }
110 static inline OrderedHashmap *ordered_hashmap_free_free(OrderedHashmap *h) {
111 return (void*) _hashmap_free(HASHMAP_BASE(h), NULL, free);
112 }
113
114 static inline Hashmap *hashmap_free_free_key(Hashmap *h) {
115 return (void*) _hashmap_free(HASHMAP_BASE(h), free, NULL);
116 }
117 static inline OrderedHashmap *ordered_hashmap_free_free_key(OrderedHashmap *h) {
118 return (void*) _hashmap_free(HASHMAP_BASE(h), free, NULL);
119 }
120
121 static inline Hashmap *hashmap_free_free_free(Hashmap *h) {
122 return (void*) _hashmap_free(HASHMAP_BASE(h), free, free);
123 }
124 static inline OrderedHashmap *ordered_hashmap_free_free_free(OrderedHashmap *h) {
125 return (void*) _hashmap_free(HASHMAP_BASE(h), free, free);
126 }
127
128 IteratedCache *iterated_cache_free(IteratedCache *cache);
129 int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries);
130
131 HashmapBase *_hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS);
132 #define hashmap_copy(h) ((Hashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
133 #define ordered_hashmap_copy(h) ((OrderedHashmap*) _hashmap_copy(HASHMAP_BASE(h) HASHMAP_DEBUG_SRC_ARGS))
134
135 int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
136 int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS);
137 #define hashmap_ensure_allocated(h, ops) _hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
138 #define ordered_hashmap_ensure_allocated(h, ops) _ordered_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
139
140 IteratedCache *_hashmap_iterated_cache_new(HashmapBase *h);
141 static inline IteratedCache *hashmap_iterated_cache_new(Hashmap *h) {
142 return (IteratedCache*) _hashmap_iterated_cache_new(HASHMAP_BASE(h));
143 }
144 static inline IteratedCache *ordered_hashmap_iterated_cache_new(OrderedHashmap *h) {
145 return (IteratedCache*) _hashmap_iterated_cache_new(HASHMAP_BASE(h));
146 }
147
148 int hashmap_put(Hashmap *h, const void *key, void *value);
149 static inline int ordered_hashmap_put(OrderedHashmap *h, const void *key, void *value) {
150 return hashmap_put(PLAIN_HASHMAP(h), key, value);
151 }
152
153 int _hashmap_put_strdup(Hashmap **h, const char *k, const char *v HASHMAP_DEBUG_PARAMS);
154 #define hashmap_put_strdup(h, k, v) _hashmap_put_strdup(h, k, v HASHMAP_DEBUG_SRC_ARGS)
155
156 int hashmap_update(Hashmap *h, const void *key, void *value);
157 static inline int ordered_hashmap_update(OrderedHashmap *h, const void *key, void *value) {
158 return hashmap_update(PLAIN_HASHMAP(h), key, value);
159 }
160
161 int hashmap_replace(Hashmap *h, const void *key, void *value);
162 static inline int ordered_hashmap_replace(OrderedHashmap *h, const void *key, void *value) {
163 return hashmap_replace(PLAIN_HASHMAP(h), key, value);
164 }
165
166 void *_hashmap_get(HashmapBase *h, const void *key);
167 static inline void *hashmap_get(Hashmap *h, const void *key) {
168 return _hashmap_get(HASHMAP_BASE(h), key);
169 }
170 static inline void *ordered_hashmap_get(OrderedHashmap *h, const void *key) {
171 return _hashmap_get(HASHMAP_BASE(h), key);
172 }
173
174 void *hashmap_get2(Hashmap *h, const void *key, void **rkey);
175 static inline void *ordered_hashmap_get2(OrderedHashmap *h, const void *key, void **rkey) {
176 return hashmap_get2(PLAIN_HASHMAP(h), key, rkey);
177 }
178
179 bool _hashmap_contains(HashmapBase *h, const void *key);
180 static inline bool hashmap_contains(Hashmap *h, const void *key) {
181 return _hashmap_contains(HASHMAP_BASE(h), key);
182 }
183 static inline bool ordered_hashmap_contains(OrderedHashmap *h, const void *key) {
184 return _hashmap_contains(HASHMAP_BASE(h), key);
185 }
186
187 void *_hashmap_remove(HashmapBase *h, const void *key);
188 static inline void *hashmap_remove(Hashmap *h, const void *key) {
189 return _hashmap_remove(HASHMAP_BASE(h), key);
190 }
191 static inline void *ordered_hashmap_remove(OrderedHashmap *h, const void *key) {
192 return _hashmap_remove(HASHMAP_BASE(h), key);
193 }
194
195 void *hashmap_remove2(Hashmap *h, const void *key, void **rkey);
196 static inline void *ordered_hashmap_remove2(OrderedHashmap *h, const void *key, void **rkey) {
197 return hashmap_remove2(PLAIN_HASHMAP(h), key, rkey);
198 }
199
200 void *_hashmap_remove_value(HashmapBase *h, const void *key, void *value);
201 static inline void *hashmap_remove_value(Hashmap *h, const void *key, void *value) {
202 return _hashmap_remove_value(HASHMAP_BASE(h), key, value);
203 }
204
205 static inline void *ordered_hashmap_remove_value(OrderedHashmap *h, const void *key, void *value) {
206 return hashmap_remove_value(PLAIN_HASHMAP(h), key, value);
207 }
208
209 int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value);
210 static inline int ordered_hashmap_remove_and_put(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
211 return hashmap_remove_and_put(PLAIN_HASHMAP(h), old_key, new_key, value);
212 }
213
214 int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value);
215 static inline int ordered_hashmap_remove_and_replace(OrderedHashmap *h, const void *old_key, const void *new_key, void *value) {
216 return hashmap_remove_and_replace(PLAIN_HASHMAP(h), old_key, new_key, value);
217 }
218
219 /* Since merging data from a OrderedHashmap into a Hashmap or vice-versa
220 * should just work, allow this by having looser type-checking here. */
221 int _hashmap_merge(Hashmap *h, Hashmap *other);
222 #define hashmap_merge(h, other) _hashmap_merge(PLAIN_HASHMAP(h), PLAIN_HASHMAP(other))
223 #define ordered_hashmap_merge(h, other) hashmap_merge(h, other)
224
225 int _hashmap_reserve(HashmapBase *h, unsigned entries_add);
226 static inline int hashmap_reserve(Hashmap *h, unsigned entries_add) {
227 return _hashmap_reserve(HASHMAP_BASE(h), entries_add);
228 }
229 static inline int ordered_hashmap_reserve(OrderedHashmap *h, unsigned entries_add) {
230 return _hashmap_reserve(HASHMAP_BASE(h), entries_add);
231 }
232
233 int _hashmap_move(HashmapBase *h, HashmapBase *other);
234 /* Unlike hashmap_merge, hashmap_move does not allow mixing the types. */
235 static inline int hashmap_move(Hashmap *h, Hashmap *other) {
236 return _hashmap_move(HASHMAP_BASE(h), HASHMAP_BASE(other));
237 }
238 static inline int ordered_hashmap_move(OrderedHashmap *h, OrderedHashmap *other) {
239 return _hashmap_move(HASHMAP_BASE(h), HASHMAP_BASE(other));
240 }
241
242 int _hashmap_move_one(HashmapBase *h, HashmapBase *other, const void *key);
243 static inline int hashmap_move_one(Hashmap *h, Hashmap *other, const void *key) {
244 return _hashmap_move_one(HASHMAP_BASE(h), HASHMAP_BASE(other), key);
245 }
246 static inline int ordered_hashmap_move_one(OrderedHashmap *h, OrderedHashmap *other, const void *key) {
247 return _hashmap_move_one(HASHMAP_BASE(h), HASHMAP_BASE(other), key);
248 }
249
250 unsigned _hashmap_size(HashmapBase *h) _pure_;
251 static inline unsigned hashmap_size(Hashmap *h) {
252 return _hashmap_size(HASHMAP_BASE(h));
253 }
254 static inline unsigned ordered_hashmap_size(OrderedHashmap *h) {
255 return _hashmap_size(HASHMAP_BASE(h));
256 }
257
258 static inline bool hashmap_isempty(Hashmap *h) {
259 return hashmap_size(h) == 0;
260 }
261 static inline bool ordered_hashmap_isempty(OrderedHashmap *h) {
262 return ordered_hashmap_size(h) == 0;
263 }
264
265 unsigned _hashmap_buckets(HashmapBase *h) _pure_;
266 static inline unsigned hashmap_buckets(Hashmap *h) {
267 return _hashmap_buckets(HASHMAP_BASE(h));
268 }
269 static inline unsigned ordered_hashmap_buckets(OrderedHashmap *h) {
270 return _hashmap_buckets(HASHMAP_BASE(h));
271 }
272
273 bool _hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key);
274 static inline bool hashmap_iterate(Hashmap *h, Iterator *i, void **value, const void **key) {
275 return _hashmap_iterate(HASHMAP_BASE(h), i, value, key);
276 }
277 static inline bool ordered_hashmap_iterate(OrderedHashmap *h, Iterator *i, void **value, const void **key) {
278 return _hashmap_iterate(HASHMAP_BASE(h), i, value, key);
279 }
280
281 void _hashmap_clear(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value);
282 static inline void hashmap_clear(Hashmap *h) {
283 _hashmap_clear(HASHMAP_BASE(h), NULL, NULL);
284 }
285 static inline void ordered_hashmap_clear(OrderedHashmap *h) {
286 _hashmap_clear(HASHMAP_BASE(h), NULL, NULL);
287 }
288
289 static inline void hashmap_clear_free(Hashmap *h) {
290 _hashmap_clear(HASHMAP_BASE(h), NULL, free);
291 }
292 static inline void ordered_hashmap_clear_free(OrderedHashmap *h) {
293 _hashmap_clear(HASHMAP_BASE(h), NULL, free);
294 }
295
296 static inline void hashmap_clear_free_key(Hashmap *h) {
297 _hashmap_clear(HASHMAP_BASE(h), free, NULL);
298 }
299 static inline void ordered_hashmap_clear_free_key(OrderedHashmap *h) {
300 _hashmap_clear(HASHMAP_BASE(h), free, NULL);
301 }
302
303 static inline void hashmap_clear_free_free(Hashmap *h) {
304 _hashmap_clear(HASHMAP_BASE(h), free, free);
305 }
306 static inline void ordered_hashmap_clear_free_free(OrderedHashmap *h) {
307 _hashmap_clear(HASHMAP_BASE(h), free, free);
308 }
309
310 /*
311 * Note about all *_first*() functions
312 *
313 * For plain Hashmaps and Sets the order of entries is undefined.
314 * The functions find whatever entry is first in the implementation
315 * internal order.
316 *
317 * Only for OrderedHashmaps the order is well defined and finding
318 * the first entry is O(1).
319 */
320
321 void *_hashmap_first_key_and_value(HashmapBase *h, bool remove, void **ret_key);
322 static inline void *hashmap_steal_first_key_and_value(Hashmap *h, void **ret) {
323 return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, ret);
324 }
325 static inline void *ordered_hashmap_steal_first_key_and_value(OrderedHashmap *h, void **ret) {
326 return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, ret);
327 }
328 static inline void *hashmap_first_key_and_value(Hashmap *h, void **ret) {
329 return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, ret);
330 }
331 static inline void *ordered_hashmap_first_key_and_value(OrderedHashmap *h, void **ret) {
332 return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, ret);
333 }
334
335 static inline void *hashmap_steal_first(Hashmap *h) {
336 return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, NULL);
337 }
338 static inline void *ordered_hashmap_steal_first(OrderedHashmap *h) {
339 return _hashmap_first_key_and_value(HASHMAP_BASE(h), true, NULL);
340 }
341 static inline void *hashmap_first(Hashmap *h) {
342 return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, NULL);
343 }
344 static inline void *ordered_hashmap_first(OrderedHashmap *h) {
345 return _hashmap_first_key_and_value(HASHMAP_BASE(h), false, NULL);
346 }
347
348 static inline void *_hashmap_first_key(HashmapBase *h, bool remove) {
349 void *key = NULL;
350
351 (void) _hashmap_first_key_and_value(HASHMAP_BASE(h), remove, &key);
352 return key;
353 }
354 static inline void *hashmap_steal_first_key(Hashmap *h) {
355 return _hashmap_first_key(HASHMAP_BASE(h), true);
356 }
357 static inline void *ordered_hashmap_steal_first_key(OrderedHashmap *h) {
358 return _hashmap_first_key(HASHMAP_BASE(h), true);
359 }
360 static inline void *hashmap_first_key(Hashmap *h) {
361 return _hashmap_first_key(HASHMAP_BASE(h), false);
362 }
363 static inline void *ordered_hashmap_first_key(OrderedHashmap *h) {
364 return _hashmap_first_key(HASHMAP_BASE(h), false);
365 }
366
367 #define hashmap_clear_with_destructor(_s, _f) \
368 ({ \
369 void *_item; \
370 while ((_item = hashmap_steal_first(_s))) \
371 _f(_item); \
372 })
373 #define hashmap_free_with_destructor(_s, _f) \
374 ({ \
375 hashmap_clear_with_destructor(_s, _f); \
376 hashmap_free(_s); \
377 })
378 #define ordered_hashmap_clear_with_destructor(_s, _f) \
379 ({ \
380 void *_item; \
381 while ((_item = ordered_hashmap_steal_first(_s))) \
382 _f(_item); \
383 })
384 #define ordered_hashmap_free_with_destructor(_s, _f) \
385 ({ \
386 ordered_hashmap_clear_with_destructor(_s, _f); \
387 ordered_hashmap_free(_s); \
388 })
389
390 /* no hashmap_next */
391 void *ordered_hashmap_next(OrderedHashmap *h, const void *key);
392
393 char **_hashmap_get_strv(HashmapBase *h);
394 static inline char **hashmap_get_strv(Hashmap *h) {
395 return _hashmap_get_strv(HASHMAP_BASE(h));
396 }
397 static inline char **ordered_hashmap_get_strv(OrderedHashmap *h) {
398 return _hashmap_get_strv(HASHMAP_BASE(h));
399 }
400
401 /*
402 * Hashmaps are iterated in unpredictable order.
403 * OrderedHashmaps are an exception to this. They are iterated in the order
404 * the entries were inserted.
405 * It is safe to remove the current entry.
406 */
407 #define HASHMAP_FOREACH(e, h, i) \
408 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), NULL); )
409
410 #define ORDERED_HASHMAP_FOREACH(e, h, i) \
411 for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), NULL); )
412
413 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
414 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
415
416 #define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
417 for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
418
419 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free);
420 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free);
421 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_key);
422 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, hashmap_free_free_free);
423 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free);
424 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free);
425 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free_key);
426 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap*, ordered_hashmap_free_free_free);
427
428 #define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
429 #define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
430 #define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)
431 #define _cleanup_ordered_hashmap_free_ _cleanup_(ordered_hashmap_freep)
432 #define _cleanup_ordered_hashmap_free_free_ _cleanup_(ordered_hashmap_free_freep)
433 #define _cleanup_ordered_hashmap_free_free_free_ _cleanup_(ordered_hashmap_free_free_freep)
434
435 DEFINE_TRIVIAL_CLEANUP_FUNC(IteratedCache*, iterated_cache_free);
436
437 #define _cleanup_iterated_cache_free_ _cleanup_(iterated_cache_freep)