1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "hash-funcs.h"
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.
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
23 #define HASH_KEY_SIZE 16
25 typedef void* (*hashmap_destroy_t
)(void *p
);
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
;
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 */
37 typedef struct IteratedCache IteratedCache
; /* Caches the iterated order of one of the above */
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
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 */
52 #define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
53 #define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
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*))
62 #define PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h) \
63 (__builtin_types_compatible_p(typeof(h), Hashmap*) || \
64 __builtin_types_compatible_p(typeof(h), OrderedHashmap*)) \
66 #define HASHMAP_BASE(h) \
67 __builtin_choose_expr(PTR_COMPATIBLE_WITH_HASHMAP_BASE(h), \
71 #define PLAIN_HASHMAP(h) \
72 __builtin_choose_expr(PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h), \
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
81 # define HASHMAP_DEBUG_PARAMS
82 # define HASHMAP_DEBUG_SRC_ARGS
83 # define HASHMAP_DEBUG_PASS_ARGS
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)
91 #define hashmap_free_and_replace(a, b) \
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
);
103 static inline OrderedHashmap
*ordered_hashmap_free(OrderedHashmap
*h
) {
104 return (void*) _hashmap_free(HASHMAP_BASE(h
), NULL
, NULL
);
107 static inline Hashmap
*hashmap_free_free(Hashmap
*h
) {
108 return (void*) _hashmap_free(HASHMAP_BASE(h
), NULL
, free
);
110 static inline OrderedHashmap
*ordered_hashmap_free_free(OrderedHashmap
*h
) {
111 return (void*) _hashmap_free(HASHMAP_BASE(h
), NULL
, free
);
114 static inline Hashmap
*hashmap_free_free_key(Hashmap
*h
) {
115 return (void*) _hashmap_free(HASHMAP_BASE(h
), free
, NULL
);
117 static inline OrderedHashmap
*ordered_hashmap_free_free_key(OrderedHashmap
*h
) {
118 return (void*) _hashmap_free(HASHMAP_BASE(h
), free
, NULL
);
121 static inline Hashmap
*hashmap_free_free_free(Hashmap
*h
) {
122 return (void*) _hashmap_free(HASHMAP_BASE(h
), free
, free
);
124 static inline OrderedHashmap
*ordered_hashmap_free_free_free(OrderedHashmap
*h
) {
125 return (void*) _hashmap_free(HASHMAP_BASE(h
), free
, free
);
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
);
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))
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)
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
));
144 static inline IteratedCache
*ordered_hashmap_iterated_cache_new(OrderedHashmap
*h
) {
145 return (IteratedCache
*) _hashmap_iterated_cache_new(HASHMAP_BASE(h
));
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
);
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)
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
);
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
);
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
);
170 static inline void *ordered_hashmap_get(OrderedHashmap
*h
, const void *key
) {
171 return _hashmap_get(HASHMAP_BASE(h
), key
);
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
);
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
);
183 static inline bool ordered_hashmap_contains(OrderedHashmap
*h
, const void *key
) {
184 return _hashmap_contains(HASHMAP_BASE(h
), key
);
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
);
191 static inline void *ordered_hashmap_remove(OrderedHashmap
*h
, const void *key
) {
192 return _hashmap_remove(HASHMAP_BASE(h
), key
);
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
);
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
);
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
);
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
);
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
);
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)
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
);
229 static inline int ordered_hashmap_reserve(OrderedHashmap
*h
, unsigned entries_add
) {
230 return _hashmap_reserve(HASHMAP_BASE(h
), entries_add
);
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
));
238 static inline int ordered_hashmap_move(OrderedHashmap
*h
, OrderedHashmap
*other
) {
239 return _hashmap_move(HASHMAP_BASE(h
), HASHMAP_BASE(other
));
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
);
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
);
250 unsigned _hashmap_size(HashmapBase
*h
) _pure_
;
251 static inline unsigned hashmap_size(Hashmap
*h
) {
252 return _hashmap_size(HASHMAP_BASE(h
));
254 static inline unsigned ordered_hashmap_size(OrderedHashmap
*h
) {
255 return _hashmap_size(HASHMAP_BASE(h
));
258 static inline bool hashmap_isempty(Hashmap
*h
) {
259 return hashmap_size(h
) == 0;
261 static inline bool ordered_hashmap_isempty(OrderedHashmap
*h
) {
262 return ordered_hashmap_size(h
) == 0;
265 unsigned _hashmap_buckets(HashmapBase
*h
) _pure_
;
266 static inline unsigned hashmap_buckets(Hashmap
*h
) {
267 return _hashmap_buckets(HASHMAP_BASE(h
));
269 static inline unsigned ordered_hashmap_buckets(OrderedHashmap
*h
) {
270 return _hashmap_buckets(HASHMAP_BASE(h
));
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
);
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
);
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
);
285 static inline void ordered_hashmap_clear(OrderedHashmap
*h
) {
286 _hashmap_clear(HASHMAP_BASE(h
), NULL
, NULL
);
289 static inline void hashmap_clear_free(Hashmap
*h
) {
290 _hashmap_clear(HASHMAP_BASE(h
), NULL
, free
);
292 static inline void ordered_hashmap_clear_free(OrderedHashmap
*h
) {
293 _hashmap_clear(HASHMAP_BASE(h
), NULL
, free
);
296 static inline void hashmap_clear_free_key(Hashmap
*h
) {
297 _hashmap_clear(HASHMAP_BASE(h
), free
, NULL
);
299 static inline void ordered_hashmap_clear_free_key(OrderedHashmap
*h
) {
300 _hashmap_clear(HASHMAP_BASE(h
), free
, NULL
);
303 static inline void hashmap_clear_free_free(Hashmap
*h
) {
304 _hashmap_clear(HASHMAP_BASE(h
), free
, free
);
306 static inline void ordered_hashmap_clear_free_free(OrderedHashmap
*h
) {
307 _hashmap_clear(HASHMAP_BASE(h
), free
, free
);
311 * Note about all *_first*() functions
313 * For plain Hashmaps and Sets the order of entries is undefined.
314 * The functions find whatever entry is first in the implementation
317 * Only for OrderedHashmaps the order is well defined and finding
318 * the first entry is O(1).
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
);
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
);
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
);
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
);
335 static inline void *hashmap_steal_first(Hashmap
*h
) {
336 return _hashmap_first_key_and_value(HASHMAP_BASE(h
), true, NULL
);
338 static inline void *ordered_hashmap_steal_first(OrderedHashmap
*h
) {
339 return _hashmap_first_key_and_value(HASHMAP_BASE(h
), true, NULL
);
341 static inline void *hashmap_first(Hashmap
*h
) {
342 return _hashmap_first_key_and_value(HASHMAP_BASE(h
), false, NULL
);
344 static inline void *ordered_hashmap_first(OrderedHashmap
*h
) {
345 return _hashmap_first_key_and_value(HASHMAP_BASE(h
), false, NULL
);
348 static inline void *_hashmap_first_key(HashmapBase
*h
, bool remove
) {
351 (void) _hashmap_first_key_and_value(HASHMAP_BASE(h
), remove
, &key
);
354 static inline void *hashmap_steal_first_key(Hashmap
*h
) {
355 return _hashmap_first_key(HASHMAP_BASE(h
), true);
357 static inline void *ordered_hashmap_steal_first_key(OrderedHashmap
*h
) {
358 return _hashmap_first_key(HASHMAP_BASE(h
), true);
360 static inline void *hashmap_first_key(Hashmap
*h
) {
361 return _hashmap_first_key(HASHMAP_BASE(h
), false);
363 static inline void *ordered_hashmap_first_key(OrderedHashmap
*h
) {
364 return _hashmap_first_key(HASHMAP_BASE(h
), false);
367 #define hashmap_clear_with_destructor(_s, _f) \
370 while ((_item = hashmap_steal_first(_s))) \
373 #define hashmap_free_with_destructor(_s, _f) \
375 hashmap_clear_with_destructor(_s, _f); \
378 #define ordered_hashmap_clear_with_destructor(_s, _f) \
381 while ((_item = ordered_hashmap_steal_first(_s))) \
384 #define ordered_hashmap_free_with_destructor(_s, _f) \
386 ordered_hashmap_clear_with_destructor(_s, _f); \
387 ordered_hashmap_free(_s); \
390 /* no hashmap_next */
391 void *ordered_hashmap_next(OrderedHashmap
*h
, const void *key
);
393 char **_hashmap_get_strv(HashmapBase
*h
);
394 static inline char **hashmap_get_strv(Hashmap
*h
) {
395 return _hashmap_get_strv(HASHMAP_BASE(h
));
397 static inline char **ordered_hashmap_get_strv(OrderedHashmap
*h
) {
398 return _hashmap_get_strv(HASHMAP_BASE(h
));
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.
407 #define HASHMAP_FOREACH(e, h, i) \
408 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), NULL); )
410 #define ORDERED_HASHMAP_FOREACH(e, h, i) \
411 for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), NULL); )
413 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
414 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
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)); )
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
);
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)
435 DEFINE_TRIVIAL_CLEANUP_FUNC(IteratedCache
*, iterated_cache_free
);
437 #define _cleanup_iterated_cache_free_ _cleanup_(iterated_cache_freep)