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 --enable-debug=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
28 * implementation take (HashmapBase*) parameters and are run-time polymorphic,
29 * though the API is not meant to be polymorphic (do not call functions
30 * internal_*() directly). */
31 typedef struct HashmapBase HashmapBase
;
33 /* Specific hashmap/set types */
34 typedef struct Hashmap Hashmap
; /* Maps keys to values */
35 typedef struct OrderedHashmap OrderedHashmap
; /* Like Hashmap, but also remembers entry insertion order */
36 typedef struct Set Set
; /* Stores just keys */
38 typedef struct IteratedCache IteratedCache
; /* Caches the iterated order of one of the above */
40 /* Ideally the Iterator would be an opaque struct, but it is instantiated
41 * by hashmap users, so the definition has to be here. Do not use its fields
44 unsigned idx
; /* index of an entry to be iterated next */
45 const void *next_key
; /* expected value of that entry's key pointer */
46 #if ENABLE_DEBUG_HASHMAP
47 unsigned put_count
; /* hashmap's put_count recorded at start of iteration */
48 unsigned rem_count
; /* hashmap's rem_count in previous iteration */
49 unsigned prev_idx
; /* idx in previous iteration */
53 #define _IDX_ITERATOR_FIRST (UINT_MAX - 1)
54 #define ITERATOR_FIRST ((Iterator) { .idx = _IDX_ITERATOR_FIRST, .next_key = NULL })
56 /* Macros for type checking */
57 #define PTR_COMPATIBLE_WITH_HASHMAP_BASE(h) \
58 (__builtin_types_compatible_p(typeof(h), HashmapBase*) || \
59 __builtin_types_compatible_p(typeof(h), Hashmap*) || \
60 __builtin_types_compatible_p(typeof(h), OrderedHashmap*) || \
61 __builtin_types_compatible_p(typeof(h), Set*))
63 #define PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h) \
64 (__builtin_types_compatible_p(typeof(h), Hashmap*) || \
65 __builtin_types_compatible_p(typeof(h), OrderedHashmap*)) \
67 #define HASHMAP_BASE(h) \
68 __builtin_choose_expr(PTR_COMPATIBLE_WITH_HASHMAP_BASE(h), \
72 #define PLAIN_HASHMAP(h) \
73 __builtin_choose_expr(PTR_COMPATIBLE_WITH_PLAIN_HASHMAP(h), \
77 #if ENABLE_DEBUG_HASHMAP
78 # define HASHMAP_DEBUG_PARAMS , const char *func, const char *file, int line
79 # define HASHMAP_DEBUG_SRC_ARGS , __func__, PROJECT_FILE, __LINE__
80 # define HASHMAP_DEBUG_PASS_ARGS , func, file, line
82 # define HASHMAP_DEBUG_PARAMS
83 # define HASHMAP_DEBUG_SRC_ARGS
84 # define HASHMAP_DEBUG_PASS_ARGS
87 Hashmap
*internal_hashmap_new(const struct hash_ops
*hash_ops HASHMAP_DEBUG_PARAMS
);
88 OrderedHashmap
*internal_ordered_hashmap_new(const struct hash_ops
*hash_ops HASHMAP_DEBUG_PARAMS
);
89 #define hashmap_new(ops) internal_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
90 #define ordered_hashmap_new(ops) internal_ordered_hashmap_new(ops HASHMAP_DEBUG_SRC_ARGS)
92 #define hashmap_free_and_replace(a, b) \
100 HashmapBase
*internal_hashmap_free(HashmapBase
*h
, free_func_t default_free_key
, free_func_t default_free_value
);
101 static inline Hashmap
*hashmap_free(Hashmap
*h
) {
102 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), NULL
, NULL
);
104 static inline OrderedHashmap
*ordered_hashmap_free(OrderedHashmap
*h
) {
105 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), NULL
, NULL
);
108 static inline Hashmap
*hashmap_free_free(Hashmap
*h
) {
109 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), NULL
, free
);
111 static inline OrderedHashmap
*ordered_hashmap_free_free(OrderedHashmap
*h
) {
112 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), NULL
, free
);
115 static inline Hashmap
*hashmap_free_free_key(Hashmap
*h
) {
116 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), free
, NULL
);
118 static inline OrderedHashmap
*ordered_hashmap_free_free_key(OrderedHashmap
*h
) {
119 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), free
, NULL
);
122 static inline Hashmap
*hashmap_free_free_free(Hashmap
*h
) {
123 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), free
, free
);
125 static inline OrderedHashmap
*ordered_hashmap_free_free_free(OrderedHashmap
*h
) {
126 return (void*) internal_hashmap_free(HASHMAP_BASE(h
), free
, free
);
129 IteratedCache
*iterated_cache_free(IteratedCache
*cache
);
130 int iterated_cache_get(IteratedCache
*cache
, const void ***res_keys
, const void ***res_values
, unsigned *res_n_entries
);
132 HashmapBase
*internal_hashmap_copy(HashmapBase
*h
);
133 static inline Hashmap
*hashmap_copy(Hashmap
*h
) {
134 return (Hashmap
*) internal_hashmap_copy(HASHMAP_BASE(h
));
136 static inline OrderedHashmap
*ordered_hashmap_copy(OrderedHashmap
*h
) {
137 return (OrderedHashmap
*) internal_hashmap_copy(HASHMAP_BASE(h
));
140 int internal_hashmap_ensure_allocated(Hashmap
**h
, const struct hash_ops
*hash_ops HASHMAP_DEBUG_PARAMS
);
141 int internal_ordered_hashmap_ensure_allocated(OrderedHashmap
**h
, const struct hash_ops
*hash_ops HASHMAP_DEBUG_PARAMS
);
142 #define hashmap_ensure_allocated(h, ops) internal_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
143 #define ordered_hashmap_ensure_allocated(h, ops) internal_ordered_hashmap_ensure_allocated(h, ops HASHMAP_DEBUG_SRC_ARGS)
145 IteratedCache
*internal_hashmap_iterated_cache_new(HashmapBase
*h
);
146 static inline IteratedCache
*hashmap_iterated_cache_new(Hashmap
*h
) {
147 return (IteratedCache
*) internal_hashmap_iterated_cache_new(HASHMAP_BASE(h
));
149 static inline IteratedCache
*ordered_hashmap_iterated_cache_new(OrderedHashmap
*h
) {
150 return (IteratedCache
*) internal_hashmap_iterated_cache_new(HASHMAP_BASE(h
));
153 int hashmap_put(Hashmap
*h
, const void *key
, void *value
);
154 static inline int ordered_hashmap_put(OrderedHashmap
*h
, const void *key
, void *value
) {
155 return hashmap_put(PLAIN_HASHMAP(h
), key
, value
);
158 int hashmap_put_strdup(Hashmap
**h
, const char *k
, const char *v
);
160 int hashmap_update(Hashmap
*h
, const void *key
, void *value
);
161 static inline int ordered_hashmap_update(OrderedHashmap
*h
, const void *key
, void *value
) {
162 return hashmap_update(PLAIN_HASHMAP(h
), key
, value
);
165 int hashmap_replace(Hashmap
*h
, const void *key
, void *value
);
166 static inline int ordered_hashmap_replace(OrderedHashmap
*h
, const void *key
, void *value
) {
167 return hashmap_replace(PLAIN_HASHMAP(h
), key
, value
);
170 void *internal_hashmap_get(HashmapBase
*h
, const void *key
);
171 static inline void *hashmap_get(Hashmap
*h
, const void *key
) {
172 return internal_hashmap_get(HASHMAP_BASE(h
), key
);
174 static inline void *ordered_hashmap_get(OrderedHashmap
*h
, const void *key
) {
175 return internal_hashmap_get(HASHMAP_BASE(h
), key
);
178 void *hashmap_get2(Hashmap
*h
, const void *key
, void **rkey
);
179 static inline void *ordered_hashmap_get2(OrderedHashmap
*h
, const void *key
, void **rkey
) {
180 return hashmap_get2(PLAIN_HASHMAP(h
), key
, rkey
);
183 bool internal_hashmap_contains(HashmapBase
*h
, const void *key
);
184 static inline bool hashmap_contains(Hashmap
*h
, const void *key
) {
185 return internal_hashmap_contains(HASHMAP_BASE(h
), key
);
187 static inline bool ordered_hashmap_contains(OrderedHashmap
*h
, const void *key
) {
188 return internal_hashmap_contains(HASHMAP_BASE(h
), key
);
191 void *internal_hashmap_remove(HashmapBase
*h
, const void *key
);
192 static inline void *hashmap_remove(Hashmap
*h
, const void *key
) {
193 return internal_hashmap_remove(HASHMAP_BASE(h
), key
);
195 static inline void *ordered_hashmap_remove(OrderedHashmap
*h
, const void *key
) {
196 return internal_hashmap_remove(HASHMAP_BASE(h
), key
);
199 void *hashmap_remove2(Hashmap
*h
, const void *key
, void **rkey
);
200 static inline void *ordered_hashmap_remove2(OrderedHashmap
*h
, const void *key
, void **rkey
) {
201 return hashmap_remove2(PLAIN_HASHMAP(h
), key
, rkey
);
204 void *internal_hashmap_remove_value(HashmapBase
*h
, const void *key
, void *value
);
205 static inline void *hashmap_remove_value(Hashmap
*h
, const void *key
, void *value
) {
206 return internal_hashmap_remove_value(HASHMAP_BASE(h
), key
, value
);
209 static inline void *ordered_hashmap_remove_value(OrderedHashmap
*h
, const void *key
, void *value
) {
210 return hashmap_remove_value(PLAIN_HASHMAP(h
), key
, value
);
213 int hashmap_remove_and_put(Hashmap
*h
, const void *old_key
, const void *new_key
, void *value
);
214 static inline int ordered_hashmap_remove_and_put(OrderedHashmap
*h
, const void *old_key
, const void *new_key
, void *value
) {
215 return hashmap_remove_and_put(PLAIN_HASHMAP(h
), old_key
, new_key
, value
);
218 int hashmap_remove_and_replace(Hashmap
*h
, const void *old_key
, const void *new_key
, void *value
);
219 static inline int ordered_hashmap_remove_and_replace(OrderedHashmap
*h
, const void *old_key
, const void *new_key
, void *value
) {
220 return hashmap_remove_and_replace(PLAIN_HASHMAP(h
), old_key
, new_key
, value
);
223 /* Since merging data from a OrderedHashmap into a Hashmap or vice-versa
224 * should just work, allow this by having looser type-checking here. */
225 int internal_hashmap_merge(Hashmap
*h
, Hashmap
*other
);
226 #define hashmap_merge(h, other) internal_hashmap_merge(PLAIN_HASHMAP(h), PLAIN_HASHMAP(other))
227 #define ordered_hashmap_merge(h, other) hashmap_merge(h, other)
229 int internal_hashmap_reserve(HashmapBase
*h
, unsigned entries_add
);
230 static inline int hashmap_reserve(Hashmap
*h
, unsigned entries_add
) {
231 return internal_hashmap_reserve(HASHMAP_BASE(h
), entries_add
);
233 static inline int ordered_hashmap_reserve(OrderedHashmap
*h
, unsigned entries_add
) {
234 return internal_hashmap_reserve(HASHMAP_BASE(h
), entries_add
);
237 int internal_hashmap_move(HashmapBase
*h
, HashmapBase
*other
);
238 /* Unlike hashmap_merge, hashmap_move does not allow mixing the types. */
239 static inline int hashmap_move(Hashmap
*h
, Hashmap
*other
) {
240 return internal_hashmap_move(HASHMAP_BASE(h
), HASHMAP_BASE(other
));
242 static inline int ordered_hashmap_move(OrderedHashmap
*h
, OrderedHashmap
*other
) {
243 return internal_hashmap_move(HASHMAP_BASE(h
), HASHMAP_BASE(other
));
246 int internal_hashmap_move_one(HashmapBase
*h
, HashmapBase
*other
, const void *key
);
247 static inline int hashmap_move_one(Hashmap
*h
, Hashmap
*other
, const void *key
) {
248 return internal_hashmap_move_one(HASHMAP_BASE(h
), HASHMAP_BASE(other
), key
);
250 static inline int ordered_hashmap_move_one(OrderedHashmap
*h
, OrderedHashmap
*other
, const void *key
) {
251 return internal_hashmap_move_one(HASHMAP_BASE(h
), HASHMAP_BASE(other
), key
);
254 unsigned internal_hashmap_size(HashmapBase
*h
) _pure_
;
255 static inline unsigned hashmap_size(Hashmap
*h
) {
256 return internal_hashmap_size(HASHMAP_BASE(h
));
258 static inline unsigned ordered_hashmap_size(OrderedHashmap
*h
) {
259 return internal_hashmap_size(HASHMAP_BASE(h
));
262 static inline bool hashmap_isempty(Hashmap
*h
) {
263 return hashmap_size(h
) == 0;
265 static inline bool ordered_hashmap_isempty(OrderedHashmap
*h
) {
266 return ordered_hashmap_size(h
) == 0;
269 unsigned internal_hashmap_buckets(HashmapBase
*h
) _pure_
;
270 static inline unsigned hashmap_buckets(Hashmap
*h
) {
271 return internal_hashmap_buckets(HASHMAP_BASE(h
));
273 static inline unsigned ordered_hashmap_buckets(OrderedHashmap
*h
) {
274 return internal_hashmap_buckets(HASHMAP_BASE(h
));
277 bool internal_hashmap_iterate(HashmapBase
*h
, Iterator
*i
, void **value
, const void **key
);
278 static inline bool hashmap_iterate(Hashmap
*h
, Iterator
*i
, void **value
, const void **key
) {
279 return internal_hashmap_iterate(HASHMAP_BASE(h
), i
, value
, key
);
281 static inline bool ordered_hashmap_iterate(OrderedHashmap
*h
, Iterator
*i
, void **value
, const void **key
) {
282 return internal_hashmap_iterate(HASHMAP_BASE(h
), i
, value
, key
);
285 void internal_hashmap_clear(HashmapBase
*h
, free_func_t default_free_key
, free_func_t default_free_value
);
286 static inline void hashmap_clear(Hashmap
*h
) {
287 internal_hashmap_clear(HASHMAP_BASE(h
), NULL
, NULL
);
289 static inline void ordered_hashmap_clear(OrderedHashmap
*h
) {
290 internal_hashmap_clear(HASHMAP_BASE(h
), NULL
, NULL
);
293 static inline void hashmap_clear_free(Hashmap
*h
) {
294 internal_hashmap_clear(HASHMAP_BASE(h
), NULL
, free
);
296 static inline void ordered_hashmap_clear_free(OrderedHashmap
*h
) {
297 internal_hashmap_clear(HASHMAP_BASE(h
), NULL
, free
);
300 static inline void hashmap_clear_free_key(Hashmap
*h
) {
301 internal_hashmap_clear(HASHMAP_BASE(h
), free
, NULL
);
303 static inline void ordered_hashmap_clear_free_key(OrderedHashmap
*h
) {
304 internal_hashmap_clear(HASHMAP_BASE(h
), free
, NULL
);
307 static inline void hashmap_clear_free_free(Hashmap
*h
) {
308 internal_hashmap_clear(HASHMAP_BASE(h
), free
, free
);
310 static inline void ordered_hashmap_clear_free_free(OrderedHashmap
*h
) {
311 internal_hashmap_clear(HASHMAP_BASE(h
), free
, free
);
315 * Note about all *_first*() functions
317 * For plain Hashmaps and Sets the order of entries is undefined.
318 * The functions find whatever entry is first in the implementation
321 * Only for OrderedHashmaps the order is well defined and finding
322 * the first entry is O(1).
325 void *internal_hashmap_first_key_and_value(HashmapBase
*h
, bool remove
, void **ret_key
);
326 static inline void *hashmap_steal_first_key_and_value(Hashmap
*h
, void **ret
) {
327 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), true, ret
);
329 static inline void *ordered_hashmap_steal_first_key_and_value(OrderedHashmap
*h
, void **ret
) {
330 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), true, ret
);
332 static inline void *hashmap_first_key_and_value(Hashmap
*h
, void **ret
) {
333 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), false, ret
);
335 static inline void *ordered_hashmap_first_key_and_value(OrderedHashmap
*h
, void **ret
) {
336 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), false, ret
);
339 static inline void *hashmap_steal_first(Hashmap
*h
) {
340 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), true, NULL
);
342 static inline void *ordered_hashmap_steal_first(OrderedHashmap
*h
) {
343 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), true, NULL
);
345 static inline void *hashmap_first(Hashmap
*h
) {
346 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), false, NULL
);
348 static inline void *ordered_hashmap_first(OrderedHashmap
*h
) {
349 return internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), false, NULL
);
352 static inline void *internal_hashmap_first_key(HashmapBase
*h
, bool remove
) {
355 (void) internal_hashmap_first_key_and_value(HASHMAP_BASE(h
), remove
, &key
);
358 static inline void *hashmap_steal_first_key(Hashmap
*h
) {
359 return internal_hashmap_first_key(HASHMAP_BASE(h
), true);
361 static inline void *ordered_hashmap_steal_first_key(OrderedHashmap
*h
) {
362 return internal_hashmap_first_key(HASHMAP_BASE(h
), true);
364 static inline void *hashmap_first_key(Hashmap
*h
) {
365 return internal_hashmap_first_key(HASHMAP_BASE(h
), false);
367 static inline void *ordered_hashmap_first_key(OrderedHashmap
*h
) {
368 return internal_hashmap_first_key(HASHMAP_BASE(h
), false);
371 #define hashmap_clear_with_destructor(_s, _f) \
374 while ((_item = hashmap_steal_first(_s))) \
377 #define hashmap_free_with_destructor(_s, _f) \
379 hashmap_clear_with_destructor(_s, _f); \
382 #define ordered_hashmap_clear_with_destructor(_s, _f) \
385 while ((_item = ordered_hashmap_steal_first(_s))) \
388 #define ordered_hashmap_free_with_destructor(_s, _f) \
390 ordered_hashmap_clear_with_destructor(_s, _f); \
391 ordered_hashmap_free(_s); \
394 /* no hashmap_next */
395 void *ordered_hashmap_next(OrderedHashmap
*h
, const void *key
);
397 char **internal_hashmap_get_strv(HashmapBase
*h
);
398 static inline char **hashmap_get_strv(Hashmap
*h
) {
399 return internal_hashmap_get_strv(HASHMAP_BASE(h
));
401 static inline char **ordered_hashmap_get_strv(OrderedHashmap
*h
) {
402 return internal_hashmap_get_strv(HASHMAP_BASE(h
));
406 * Hashmaps are iterated in unpredictable order.
407 * OrderedHashmaps are an exception to this. They are iterated in the order
408 * the entries were inserted.
409 * It is safe to remove the current entry.
411 #define HASHMAP_FOREACH(e, h, i) \
412 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), NULL); )
414 #define ORDERED_HASHMAP_FOREACH(e, h, i) \
415 for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), NULL); )
417 #define HASHMAP_FOREACH_KEY(e, k, h, i) \
418 for ((i) = ITERATOR_FIRST; hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
420 #define ORDERED_HASHMAP_FOREACH_KEY(e, k, h, i) \
421 for ((i) = ITERATOR_FIRST; ordered_hashmap_iterate((h), &(i), (void**)&(e), (const void**) &(k)); )
423 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, hashmap_free
);
424 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, hashmap_free_free
);
425 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, hashmap_free_free_key
);
426 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, hashmap_free_free_free
);
427 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap
*, ordered_hashmap_free
);
428 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap
*, ordered_hashmap_free_free
);
429 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap
*, ordered_hashmap_free_free_key
);
430 DEFINE_TRIVIAL_CLEANUP_FUNC(OrderedHashmap
*, ordered_hashmap_free_free_free
);
432 #define _cleanup_hashmap_free_ _cleanup_(hashmap_freep)
433 #define _cleanup_hashmap_free_free_ _cleanup_(hashmap_free_freep)
434 #define _cleanup_hashmap_free_free_free_ _cleanup_(hashmap_free_free_freep)
435 #define _cleanup_ordered_hashmap_free_ _cleanup_(ordered_hashmap_freep)
436 #define _cleanup_ordered_hashmap_free_free_ _cleanup_(ordered_hashmap_free_freep)
437 #define _cleanup_ordered_hashmap_free_free_free_ _cleanup_(ordered_hashmap_free_free_freep)
439 DEFINE_TRIVIAL_CLEANUP_FUNC(IteratedCache
*, iterated_cache_free
);
441 #define _cleanup_iterated_cache_free_ _cleanup_(iterated_cache_freep)