]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/hashmap.c
tree-wide: use ASSERT_PTR more
[thirdparty/systemd.git] / src / basic / hashmap.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
a7334b09 2
60918275 3#include <errno.h>
d25d4f18 4#include <fnmatch.h>
ae0b700a 5#include <pthread.h>
11c3a366 6#include <stdint.h>
d4510856 7#include <stdlib.h>
60918275 8
b5efdb8a 9#include "alloc-util.h"
556c7bae 10#include "fileio.h"
b4f60743 11#include "hashmap.h"
60918275 12#include "macro.h"
0a970718 13#include "memory-util.h"
b3dcf58e 14#include "mempool.h"
f5947a5e 15#include "missing_syscall.h"
d4510856 16#include "process-util.h"
3df3e884 17#include "random-util.h"
d4510856
LP
18#include "set.h"
19#include "siphash24.h"
556c7bae 20#include "string-util.h"
d4510856 21#include "strv.h"
60918275 22
349cc4a5 23#if ENABLE_DEBUG_HASHMAP
2eec67ac
TA
24#include "list.h"
25#endif
26
89439d4f
MS
27/*
28 * Implementation of hashmaps.
29 * Addressing: open
30 * - uses less RAM compared to closed addressing (chaining), because
31 * our entries are small (especially in Sets, which tend to contain
32 * the majority of entries in systemd).
33 * Collision resolution: Robin Hood
34 * - tends to equalize displacement of entries from their optimal buckets.
35 * Probe sequence: linear
36 * - though theoretically worse than random probing/uniform hashing/double
37 * hashing, it is good for cache locality.
38 *
39 * References:
40 * Celis, P. 1986. Robin Hood Hashing.
41 * Ph.D. Dissertation. University of Waterloo, Waterloo, Ont., Canada, Canada.
42 * https://cs.uwaterloo.ca/research/tr/1986/CS-86-14.pdf
43 * - The results are derived for random probing. Suggests deletion with
44 * tombstones and two mean-centered search methods. None of that works
45 * well for linear probing.
46 *
47 * Janson, S. 2005. Individual displacements for linear probing hashing with different insertion policies.
48 * ACM Trans. Algorithms 1, 2 (October 2005), 177-213.
49 * DOI=10.1145/1103963.1103964 http://doi.acm.org/10.1145/1103963.1103964
50 * http://www.math.uu.se/~svante/papers/sj157.pdf
51 * - Applies to Robin Hood with linear probing. Contains remarks on
52 * the unsuitability of mean-centered search with linear probing.
53 *
54 * Viola, A. 2005. Exact distribution of individual displacements in linear probing hashing.
55 * ACM Trans. Algorithms 1, 2 (October 2005), 214-242.
56 * DOI=10.1145/1103963.1103965 http://doi.acm.org/10.1145/1103963.1103965
57 * - Similar to Janson. Note that Viola writes about C_{m,n} (number of probes
58 * in a successful search), and Janson writes about displacement. C = d + 1.
59 *
60 * Goossaert, E. 2013. Robin Hood hashing: backward shift deletion.
61 * http://codecapsule.com/2013/11/17/robin-hood-hashing-backward-shift-deletion/
62 * - Explanation of backward shift deletion with pictures.
63 *
64 * Khuong, P. 2013. The Other Robin Hood Hashing.
65 * http://www.pvk.ca/Blog/2013/11/26/the-other-robin-hood-hashing/
66 * - Short summary of random vs. linear probing, and tombstones vs. backward shift.
67 */
68
69/*
70 * XXX Ideas for improvement:
71 * For unordered hashmaps, randomize iteration order, similarly to Perl:
72 * http://blog.booking.com/hardening-perls-hash-function.html
73 */
74
75/* INV_KEEP_FREE = 1 / (1 - max_load_factor)
76 * e.g. 1 / (1 - 0.8) = 5 ... keep one fifth of the buckets free. */
77#define INV_KEEP_FREE 5U
78
79/* Fields common to entries of all hashmap/set types */
80struct hashmap_base_entry {
60918275 81 const void *key;
89439d4f
MS
82};
83
84/* Entry types for specific hashmap/set types
85 * hashmap_base_entry must be at the beginning of each entry struct. */
86
87struct plain_hashmap_entry {
88 struct hashmap_base_entry b;
60918275 89 void *value;
60918275
LP
90};
91
89439d4f
MS
92struct ordered_hashmap_entry {
93 struct plain_hashmap_entry p;
94 unsigned iterate_next, iterate_previous;
95};
60918275 96
89439d4f
MS
97struct set_entry {
98 struct hashmap_base_entry b;
99};
45fa9e29 100
89439d4f
MS
101/* In several functions it is advantageous to have the hash table extended
102 * virtually by a couple of additional buckets. We reserve special index values
103 * for these "swap" buckets. */
104#define _IDX_SWAP_BEGIN (UINT_MAX - 3)
105#define IDX_PUT (_IDX_SWAP_BEGIN + 0)
106#define IDX_TMP (_IDX_SWAP_BEGIN + 1)
107#define _IDX_SWAP_END (_IDX_SWAP_BEGIN + 2)
39c2a6f1 108
89439d4f
MS
109#define IDX_FIRST (UINT_MAX - 1) /* special index for freshly initialized iterators */
110#define IDX_NIL UINT_MAX /* special index value meaning "none" or "end" */
111
112assert_cc(IDX_FIRST == _IDX_SWAP_END);
113assert_cc(IDX_FIRST == _IDX_ITERATOR_FIRST);
114
115/* Storage space for the "swap" buckets.
387f6955 116 * All entry types can fit into an ordered_hashmap_entry. */
89439d4f
MS
117struct swap_entries {
118 struct ordered_hashmap_entry e[_IDX_SWAP_END - _IDX_SWAP_BEGIN];
60918275
LP
119};
120
89439d4f
MS
121/* Distance from Initial Bucket */
122typedef uint8_t dib_raw_t;
3ef11dcf
ZJS
123#define DIB_RAW_OVERFLOW ((dib_raw_t)0xfdU) /* indicates DIB value is greater than representable */
124#define DIB_RAW_REHASH ((dib_raw_t)0xfeU) /* entry yet to be rehashed during in-place resize */
125#define DIB_RAW_FREE ((dib_raw_t)0xffU) /* a free bucket */
126#define DIB_RAW_INIT ((char)DIB_RAW_FREE) /* a byte to memset a DIB store with when initializing */
89439d4f
MS
127
128#define DIB_FREE UINT_MAX
129
349cc4a5 130#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
131struct hashmap_debug_info {
132 LIST_FIELDS(struct hashmap_debug_info, debug_list);
133 unsigned max_entries; /* high watermark of n_entries */
134
135 /* who allocated this hashmap */
136 int line;
137 const char *file;
138 const char *func;
139
140 /* fields to detect modification while iterating */
141 unsigned put_count; /* counts puts into the hashmap */
142 unsigned rem_count; /* counts removals from hashmap */
143 unsigned last_rem_idx; /* remembers last removal index */
39c2a6f1
LP
144};
145
89439d4f
MS
146/* Tracks all existing hashmaps. Get at it from gdb. See sd_dump_hashmaps.py */
147static LIST_HEAD(struct hashmap_debug_info, hashmap_debug_list);
4f1b3061 148static pthread_mutex_t hashmap_debug_list_mutex = PTHREAD_MUTEX_INITIALIZER;
55825de5 149#endif
39c2a6f1 150
89439d4f
MS
151enum HashmapType {
152 HASHMAP_TYPE_PLAIN,
153 HASHMAP_TYPE_ORDERED,
154 HASHMAP_TYPE_SET,
155 _HASHMAP_TYPE_MAX
156};
39c2a6f1 157
89439d4f 158struct _packed_ indirect_storage {
1a39bc8c 159 void *storage; /* where buckets and DIBs are stored */
89439d4f
MS
160 uint8_t hash_key[HASH_KEY_SIZE]; /* hash key; changes during resize */
161
162 unsigned n_entries; /* number of stored entries */
163 unsigned n_buckets; /* number of buckets */
164
165 unsigned idx_lowest_entry; /* Index below which all buckets are free.
79893116 166 Makes "while (hashmap_steal_first())" loops
89439d4f
MS
167 O(n) instead of O(n^2) for unordered hashmaps. */
168 uint8_t _pad[3]; /* padding for the whole HashmapBase */
169 /* The bitfields in HashmapBase complete the alignment of the whole thing. */
170};
171
172struct direct_storage {
173 /* This gives us 39 bytes on 64bit, or 35 bytes on 32bit.
174 * That's room for 4 set_entries + 4 DIB bytes + 3 unused bytes on 64bit,
175 * or 7 set_entries + 7 DIB bytes + 0 unused bytes on 32bit. */
1a39bc8c 176 uint8_t storage[sizeof(struct indirect_storage)];
89439d4f
MS
177};
178
179#define DIRECT_BUCKETS(entry_t) \
180 (sizeof(struct direct_storage) / (sizeof(entry_t) + sizeof(dib_raw_t)))
181
182/* We should be able to store at least one entry directly. */
183assert_cc(DIRECT_BUCKETS(struct ordered_hashmap_entry) >= 1);
184
185/* We have 3 bits for n_direct_entries. */
186assert_cc(DIRECT_BUCKETS(struct set_entry) < (1 << 3));
187
188/* Hashmaps with directly stored entries all use this shared hash key.
189 * It's no big deal if the key is guessed, because there can be only
190 * a handful of directly stored entries in a hashmap. When a hashmap
191 * outgrows direct storage, it gets its own key for indirect storage. */
192static uint8_t shared_hash_key[HASH_KEY_SIZE];
89439d4f
MS
193
194/* Fields that all hashmap/set types must have */
195struct HashmapBase {
196 const struct hash_ops *hash_ops; /* hash and compare ops to use */
197
198 union _packed_ {
199 struct indirect_storage indirect; /* if has_indirect */
200 struct direct_storage direct; /* if !has_indirect */
201 };
202
203 enum HashmapType type:2; /* HASHMAP_TYPE_* */
204 bool has_indirect:1; /* whether indirect storage is used */
205 unsigned n_direct_entries:3; /* Number of entries in direct storage.
206 * Only valid if !has_indirect. */
207 bool from_pool:1; /* whether was allocated from mempool */
45ea84d8
VC
208 bool dirty:1; /* whether dirtied since last iterated_cache_get() */
209 bool cached:1; /* whether this hashmap is being cached */
55825de5
ZJS
210
211#if ENABLE_DEBUG_HASHMAP
212 struct hashmap_debug_info debug;
213#endif
89439d4f
MS
214};
215
216/* Specific hash types
217 * HashmapBase must be at the beginning of each hashmap struct. */
218
219struct Hashmap {
220 struct HashmapBase b;
221};
222
223struct OrderedHashmap {
224 struct HashmapBase b;
225 unsigned iterate_list_head, iterate_list_tail;
226};
227
228struct Set {
229 struct HashmapBase b;
230};
231
45ea84d8
VC
232typedef struct CacheMem {
233 const void **ptr;
319a4f4b 234 size_t n_populated;
45ea84d8
VC
235 bool active:1;
236} CacheMem;
237
238struct IteratedCache {
239 HashmapBase *hashmap;
240 CacheMem keys, values;
241};
242
89439d4f
MS
243DEFINE_MEMPOOL(hashmap_pool, Hashmap, 8);
244DEFINE_MEMPOOL(ordered_hashmap_pool, OrderedHashmap, 8);
245/* No need for a separate Set pool */
246assert_cc(sizeof(Hashmap) == sizeof(Set));
247
248struct hashmap_type_info {
249 size_t head_size;
250 size_t entry_size;
251 struct mempool *mempool;
252 unsigned n_direct_buckets;
253};
254
43874aa7 255static _used_ const struct hashmap_type_info hashmap_type_info[_HASHMAP_TYPE_MAX] = {
89439d4f
MS
256 [HASHMAP_TYPE_PLAIN] = {
257 .head_size = sizeof(Hashmap),
258 .entry_size = sizeof(struct plain_hashmap_entry),
259 .mempool = &hashmap_pool,
260 .n_direct_buckets = DIRECT_BUCKETS(struct plain_hashmap_entry),
261 },
262 [HASHMAP_TYPE_ORDERED] = {
263 .head_size = sizeof(OrderedHashmap),
264 .entry_size = sizeof(struct ordered_hashmap_entry),
265 .mempool = &ordered_hashmap_pool,
266 .n_direct_buckets = DIRECT_BUCKETS(struct ordered_hashmap_entry),
267 },
268 [HASHMAP_TYPE_SET] = {
269 .head_size = sizeof(Set),
270 .entry_size = sizeof(struct set_entry),
271 .mempool = &hashmap_pool,
272 .n_direct_buckets = DIRECT_BUCKETS(struct set_entry),
273 },
274};
39c2a6f1 275
d18cb393 276#if VALGRIND
d34dae18 277_destructor_ static void cleanup_pools(void) {
556c7bae
ZJS
278 _cleanup_free_ char *t = NULL;
279 int r;
280
281 /* Be nice to valgrind */
282
283 /* The pool is only allocated by the main thread, but the memory can
284 * be passed to other threads. Let's clean up if we are the main thread
285 * and no other threads are live. */
31c9d74d
FS
286 /* We build our own is_main_thread() here, which doesn't use C11
287 * TLS based caching of the result. That's because valgrind apparently
288 * doesn't like malloc() (which C11 TLS internally uses) to be called
289 * from a GCC destructors. */
290 if (getpid() != gettid())
556c7bae
ZJS
291 return;
292
293 r = get_proc_field("/proc/self/status", "Threads", WHITESPACE, &t);
294 if (r < 0 || !streq(t, "1"))
295 return;
296
297 mempool_drop(&hashmap_pool);
298 mempool_drop(&ordered_hashmap_pool);
299}
300#endif
301
89439d4f
MS
302static unsigned n_buckets(HashmapBase *h) {
303 return h->has_indirect ? h->indirect.n_buckets
304 : hashmap_type_info[h->type].n_direct_buckets;
305}
306
307static unsigned n_entries(HashmapBase *h) {
308 return h->has_indirect ? h->indirect.n_entries
309 : h->n_direct_entries;
310}
311
312static void n_entries_inc(HashmapBase *h) {
313 if (h->has_indirect)
314 h->indirect.n_entries++;
315 else
316 h->n_direct_entries++;
317}
318
319static void n_entries_dec(HashmapBase *h) {
320 if (h->has_indirect)
321 h->indirect.n_entries--;
322 else
323 h->n_direct_entries--;
324}
325
8a35af80 326static void* storage_ptr(HashmapBase *h) {
89439d4f
MS
327 return h->has_indirect ? h->indirect.storage
328 : h->direct.storage;
329}
330
8a35af80 331static uint8_t* hash_key(HashmapBase *h) {
89439d4f
MS
332 return h->has_indirect ? h->indirect.hash_key
333 : shared_hash_key;
334}
335
336static unsigned base_bucket_hash(HashmapBase *h, const void *p) {
b826ab58 337 struct siphash state;
0cb3c286 338 uint64_t hash;
b826ab58 339
0cb3c286 340 siphash24_init(&state, hash_key(h));
b826ab58
TG
341
342 h->hash_ops->hash(p, &state);
343
933f9cae 344 hash = siphash24_finalize(&state);
0cb3c286
TG
345
346 return (unsigned) (hash % n_buckets(h));
9bf3b535 347}
89439d4f 348#define bucket_hash(h, p) base_bucket_hash(HASHMAP_BASE(h), p)
9bf3b535 349
a1e92eee 350static void base_set_dirty(HashmapBase *h) {
84dcca75
VC
351 h->dirty = true;
352}
353#define hashmap_set_dirty(h) base_set_dirty(HASHMAP_BASE(h))
354
9bf3b535
LP
355static void get_hash_key(uint8_t hash_key[HASH_KEY_SIZE], bool reuse_is_ok) {
356 static uint8_t current[HASH_KEY_SIZE];
357 static bool current_initialized = false;
358
359 /* Returns a hash function key to use. In order to keep things
360 * fast we will not generate a new key each time we allocate a
361 * new hash table. Instead, we'll just reuse the most recently
362 * generated one, except if we never generated one or when we
363 * are rehashing an entire hash table because we reached a
364 * fill level */
365
366 if (!current_initialized || !reuse_is_ok) {
367 random_bytes(current, sizeof(current));
368 current_initialized = true;
369 }
370
371 memcpy(hash_key, current, sizeof(current));
a3b6fafe
LP
372}
373
8a35af80 374static struct hashmap_base_entry* bucket_at(HashmapBase *h, unsigned idx) {
89439d4f 375 return (struct hashmap_base_entry*)
1a39bc8c 376 ((uint8_t*) storage_ptr(h) + idx * hashmap_type_info[h->type].entry_size);
89439d4f
MS
377}
378
8a35af80 379static struct plain_hashmap_entry* plain_bucket_at(Hashmap *h, unsigned idx) {
89439d4f
MS
380 return (struct plain_hashmap_entry*) bucket_at(HASHMAP_BASE(h), idx);
381}
382
8a35af80 383static struct ordered_hashmap_entry* ordered_bucket_at(OrderedHashmap *h, unsigned idx) {
89439d4f
MS
384 return (struct ordered_hashmap_entry*) bucket_at(HASHMAP_BASE(h), idx);
385}
39c2a6f1 386
89439d4f
MS
387static struct set_entry *set_bucket_at(Set *h, unsigned idx) {
388 return (struct set_entry*) bucket_at(HASHMAP_BASE(h), idx);
389}
39c2a6f1 390
8a35af80 391static struct ordered_hashmap_entry* bucket_at_swap(struct swap_entries *swap, unsigned idx) {
89439d4f
MS
392 return &swap->e[idx - _IDX_SWAP_BEGIN];
393}
39c2a6f1 394
89439d4f
MS
395/* Returns a pointer to the bucket at index idx.
396 * Understands real indexes and swap indexes, hence "_virtual". */
8a35af80 397static struct hashmap_base_entry* bucket_at_virtual(HashmapBase *h, struct swap_entries *swap,
89439d4f
MS
398 unsigned idx) {
399 if (idx < _IDX_SWAP_BEGIN)
400 return bucket_at(h, idx);
401
402 if (idx < _IDX_SWAP_END)
403 return &bucket_at_swap(swap, idx)->p.b;
404
04499a70 405 assert_not_reached();
89439d4f
MS
406}
407
8a35af80 408static dib_raw_t* dib_raw_ptr(HashmapBase *h) {
89439d4f 409 return (dib_raw_t*)
1a39bc8c 410 ((uint8_t*) storage_ptr(h) + hashmap_type_info[h->type].entry_size * n_buckets(h));
89439d4f
MS
411}
412
413static unsigned bucket_distance(HashmapBase *h, unsigned idx, unsigned from) {
414 return idx >= from ? idx - from
415 : n_buckets(h) + idx - from;
416}
417
418static unsigned bucket_calculate_dib(HashmapBase *h, unsigned idx, dib_raw_t raw_dib) {
419 unsigned initial_bucket;
420
421 if (raw_dib == DIB_RAW_FREE)
422 return DIB_FREE;
423
424 if (_likely_(raw_dib < DIB_RAW_OVERFLOW))
425 return raw_dib;
426
427 /*
428 * Having an overflow DIB value is very unlikely. The hash function
429 * would have to be bad. For example, in a table of size 2^24 filled
430 * to load factor 0.9 the maximum observed DIB is only about 60.
431 * In theory (assuming I used Maxima correctly), for an infinite size
432 * hash table with load factor 0.8 the probability of a given entry
433 * having DIB > 40 is 1.9e-8.
434 * This returns the correct DIB value by recomputing the hash value in
435 * the unlikely case. XXX Hitting this case could be a hint to rehash.
436 */
437 initial_bucket = bucket_hash(h, bucket_at(h, idx)->key);
438 return bucket_distance(h, idx, initial_bucket);
439}
440
441static void bucket_set_dib(HashmapBase *h, unsigned idx, unsigned dib) {
442 dib_raw_ptr(h)[idx] = dib != DIB_FREE ? MIN(dib, DIB_RAW_OVERFLOW) : DIB_RAW_FREE;
443}
444
445static unsigned skip_free_buckets(HashmapBase *h, unsigned idx) {
446 dib_raw_t *dibs;
447
448 dibs = dib_raw_ptr(h);
449
450 for ( ; idx < n_buckets(h); idx++)
451 if (dibs[idx] != DIB_RAW_FREE)
452 return idx;
453
454 return IDX_NIL;
455}
456
457static void bucket_mark_free(HashmapBase *h, unsigned idx) {
eccaf899 458 memzero(bucket_at(h, idx), hashmap_type_info[h->type].entry_size);
89439d4f
MS
459 bucket_set_dib(h, idx, DIB_FREE);
460}
461
462static void bucket_move_entry(HashmapBase *h, struct swap_entries *swap,
463 unsigned from, unsigned to) {
464 struct hashmap_base_entry *e_from, *e_to;
465
466 assert(from != to);
39c2a6f1 467
89439d4f
MS
468 e_from = bucket_at_virtual(h, swap, from);
469 e_to = bucket_at_virtual(h, swap, to);
470
471 memcpy(e_to, e_from, hashmap_type_info[h->type].entry_size);
472
473 if (h->type == HASHMAP_TYPE_ORDERED) {
474 OrderedHashmap *lh = (OrderedHashmap*) h;
475 struct ordered_hashmap_entry *le, *le_to;
476
477 le_to = (struct ordered_hashmap_entry*) e_to;
478
479 if (le_to->iterate_next != IDX_NIL) {
480 le = (struct ordered_hashmap_entry*)
481 bucket_at_virtual(h, swap, le_to->iterate_next);
482 le->iterate_previous = to;
483 }
484
485 if (le_to->iterate_previous != IDX_NIL) {
486 le = (struct ordered_hashmap_entry*)
487 bucket_at_virtual(h, swap, le_to->iterate_previous);
488 le->iterate_next = to;
489 }
490
491 if (lh->iterate_list_head == from)
492 lh->iterate_list_head = to;
493 if (lh->iterate_list_tail == from)
494 lh->iterate_list_tail = to;
39c2a6f1 495 }
89439d4f 496}
60918275 497
89439d4f
MS
498static unsigned next_idx(HashmapBase *h, unsigned idx) {
499 return (idx + 1U) % n_buckets(h);
500}
60918275 501
89439d4f
MS
502static unsigned prev_idx(HashmapBase *h, unsigned idx) {
503 return (n_buckets(h) + idx - 1U) % n_buckets(h);
504}
60918275 505
8a35af80 506static void* entry_value(HashmapBase *h, struct hashmap_base_entry *e) {
89439d4f 507 switch (h->type) {
45fa9e29 508
89439d4f
MS
509 case HASHMAP_TYPE_PLAIN:
510 case HASHMAP_TYPE_ORDERED:
511 return ((struct plain_hashmap_entry*)e)->value;
39c2a6f1 512
89439d4f
MS
513 case HASHMAP_TYPE_SET:
514 return (void*) e->key;
a3b6fafe 515
89439d4f 516 default:
04499a70 517 assert_not_reached();
89439d4f 518 }
60918275
LP
519}
520
89439d4f
MS
521static void base_remove_entry(HashmapBase *h, unsigned idx) {
522 unsigned left, right, prev, dib;
523 dib_raw_t raw_dib, *dibs;
45fa9e29 524
89439d4f
MS
525 dibs = dib_raw_ptr(h);
526 assert(dibs[idx] != DIB_RAW_FREE);
034c6ed7 527
349cc4a5 528#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
529 h->debug.rem_count++;
530 h->debug.last_rem_idx = idx;
531#endif
034c6ed7 532
89439d4f
MS
533 left = idx;
534 /* Find the stop bucket ("right"). It is either free or has DIB == 0. */
535 for (right = next_idx(h, left); ; right = next_idx(h, right)) {
536 raw_dib = dibs[right];
4c701096 537 if (IN_SET(raw_dib, 0, DIB_RAW_FREE))
89439d4f
MS
538 break;
539
540 /* The buckets are not supposed to be all occupied and with DIB > 0.
541 * That would mean we could make everyone better off by shifting them
542 * backward. This scenario is impossible. */
543 assert(left != right);
544 }
034c6ed7 545
89439d4f
MS
546 if (h->type == HASHMAP_TYPE_ORDERED) {
547 OrderedHashmap *lh = (OrderedHashmap*) h;
548 struct ordered_hashmap_entry *le = ordered_bucket_at(lh, idx);
549
550 if (le->iterate_next != IDX_NIL)
551 ordered_bucket_at(lh, le->iterate_next)->iterate_previous = le->iterate_previous;
552 else
553 lh->iterate_list_tail = le->iterate_previous;
554
555 if (le->iterate_previous != IDX_NIL)
556 ordered_bucket_at(lh, le->iterate_previous)->iterate_next = le->iterate_next;
557 else
558 lh->iterate_list_head = le->iterate_next;
559 }
560
561 /* Now shift all buckets in the interval (left, right) one step backwards */
562 for (prev = left, left = next_idx(h, left); left != right;
563 prev = left, left = next_idx(h, left)) {
564 dib = bucket_calculate_dib(h, left, dibs[left]);
565 assert(dib != 0);
566 bucket_move_entry(h, NULL, left, prev);
567 bucket_set_dib(h, prev, dib - 1);
568 }
569
570 bucket_mark_free(h, prev);
571 n_entries_dec(h);
84dcca75 572 base_set_dirty(h);
034c6ed7 573}
89439d4f
MS
574#define remove_entry(h, idx) base_remove_entry(HASHMAP_BASE(h), idx)
575
576static unsigned hashmap_iterate_in_insertion_order(OrderedHashmap *h, Iterator *i) {
577 struct ordered_hashmap_entry *e;
578 unsigned idx;
034c6ed7 579
101d8e63 580 assert(h);
89439d4f
MS
581 assert(i);
582
583 if (i->idx == IDX_NIL)
584 goto at_end;
585
586 if (i->idx == IDX_FIRST && h->iterate_list_head == IDX_NIL)
587 goto at_end;
588
589 if (i->idx == IDX_FIRST) {
590 idx = h->iterate_list_head;
591 e = ordered_bucket_at(h, idx);
101d8e63 592 } else {
89439d4f
MS
593 idx = i->idx;
594 e = ordered_bucket_at(h, idx);
595 /*
596 * We allow removing the current entry while iterating, but removal may cause
597 * a backward shift. The next entry may thus move one bucket to the left.
598 * To detect when it happens, we remember the key pointer of the entry we were
599 * going to iterate next. If it does not match, there was a backward shift.
600 */
601 if (e->p.b.key != i->next_key) {
602 idx = prev_idx(HASHMAP_BASE(h), idx);
603 e = ordered_bucket_at(h, idx);
604 }
605 assert(e->p.b.key == i->next_key);
101d8e63 606 }
101d8e63 607
349cc4a5 608#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
609 i->prev_idx = idx;
610#endif
611
612 if (e->iterate_next != IDX_NIL) {
613 struct ordered_hashmap_entry *n;
614 i->idx = e->iterate_next;
615 n = ordered_bucket_at(h, i->idx);
616 i->next_key = n->p.b.key;
617 } else
618 i->idx = IDX_NIL;
619
620 return idx;
621
622at_end:
623 i->idx = IDX_NIL;
624 return IDX_NIL;
101d8e63
LP
625}
626
89439d4f
MS
627static unsigned hashmap_iterate_in_internal_order(HashmapBase *h, Iterator *i) {
628 unsigned idx;
629
60918275 630 assert(h);
89439d4f 631 assert(i);
60918275 632
89439d4f
MS
633 if (i->idx == IDX_NIL)
634 goto at_end;
60918275 635
89439d4f
MS
636 if (i->idx == IDX_FIRST) {
637 /* fast forward to the first occupied bucket */
638 if (h->has_indirect) {
639 i->idx = skip_free_buckets(h, h->indirect.idx_lowest_entry);
640 h->indirect.idx_lowest_entry = i->idx;
641 } else
642 i->idx = skip_free_buckets(h, 0);
643
644 if (i->idx == IDX_NIL)
645 goto at_end;
646 } else {
647 struct hashmap_base_entry *e;
648
649 assert(i->idx > 0);
60918275 650
89439d4f
MS
651 e = bucket_at(h, i->idx);
652 /*
653 * We allow removing the current entry while iterating, but removal may cause
654 * a backward shift. The next entry may thus move one bucket to the left.
655 * To detect when it happens, we remember the key pointer of the entry we were
656 * going to iterate next. If it does not match, there was a backward shift.
657 */
658 if (e->key != i->next_key)
659 e = bucket_at(h, --i->idx);
60918275 660
89439d4f
MS
661 assert(e->key == i->next_key);
662 }
663
664 idx = i->idx;
349cc4a5 665#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
666 i->prev_idx = idx;
667#endif
668
669 i->idx = skip_free_buckets(h, i->idx + 1);
670 if (i->idx != IDX_NIL)
671 i->next_key = bucket_at(h, i->idx)->key;
101d8e63 672 else
89439d4f
MS
673 i->idx = IDX_NIL;
674
675 return idx;
60918275 676
89439d4f
MS
677at_end:
678 i->idx = IDX_NIL;
679 return IDX_NIL;
60918275
LP
680}
681
89439d4f
MS
682static unsigned hashmap_iterate_entry(HashmapBase *h, Iterator *i) {
683 if (!h) {
684 i->idx = IDX_NIL;
685 return IDX_NIL;
686 }
101d8e63 687
349cc4a5 688#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
689 if (i->idx == IDX_FIRST) {
690 i->put_count = h->debug.put_count;
691 i->rem_count = h->debug.rem_count;
692 } else {
693 /* While iterating, must not add any new entries */
694 assert(i->put_count == h->debug.put_count);
695 /* ... or remove entries other than the current one */
696 assert(i->rem_count == h->debug.rem_count ||
697 (i->rem_count == h->debug.rem_count - 1 &&
698 i->prev_idx == h->debug.last_rem_idx));
699 /* Reset our removals counter */
700 i->rem_count = h->debug.rem_count;
701 }
702#endif
101d8e63 703
89439d4f
MS
704 return h->type == HASHMAP_TYPE_ORDERED ? hashmap_iterate_in_insertion_order((OrderedHashmap*) h, i)
705 : hashmap_iterate_in_internal_order(h, i);
706}
39c2a6f1 707
138f49e4 708bool _hashmap_iterate(HashmapBase *h, Iterator *i, void **value, const void **key) {
89439d4f
MS
709 struct hashmap_base_entry *e;
710 void *data;
711 unsigned idx;
712
713 idx = hashmap_iterate_entry(h, i);
714 if (idx == IDX_NIL) {
8927b1da
DH
715 if (value)
716 *value = NULL;
89439d4f
MS
717 if (key)
718 *key = NULL;
719
8927b1da 720 return false;
89439d4f
MS
721 }
722
723 e = bucket_at(h, idx);
724 data = entry_value(h, e);
8927b1da
DH
725 if (value)
726 *value = data;
89439d4f
MS
727 if (key)
728 *key = e->key;
729
8927b1da 730 return true;
101d8e63
LP
731}
732
89439d4f
MS
733#define HASHMAP_FOREACH_IDX(idx, h, i) \
734 for ((i) = ITERATOR_FIRST, (idx) = hashmap_iterate_entry((h), &(i)); \
735 (idx != IDX_NIL); \
736 (idx) = hashmap_iterate_entry((h), &(i)))
737
8a35af80 738IteratedCache* _hashmap_iterated_cache_new(HashmapBase *h) {
45ea84d8
VC
739 IteratedCache *cache;
740
741 assert(h);
742 assert(!h->cached);
743
744 if (h->cached)
745 return NULL;
746
747 cache = new0(IteratedCache, 1);
748 if (!cache)
749 return NULL;
750
751 cache->hashmap = h;
752 h->cached = true;
753
754 return cache;
755}
756
89439d4f
MS
757static void reset_direct_storage(HashmapBase *h) {
758 const struct hashmap_type_info *hi = &hashmap_type_info[h->type];
759 void *p;
760
761 assert(!h->has_indirect);
762
763 p = mempset(h->direct.storage, 0, hi->entry_size * hi->n_direct_buckets);
764 memset(p, DIB_RAW_INIT, sizeof(dib_raw_t) * hi->n_direct_buckets);
765}
766
ae0b700a
LP
767static void shared_hash_key_initialize(void) {
768 random_bytes(shared_hash_key, sizeof(shared_hash_key));
769}
770
8a35af80 771static struct HashmapBase* hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
89439d4f
MS
772 HashmapBase *h;
773 const struct hashmap_type_info *hi = &hashmap_type_info[type];
89439d4f 774
b01f3195 775 bool use_pool = mempool_enabled && mempool_enabled();
67f3c402 776
b01f3195 777 h = use_pool ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
60918275 778 if (!h)
89439d4f
MS
779 return NULL;
780
781 h->type = type;
b01f3195 782 h->from_pool = use_pool;
70b400d9 783 h->hash_ops = hash_ops ?: &trivial_hash_ops;
89439d4f
MS
784
785 if (type == HASHMAP_TYPE_ORDERED) {
786 OrderedHashmap *lh = (OrderedHashmap*)h;
787 lh->iterate_list_head = lh->iterate_list_tail = IDX_NIL;
788 }
789
790 reset_direct_storage(h);
60918275 791
ae0b700a
LP
792 static pthread_once_t once = PTHREAD_ONCE_INIT;
793 assert_se(pthread_once(&once, shared_hash_key_initialize) == 0);
89439d4f 794
349cc4a5 795#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
796 h->debug.func = func;
797 h->debug.file = file;
798 h->debug.line = line;
4f1b3061
TG
799 assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0);
800 LIST_PREPEND(debug_list, hashmap_debug_list, &h->debug);
801 assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
89439d4f
MS
802#endif
803
804 return h;
805}
60918275 806
138f49e4 807Hashmap *_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 808 return (Hashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
809}
810
138f49e4 811OrderedHashmap *_ordered_hashmap_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 812 return (OrderedHashmap*) hashmap_base_new(hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
813}
814
138f49e4 815Set *_set_new(const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 816 return (Set*) hashmap_base_new(hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
817}
818
819static int hashmap_base_ensure_allocated(HashmapBase **h, const struct hash_ops *hash_ops,
add74e89 820 enum HashmapType type HASHMAP_DEBUG_PARAMS) {
89439d4f
MS
821 HashmapBase *q;
822
823 assert(h);
824
825 if (*h)
826 return 0;
827
add74e89 828 q = hashmap_base_new(hash_ops, type HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
829 if (!q)
830 return -ENOMEM;
831
832 *h = q;
9ff7c5b0 833 return 1;
89439d4f
MS
834}
835
138f49e4 836int _hashmap_ensure_allocated(Hashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 837 return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_PLAIN HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
838}
839
138f49e4 840int _ordered_hashmap_ensure_allocated(OrderedHashmap **h, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 841 return hashmap_base_ensure_allocated((HashmapBase**)h, hash_ops, HASHMAP_TYPE_ORDERED HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
842}
843
138f49e4 844int _set_ensure_allocated(Set **s, const struct hash_ops *hash_ops HASHMAP_DEBUG_PARAMS) {
add74e89 845 return hashmap_base_ensure_allocated((HashmapBase**)s, hash_ops, HASHMAP_TYPE_SET HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
846}
847
1346c36d
SS
848int _hashmap_ensure_put(Hashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) {
849 int r;
850
851 r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS);
852 if (r < 0)
853 return r;
854
855 return hashmap_put(*h, key, value);
856}
857
b7847e05
SS
858int _ordered_hashmap_ensure_put(OrderedHashmap **h, const struct hash_ops *hash_ops, const void *key, void *value HASHMAP_DEBUG_PARAMS) {
859 int r;
860
861 r = _ordered_hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS);
862 if (r < 0)
863 return r;
864
865 return ordered_hashmap_put(*h, key, value);
866}
867
89439d4f
MS
868static void hashmap_free_no_clear(HashmapBase *h) {
869 assert(!h->has_indirect);
ee05335f 870 assert(h->n_direct_entries == 0);
89439d4f 871
349cc4a5 872#if ENABLE_DEBUG_HASHMAP
4f1b3061 873 assert_se(pthread_mutex_lock(&hashmap_debug_list_mutex) == 0);
89439d4f 874 LIST_REMOVE(debug_list, hashmap_debug_list, &h->debug);
4f1b3061 875 assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
89439d4f 876#endif
45fa9e29 877
205c085b
LP
878 if (h->from_pool) {
879 /* Ensure that the object didn't get migrated between threads. */
880 assert_se(is_main_thread());
89439d4f 881 mempool_free_tile(hashmap_type_info[h->type].mempool, h);
205c085b 882 } else
39c2a6f1 883 free(h);
60918275
LP
884}
885
8a35af80 886HashmapBase* _hashmap_free(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value) {
cfe561a4 887 if (h) {
138f49e4 888 _hashmap_clear(h, default_free_key, default_free_value);
cfe561a4
DH
889 hashmap_free_no_clear(h);
890 }
89439d4f 891
cfe561a4 892 return NULL;
89439d4f
MS
893}
894
138f49e4 895void _hashmap_clear(HashmapBase *h, free_func_t default_free_key, free_func_t default_free_value) {
59a5cda7
YW
896 free_func_t free_key, free_value;
897 if (!h)
898 return;
67f3c402 899
59a5cda7
YW
900 free_key = h->hash_ops->free_key ?: default_free_key;
901 free_value = h->hash_ops->free_value ?: default_free_value;
67f3c402 902
59a5cda7 903 if (free_key || free_value) {
449ddb2d 904
c380b84d
LP
905 /* If destructor calls are defined, let's destroy things defensively: let's take the item out of the
906 * hash table, and only then call the destructor functions. If these destructors then try to unregister
907 * themselves from our hash table a second time, the entry is already gone. */
908
138f49e4 909 while (_hashmap_size(h) > 0) {
ca323715
TH
910 void *k = NULL;
911 void *v;
c380b84d 912
138f49e4 913 v = _hashmap_first_key_and_value(h, true, &k);
fabe5c0e 914
59a5cda7 915 if (free_key)
c380b84d 916 free_key(k);
fabe5c0e 917
59a5cda7 918 if (free_value)
c380b84d 919 free_value(v);
59a5cda7 920 }
cfe561a4 921 }
fabe5c0e 922
89439d4f
MS
923 if (h->has_indirect) {
924 free(h->indirect.storage);
925 h->has_indirect = false;
926 }
927
928 h->n_direct_entries = 0;
929 reset_direct_storage(h);
930
931 if (h->type == HASHMAP_TYPE_ORDERED) {
932 OrderedHashmap *lh = (OrderedHashmap*) h;
933 lh->iterate_list_head = lh->iterate_list_tail = IDX_NIL;
934 }
84dcca75
VC
935
936 base_set_dirty(h);
11dd41ce
LP
937}
938
89439d4f
MS
939static int resize_buckets(HashmapBase *h, unsigned entries_add);
940
941/*
942 * Finds an empty bucket to put an entry into, starting the scan at 'idx'.
943 * Performs Robin Hood swaps as it goes. The entry to put must be placed
944 * by the caller into swap slot IDX_PUT.
945 * If used for in-place resizing, may leave a displaced entry in swap slot
946 * IDX_PUT. Caller must rehash it next.
947 * Returns: true if it left a displaced entry to rehash next in IDX_PUT,
948 * false otherwise.
949 */
950static bool hashmap_put_robin_hood(HashmapBase *h, unsigned idx,
951 struct swap_entries *swap) {
952 dib_raw_t raw_dib, *dibs;
953 unsigned dib, distance;
954
349cc4a5 955#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
956 h->debug.put_count++;
957#endif
958
959 dibs = dib_raw_ptr(h);
960
961 for (distance = 0; ; distance++) {
962 raw_dib = dibs[idx];
3742095b 963 if (IN_SET(raw_dib, DIB_RAW_FREE, DIB_RAW_REHASH)) {
89439d4f
MS
964 if (raw_dib == DIB_RAW_REHASH)
965 bucket_move_entry(h, swap, idx, IDX_TMP);
966
967 if (h->has_indirect && h->indirect.idx_lowest_entry > idx)
968 h->indirect.idx_lowest_entry = idx;
60918275 969
89439d4f
MS
970 bucket_set_dib(h, idx, distance);
971 bucket_move_entry(h, swap, IDX_PUT, idx);
972 if (raw_dib == DIB_RAW_REHASH) {
973 bucket_move_entry(h, swap, IDX_TMP, IDX_PUT);
974 return true;
975 }
60918275 976
89439d4f
MS
977 return false;
978 }
979
980 dib = bucket_calculate_dib(h, idx, raw_dib);
981
982 if (dib < distance) {
983 /* Found a wealthier entry. Go Robin Hood! */
89439d4f
MS
984 bucket_set_dib(h, idx, distance);
985
986 /* swap the entries */
987 bucket_move_entry(h, swap, idx, IDX_TMP);
988 bucket_move_entry(h, swap, IDX_PUT, idx);
989 bucket_move_entry(h, swap, IDX_TMP, IDX_PUT);
990
991 distance = dib;
992 }
993
994 idx = next_idx(h, idx);
995 }
60918275
LP
996}
997
89439d4f
MS
998/*
999 * Puts an entry into a hashmap, boldly - no check whether key already exists.
1000 * The caller must place the entry (only its key and value, not link indexes)
1001 * in swap slot IDX_PUT.
1002 * Caller must ensure: the key does not exist yet in the hashmap.
1003 * that resize is not needed if !may_resize.
1004 * Returns: 1 if entry was put successfully.
1005 * -ENOMEM if may_resize==true and resize failed with -ENOMEM.
1006 * Cannot return -ENOMEM if !may_resize.
1007 */
1008static int hashmap_base_put_boldly(HashmapBase *h, unsigned idx,
1009 struct swap_entries *swap, bool may_resize) {
1010 struct ordered_hashmap_entry *new_entry;
1011 int r;
1012
1013 assert(idx < n_buckets(h));
1014
1015 new_entry = bucket_at_swap(swap, IDX_PUT);
1016
1017 if (may_resize) {
1018 r = resize_buckets(h, 1);
1019 if (r < 0)
1020 return r;
1021 if (r > 0)
1022 idx = bucket_hash(h, new_entry->p.b.key);
1023 }
1024 assert(n_entries(h) < n_buckets(h));
1025
1026 if (h->type == HASHMAP_TYPE_ORDERED) {
1027 OrderedHashmap *lh = (OrderedHashmap*) h;
1028
1029 new_entry->iterate_next = IDX_NIL;
1030 new_entry->iterate_previous = lh->iterate_list_tail;
1031
1032 if (lh->iterate_list_tail != IDX_NIL) {
1033 struct ordered_hashmap_entry *old_tail;
1034
1035 old_tail = ordered_bucket_at(lh, lh->iterate_list_tail);
1036 assert(old_tail->iterate_next == IDX_NIL);
1037 old_tail->iterate_next = IDX_PUT;
1038 }
1039
1040 lh->iterate_list_tail = IDX_PUT;
1041 if (lh->iterate_list_head == IDX_NIL)
1042 lh->iterate_list_head = IDX_PUT;
1043 }
1044
1045 assert_se(hashmap_put_robin_hood(h, idx, swap) == false);
1046
1047 n_entries_inc(h);
349cc4a5 1048#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
1049 h->debug.max_entries = MAX(h->debug.max_entries, n_entries(h));
1050#endif
1051
84dcca75
VC
1052 base_set_dirty(h);
1053
89439d4f
MS
1054 return 1;
1055}
1056#define hashmap_put_boldly(h, idx, swap, may_resize) \
1057 hashmap_base_put_boldly(HASHMAP_BASE(h), idx, swap, may_resize)
1058
1059/*
1060 * Returns 0 if resize is not needed.
f131770b 1061 * 1 if successfully resized.
89439d4f
MS
1062 * -ENOMEM on allocation failure.
1063 */
1064static int resize_buckets(HashmapBase *h, unsigned entries_add) {
1065 struct swap_entries swap;
1a39bc8c 1066 void *new_storage;
89439d4f
MS
1067 dib_raw_t *old_dibs, *new_dibs;
1068 const struct hashmap_type_info *hi;
1069 unsigned idx, optimal_idx;
1070 unsigned old_n_buckets, new_n_buckets, n_rehashed, new_n_entries;
1071 uint8_t new_shift;
1072 bool rehash_next;
45fa9e29
LP
1073
1074 assert(h);
1075
89439d4f
MS
1076 hi = &hashmap_type_info[h->type];
1077 new_n_entries = n_entries(h) + entries_add;
e4c691b5
MS
1078
1079 /* overflow? */
89439d4f 1080 if (_unlikely_(new_n_entries < entries_add))
e4c691b5
MS
1081 return -ENOMEM;
1082
89439d4f
MS
1083 /* For direct storage we allow 100% load, because it's tiny. */
1084 if (!h->has_indirect && new_n_entries <= hi->n_direct_buckets)
9700d698 1085 return 0;
45fa9e29 1086
89439d4f
MS
1087 /*
1088 * Load factor = n/m = 1 - (1/INV_KEEP_FREE).
1089 * From it follows: m = n + n/(INV_KEEP_FREE - 1)
1090 */
1091 new_n_buckets = new_n_entries + new_n_entries / (INV_KEEP_FREE - 1);
1092 /* overflow? */
1093 if (_unlikely_(new_n_buckets < new_n_entries))
9700d698 1094 return -ENOMEM;
45fa9e29 1095
89439d4f
MS
1096 if (_unlikely_(new_n_buckets > UINT_MAX / (hi->entry_size + sizeof(dib_raw_t))))
1097 return -ENOMEM;
a3b6fafe 1098
89439d4f 1099 old_n_buckets = n_buckets(h);
45fa9e29 1100
89439d4f
MS
1101 if (_likely_(new_n_buckets <= old_n_buckets))
1102 return 0;
45fa9e29 1103
89439d4f
MS
1104 new_shift = log2u_round_up(MAX(
1105 new_n_buckets * (hi->entry_size + sizeof(dib_raw_t)),
1106 2 * sizeof(struct direct_storage)));
45fa9e29 1107
89439d4f
MS
1108 /* Realloc storage (buckets and DIB array). */
1109 new_storage = realloc(h->has_indirect ? h->indirect.storage : NULL,
1110 1U << new_shift);
1111 if (!new_storage)
1112 return -ENOMEM;
45fa9e29 1113
89439d4f
MS
1114 /* Must upgrade direct to indirect storage. */
1115 if (!h->has_indirect) {
1116 memcpy(new_storage, h->direct.storage,
1117 old_n_buckets * (hi->entry_size + sizeof(dib_raw_t)));
1118 h->indirect.n_entries = h->n_direct_entries;
1119 h->indirect.idx_lowest_entry = 0;
1120 h->n_direct_entries = 0;
1121 }
45fa9e29 1122
89439d4f
MS
1123 /* Get a new hash key. If we've just upgraded to indirect storage,
1124 * allow reusing a previously generated key. It's still a different key
1125 * from the shared one that we used for direct storage. */
1126 get_hash_key(h->indirect.hash_key, !h->has_indirect);
1127
1128 h->has_indirect = true;
1129 h->indirect.storage = new_storage;
1130 h->indirect.n_buckets = (1U << new_shift) /
1131 (hi->entry_size + sizeof(dib_raw_t));
1132
1a39bc8c 1133 old_dibs = (dib_raw_t*)((uint8_t*) new_storage + hi->entry_size * old_n_buckets);
89439d4f
MS
1134 new_dibs = dib_raw_ptr(h);
1135
1136 /*
1137 * Move the DIB array to the new place, replacing valid DIB values with
1138 * DIB_RAW_REHASH to indicate all of the used buckets need rehashing.
1139 * Note: Overlap is not possible, because we have at least doubled the
1140 * number of buckets and dib_raw_t is smaller than any entry type.
1141 */
1142 for (idx = 0; idx < old_n_buckets; idx++) {
1143 assert(old_dibs[idx] != DIB_RAW_REHASH);
1144 new_dibs[idx] = old_dibs[idx] == DIB_RAW_FREE ? DIB_RAW_FREE
1145 : DIB_RAW_REHASH;
45fa9e29
LP
1146 }
1147
89439d4f 1148 /* Zero the area of newly added entries (including the old DIB area) */
eccaf899 1149 memzero(bucket_at(h, old_n_buckets),
89439d4f 1150 (n_buckets(h) - old_n_buckets) * hi->entry_size);
45fa9e29 1151
89439d4f
MS
1152 /* The upper half of the new DIB array needs initialization */
1153 memset(&new_dibs[old_n_buckets], DIB_RAW_INIT,
1154 (n_buckets(h) - old_n_buckets) * sizeof(dib_raw_t));
9bf3b535 1155
89439d4f
MS
1156 /* Rehash entries that need it */
1157 n_rehashed = 0;
1158 for (idx = 0; idx < old_n_buckets; idx++) {
1159 if (new_dibs[idx] != DIB_RAW_REHASH)
1160 continue;
45fa9e29 1161
89439d4f 1162 optimal_idx = bucket_hash(h, bucket_at(h, idx)->key);
45fa9e29 1163
89439d4f
MS
1164 /*
1165 * Not much to do if by luck the entry hashes to its current
1166 * location. Just set its DIB.
1167 */
1168 if (optimal_idx == idx) {
1169 new_dibs[idx] = 0;
1170 n_rehashed++;
1171 continue;
1172 }
1173
1174 new_dibs[idx] = DIB_RAW_FREE;
1175 bucket_move_entry(h, &swap, idx, IDX_PUT);
1176 /* bucket_move_entry does not clear the source */
eccaf899 1177 memzero(bucket_at(h, idx), hi->entry_size);
89439d4f
MS
1178
1179 do {
1180 /*
1181 * Find the new bucket for the current entry. This may make
1182 * another entry homeless and load it into IDX_PUT.
1183 */
1184 rehash_next = hashmap_put_robin_hood(h, optimal_idx, &swap);
1185 n_rehashed++;
1186
1187 /* Did the current entry displace another one? */
1188 if (rehash_next)
1189 optimal_idx = bucket_hash(h, bucket_at_swap(&swap, IDX_PUT)->p.b.key);
1190 } while (rehash_next);
1191 }
60918275 1192
3b2b3d29 1193 assert_se(n_rehashed == n_entries(h));
60918275 1194
89439d4f
MS
1195 return 1;
1196}
45fa9e29 1197
89439d4f
MS
1198/*
1199 * Finds an entry with a matching key
1200 * Returns: index of the found entry, or IDX_NIL if not found.
1201 */
1202static unsigned base_bucket_scan(HashmapBase *h, unsigned idx, const void *key) {
1203 struct hashmap_base_entry *e;
1204 unsigned dib, distance;
1205 dib_raw_t *dibs = dib_raw_ptr(h);
39c2a6f1 1206
89439d4f 1207 assert(idx < n_buckets(h));
60918275 1208
89439d4f
MS
1209 for (distance = 0; ; distance++) {
1210 if (dibs[idx] == DIB_RAW_FREE)
1211 return IDX_NIL;
60918275 1212
89439d4f 1213 dib = bucket_calculate_dib(h, idx, dibs[idx]);
60918275 1214
89439d4f
MS
1215 if (dib < distance)
1216 return IDX_NIL;
1217 if (dib == distance) {
1218 e = bucket_at(h, idx);
1219 if (h->hash_ops->compare(e->key, key) == 0)
1220 return idx;
1221 }
1222
1223 idx = next_idx(h, idx);
1224 }
60918275 1225}
89439d4f 1226#define bucket_scan(h, idx, key) base_bucket_scan(HASHMAP_BASE(h), idx, key)
60918275 1227
923041cb 1228int hashmap_put(Hashmap *h, const void *key, void *value) {
89439d4f
MS
1229 struct swap_entries swap;
1230 struct plain_hashmap_entry *e;
1231 unsigned hash, idx;
923041cb
MS
1232
1233 assert(h);
1234
1235 hash = bucket_hash(h, key);
89439d4f
MS
1236 idx = bucket_scan(h, hash, key);
1237 if (idx != IDX_NIL) {
1238 e = plain_bucket_at(h, idx);
923041cb
MS
1239 if (e->value == value)
1240 return 0;
1241 return -EEXIST;
1242 }
1243
89439d4f
MS
1244 e = &bucket_at_swap(&swap, IDX_PUT)->p;
1245 e->b.key = key;
1246 e->value = value;
1247 return hashmap_put_boldly(h, hash, &swap, true);
1248}
1249
1250int set_put(Set *s, const void *key) {
1251 struct swap_entries swap;
1252 struct hashmap_base_entry *e;
1253 unsigned hash, idx;
1254
1255 assert(s);
1256
1257 hash = bucket_hash(s, key);
1258 idx = bucket_scan(s, hash, key);
1259 if (idx != IDX_NIL)
1260 return 0;
1261
1262 e = &bucket_at_swap(&swap, IDX_PUT)->p.b;
1263 e->key = key;
1264 return hashmap_put_boldly(s, hash, &swap, true);
923041cb
MS
1265}
1266
0f9ccd95
ZJS
1267int _set_ensure_put(Set **s, const struct hash_ops *hash_ops, const void *key HASHMAP_DEBUG_PARAMS) {
1268 int r;
1269
1270 r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS);
1271 if (r < 0)
1272 return r;
1273
1274 return set_put(*s, key);
1275}
1276
fcc1d031
ZJS
1277int _set_ensure_consume(Set **s, const struct hash_ops *hash_ops, void *key HASHMAP_DEBUG_PARAMS) {
1278 int r;
1279
1280 r = _set_ensure_put(s, hash_ops, key HASHMAP_DEBUG_PASS_ARGS);
1281 if (r <= 0) {
1282 if (hash_ops && hash_ops->free_key)
1283 hash_ops->free_key(key);
1284 else
1285 free(key);
1286 }
1287
1288 return r;
1289}
1290
3158713e 1291int hashmap_replace(Hashmap *h, const void *key, void *value) {
89439d4f
MS
1292 struct swap_entries swap;
1293 struct plain_hashmap_entry *e;
1294 unsigned hash, idx;
3158713e
LP
1295
1296 assert(h);
1297
a3b6fafe 1298 hash = bucket_hash(h, key);
89439d4f
MS
1299 idx = bucket_scan(h, hash, key);
1300 if (idx != IDX_NIL) {
1301 e = plain_bucket_at(h, idx);
349cc4a5 1302#if ENABLE_DEBUG_HASHMAP
89439d4f
MS
1303 /* Although the key is equal, the key pointer may have changed,
1304 * and this would break our assumption for iterating. So count
1305 * this operation as incompatible with iteration. */
1306 if (e->b.key != key) {
1307 h->b.debug.put_count++;
1308 h->b.debug.rem_count++;
1309 h->b.debug.last_rem_idx = idx;
1310 }
1311#endif
1312 e->b.key = key;
3158713e 1313 e->value = value;
84dcca75
VC
1314 hashmap_set_dirty(h);
1315
3158713e
LP
1316 return 0;
1317 }
1318
89439d4f
MS
1319 e = &bucket_at_swap(&swap, IDX_PUT)->p;
1320 e->b.key = key;
1321 e->value = value;
1322 return hashmap_put_boldly(h, hash, &swap, true);
3158713e
LP
1323}
1324
d99ae53a 1325int hashmap_update(Hashmap *h, const void *key, void *value) {
89439d4f
MS
1326 struct plain_hashmap_entry *e;
1327 unsigned hash, idx;
d99ae53a
LP
1328
1329 assert(h);
1330
a3b6fafe 1331 hash = bucket_hash(h, key);
89439d4f
MS
1332 idx = bucket_scan(h, hash, key);
1333 if (idx == IDX_NIL)
d99ae53a
LP
1334 return -ENOENT;
1335
89439d4f 1336 e = plain_bucket_at(h, idx);
d99ae53a 1337 e->value = value;
84dcca75
VC
1338 hashmap_set_dirty(h);
1339
d99ae53a
LP
1340 return 0;
1341}
1342
8a35af80 1343void* _hashmap_get(HashmapBase *h, const void *key) {
89439d4f
MS
1344 struct hashmap_base_entry *e;
1345 unsigned hash, idx;
60918275
LP
1346
1347 if (!h)
1348 return NULL;
1349
a3b6fafe 1350 hash = bucket_hash(h, key);
89439d4f
MS
1351 idx = bucket_scan(h, hash, key);
1352 if (idx == IDX_NIL)
60918275
LP
1353 return NULL;
1354
89439d4f
MS
1355 e = bucket_at(h, idx);
1356 return entry_value(h, e);
60918275
LP
1357}
1358
8a35af80 1359void* hashmap_get2(Hashmap *h, const void *key, void **key2) {
89439d4f
MS
1360 struct plain_hashmap_entry *e;
1361 unsigned hash, idx;
d99ae53a
LP
1362
1363 if (!h)
1364 return NULL;
1365
a3b6fafe 1366 hash = bucket_hash(h, key);
89439d4f
MS
1367 idx = bucket_scan(h, hash, key);
1368 if (idx == IDX_NIL)
d99ae53a
LP
1369 return NULL;
1370
89439d4f 1371 e = plain_bucket_at(h, idx);
d99ae53a 1372 if (key2)
89439d4f 1373 *key2 = (void*) e->b.key;
d99ae53a
LP
1374
1375 return e->value;
1376}
1377
138f49e4 1378bool _hashmap_contains(HashmapBase *h, const void *key) {
96342de6 1379 unsigned hash;
96342de6
LN
1380
1381 if (!h)
1382 return false;
1383
a3b6fafe 1384 hash = bucket_hash(h, key);
89439d4f 1385 return bucket_scan(h, hash, key) != IDX_NIL;
96342de6
LN
1386}
1387
8a35af80 1388void* _hashmap_remove(HashmapBase *h, const void *key) {
89439d4f
MS
1389 struct hashmap_base_entry *e;
1390 unsigned hash, idx;
60918275
LP
1391 void *data;
1392
1393 if (!h)
1394 return NULL;
1395
a3b6fafe 1396 hash = bucket_hash(h, key);
89439d4f
MS
1397 idx = bucket_scan(h, hash, key);
1398 if (idx == IDX_NIL)
60918275
LP
1399 return NULL;
1400
89439d4f
MS
1401 e = bucket_at(h, idx);
1402 data = entry_value(h, e);
1403 remove_entry(h, idx);
60918275
LP
1404
1405 return data;
1406}
1407
8a35af80 1408void* hashmap_remove2(Hashmap *h, const void *key, void **rkey) {
89439d4f
MS
1409 struct plain_hashmap_entry *e;
1410 unsigned hash, idx;
c582a3b3
LP
1411 void *data;
1412
1413 if (!h) {
1414 if (rkey)
1415 *rkey = NULL;
1416 return NULL;
1417 }
1418
1419 hash = bucket_hash(h, key);
89439d4f
MS
1420 idx = bucket_scan(h, hash, key);
1421 if (idx == IDX_NIL) {
c582a3b3
LP
1422 if (rkey)
1423 *rkey = NULL;
1424 return NULL;
1425 }
1426
89439d4f 1427 e = plain_bucket_at(h, idx);
c582a3b3
LP
1428 data = e->value;
1429 if (rkey)
89439d4f 1430 *rkey = (void*) e->b.key;
c582a3b3 1431
89439d4f 1432 remove_entry(h, idx);
c582a3b3
LP
1433
1434 return data;
1435}
1436
101d8e63 1437int hashmap_remove_and_put(Hashmap *h, const void *old_key, const void *new_key, void *value) {
89439d4f
MS
1438 struct swap_entries swap;
1439 struct plain_hashmap_entry *e;
1440 unsigned old_hash, new_hash, idx;
101d8e63
LP
1441
1442 if (!h)
1443 return -ENOENT;
1444
a3b6fafe 1445 old_hash = bucket_hash(h, old_key);
89439d4f
MS
1446 idx = bucket_scan(h, old_hash, old_key);
1447 if (idx == IDX_NIL)
101d8e63
LP
1448 return -ENOENT;
1449
a3b6fafe 1450 new_hash = bucket_hash(h, new_key);
89439d4f 1451 if (bucket_scan(h, new_hash, new_key) != IDX_NIL)
101d8e63
LP
1452 return -EEXIST;
1453
89439d4f 1454 remove_entry(h, idx);
101d8e63 1455
89439d4f
MS
1456 e = &bucket_at_swap(&swap, IDX_PUT)->p;
1457 e->b.key = new_key;
101d8e63 1458 e->value = value;
89439d4f
MS
1459 assert_se(hashmap_put_boldly(h, new_hash, &swap, false) == 1);
1460
1461 return 0;
1462}
1463
1464int set_remove_and_put(Set *s, const void *old_key, const void *new_key) {
1465 struct swap_entries swap;
1466 struct hashmap_base_entry *e;
1467 unsigned old_hash, new_hash, idx;
101d8e63 1468
89439d4f
MS
1469 if (!s)
1470 return -ENOENT;
1471
1472 old_hash = bucket_hash(s, old_key);
1473 idx = bucket_scan(s, old_hash, old_key);
1474 if (idx == IDX_NIL)
1475 return -ENOENT;
1476
1477 new_hash = bucket_hash(s, new_key);
1478 if (bucket_scan(s, new_hash, new_key) != IDX_NIL)
1479 return -EEXIST;
1480
1481 remove_entry(s, idx);
1482
1483 e = &bucket_at_swap(&swap, IDX_PUT)->p.b;
1484 e->key = new_key;
1485 assert_se(hashmap_put_boldly(s, new_hash, &swap, false) == 1);
101d8e63
LP
1486
1487 return 0;
1488}
1489
8fe914ec 1490int hashmap_remove_and_replace(Hashmap *h, const void *old_key, const void *new_key, void *value) {
89439d4f
MS
1491 struct swap_entries swap;
1492 struct plain_hashmap_entry *e;
1493 unsigned old_hash, new_hash, idx_old, idx_new;
8fe914ec
LP
1494
1495 if (!h)
1496 return -ENOENT;
1497
a3b6fafe 1498 old_hash = bucket_hash(h, old_key);
89439d4f
MS
1499 idx_old = bucket_scan(h, old_hash, old_key);
1500 if (idx_old == IDX_NIL)
8fe914ec
LP
1501 return -ENOENT;
1502
89439d4f 1503 old_key = bucket_at(HASHMAP_BASE(h), idx_old)->key;
8fe914ec 1504
89439d4f
MS
1505 new_hash = bucket_hash(h, new_key);
1506 idx_new = bucket_scan(h, new_hash, new_key);
1507 if (idx_new != IDX_NIL)
1508 if (idx_old != idx_new) {
1509 remove_entry(h, idx_new);
1510 /* Compensate for a possible backward shift. */
1511 if (old_key != bucket_at(HASHMAP_BASE(h), idx_old)->key)
1512 idx_old = prev_idx(HASHMAP_BASE(h), idx_old);
1513 assert(old_key == bucket_at(HASHMAP_BASE(h), idx_old)->key);
1514 }
1515
1516 remove_entry(h, idx_old);
1517
1518 e = &bucket_at_swap(&swap, IDX_PUT)->p;
1519 e->b.key = new_key;
8fe914ec 1520 e->value = value;
89439d4f 1521 assert_se(hashmap_put_boldly(h, new_hash, &swap, false) == 1);
8fe914ec
LP
1522
1523 return 0;
1524}
1525
8a35af80 1526void* _hashmap_remove_value(HashmapBase *h, const void *key, void *value) {
c380b84d 1527 struct hashmap_base_entry *e;
89439d4f 1528 unsigned hash, idx;
3158713e
LP
1529
1530 if (!h)
1531 return NULL;
1532
a3b6fafe 1533 hash = bucket_hash(h, key);
89439d4f
MS
1534 idx = bucket_scan(h, hash, key);
1535 if (idx == IDX_NIL)
3158713e
LP
1536 return NULL;
1537
c380b84d
LP
1538 e = bucket_at(h, idx);
1539 if (entry_value(h, e) != value)
3158713e
LP
1540 return NULL;
1541
89439d4f 1542 remove_entry(h, idx);
3158713e
LP
1543
1544 return value;
1545}
1546
89439d4f
MS
1547static unsigned find_first_entry(HashmapBase *h) {
1548 Iterator i = ITERATOR_FIRST;
60918275 1549
89439d4f
MS
1550 if (!h || !n_entries(h))
1551 return IDX_NIL;
60918275 1552
89439d4f 1553 return hashmap_iterate_entry(h, &i);
60918275
LP
1554}
1555
8a35af80 1556void* _hashmap_first_key_and_value(HashmapBase *h, bool remove, void **ret_key) {
89439d4f 1557 struct hashmap_base_entry *e;
7ef670c3 1558 void *key, *data;
89439d4f 1559 unsigned idx;
60918275 1560
89439d4f 1561 idx = find_first_entry(h);
51c682df
TH
1562 if (idx == IDX_NIL) {
1563 if (ret_key)
1564 *ret_key = NULL;
60918275 1565 return NULL;
51c682df 1566 }
60918275 1567
89439d4f 1568 e = bucket_at(h, idx);
7ef670c3 1569 key = (void*) e->key;
89439d4f 1570 data = entry_value(h, e);
60918275 1571
7ef670c3
YW
1572 if (remove)
1573 remove_entry(h, idx);
60918275 1574
7ef670c3
YW
1575 if (ret_key)
1576 *ret_key = key;
22be093f 1577
7ef670c3 1578 return data;
22be093f
LP
1579}
1580
138f49e4 1581unsigned _hashmap_size(HashmapBase *h) {
60918275
LP
1582 if (!h)
1583 return 0;
1584
89439d4f 1585 return n_entries(h);
60918275
LP
1586}
1587
138f49e4 1588unsigned _hashmap_buckets(HashmapBase *h) {
45fa9e29
LP
1589 if (!h)
1590 return 0;
1591
89439d4f 1592 return n_buckets(h);
45fa9e29
LP
1593}
1594
138f49e4 1595int _hashmap_merge(Hashmap *h, Hashmap *other) {
89439d4f
MS
1596 Iterator i;
1597 unsigned idx;
60918275 1598
89439d4f 1599 assert(h);
60918275 1600
89439d4f
MS
1601 HASHMAP_FOREACH_IDX(idx, HASHMAP_BASE(other), i) {
1602 struct plain_hashmap_entry *pe = plain_bucket_at(other, idx);
1603 int r;
91cdde8a 1604
89439d4f
MS
1605 r = hashmap_put(h, pe->b.key, pe->value);
1606 if (r < 0 && r != -EEXIST)
1607 return r;
1608 }
91cdde8a 1609
89439d4f
MS
1610 return 0;
1611}
91cdde8a 1612
89439d4f
MS
1613int set_merge(Set *s, Set *other) {
1614 Iterator i;
1615 unsigned idx;
91cdde8a 1616
89439d4f
MS
1617 assert(s);
1618
1619 HASHMAP_FOREACH_IDX(idx, HASHMAP_BASE(other), i) {
1620 struct set_entry *se = set_bucket_at(other, idx);
91cdde8a
LP
1621 int r;
1622
89439d4f
MS
1623 r = set_put(s, se->b.key);
1624 if (r < 0)
a3b6fafe 1625 return r;
91cdde8a
LP
1626 }
1627
1628 return 0;
1629}
1630
138f49e4 1631int _hashmap_reserve(HashmapBase *h, unsigned entries_add) {
e4c691b5
MS
1632 int r;
1633
1634 assert(h);
1635
1636 r = resize_buckets(h, entries_add);
1637 if (r < 0)
1638 return r;
1639
1640 return 0;
1641}
1642
89439d4f
MS
1643/*
1644 * The same as hashmap_merge(), but every new item from other is moved to h.
1645 * Keys already in h are skipped and stay in other.
1646 * Returns: 0 on success.
1647 * -ENOMEM on alloc failure, in which case no move has been done.
1648 */
138f49e4 1649int _hashmap_move(HashmapBase *h, HashmapBase *other) {
89439d4f
MS
1650 struct swap_entries swap;
1651 struct hashmap_base_entry *e, *n;
1652 Iterator i;
1653 unsigned idx;
1654 int r;
101d8e63
LP
1655
1656 assert(h);
1657
101d8e63 1658 if (!other)
7ad63f57 1659 return 0;
101d8e63 1660
89439d4f
MS
1661 assert(other->type == h->type);
1662
1663 /*
1664 * This reserves buckets for the worst case, where none of other's
1665 * entries are yet present in h. This is preferable to risking
1666 * an allocation failure in the middle of the moving and having to
1667 * rollback or return a partial result.
1668 */
1669 r = resize_buckets(h, n_entries(other));
1670 if (r < 0)
1671 return r;
101d8e63 1672
89439d4f
MS
1673 HASHMAP_FOREACH_IDX(idx, other, i) {
1674 unsigned h_hash;
101d8e63 1675
89439d4f 1676 e = bucket_at(other, idx);
a3b6fafe 1677 h_hash = bucket_hash(h, e->key);
89439d4f 1678 if (bucket_scan(h, h_hash, e->key) != IDX_NIL)
101d8e63
LP
1679 continue;
1680
89439d4f
MS
1681 n = &bucket_at_swap(&swap, IDX_PUT)->p.b;
1682 n->key = e->key;
1683 if (h->type != HASHMAP_TYPE_SET)
1684 ((struct plain_hashmap_entry*) n)->value =
1685 ((struct plain_hashmap_entry*) e)->value;
1686 assert_se(hashmap_put_boldly(h, h_hash, &swap, false) == 1);
1687
1688 remove_entry(other, idx);
101d8e63 1689 }
7ad63f57
MS
1690
1691 return 0;
101d8e63
LP
1692}
1693
138f49e4 1694int _hashmap_move_one(HashmapBase *h, HashmapBase *other, const void *key) {
89439d4f
MS
1695 struct swap_entries swap;
1696 unsigned h_hash, other_hash, idx;
1697 struct hashmap_base_entry *e, *n;
1698 int r;
101d8e63 1699
101d8e63
LP
1700 assert(h);
1701
a3b6fafe 1702 h_hash = bucket_hash(h, key);
89439d4f 1703 if (bucket_scan(h, h_hash, key) != IDX_NIL)
101d8e63
LP
1704 return -EEXIST;
1705
bf3d3e2b
MS
1706 if (!other)
1707 return -ENOENT;
1708
89439d4f
MS
1709 assert(other->type == h->type);
1710
a3b6fafe 1711 other_hash = bucket_hash(other, key);
89439d4f
MS
1712 idx = bucket_scan(other, other_hash, key);
1713 if (idx == IDX_NIL)
101d8e63
LP
1714 return -ENOENT;
1715
89439d4f
MS
1716 e = bucket_at(other, idx);
1717
1718 n = &bucket_at_swap(&swap, IDX_PUT)->p.b;
1719 n->key = e->key;
1720 if (h->type != HASHMAP_TYPE_SET)
1721 ((struct plain_hashmap_entry*) n)->value =
1722 ((struct plain_hashmap_entry*) e)->value;
1723 r = hashmap_put_boldly(h, h_hash, &swap, true);
1724 if (r < 0)
1725 return r;
101d8e63 1726
89439d4f 1727 remove_entry(other, idx);
101d8e63
LP
1728 return 0;
1729}
1730
8a35af80 1731HashmapBase* _hashmap_copy(HashmapBase *h HASHMAP_DEBUG_PARAMS) {
89439d4f
MS
1732 HashmapBase *copy;
1733 int r;
91cdde8a
LP
1734
1735 assert(h);
1736
add74e89 1737 copy = hashmap_base_new(h->hash_ops, h->type HASHMAP_DEBUG_PASS_ARGS);
45fa9e29 1738 if (!copy)
91cdde8a
LP
1739 return NULL;
1740
89439d4f
MS
1741 switch (h->type) {
1742 case HASHMAP_TYPE_PLAIN:
1743 case HASHMAP_TYPE_ORDERED:
1744 r = hashmap_merge((Hashmap*)copy, (Hashmap*)h);
1745 break;
1746 case HASHMAP_TYPE_SET:
1747 r = set_merge((Set*)copy, (Set*)h);
1748 break;
1749 default:
04499a70 1750 assert_not_reached();
89439d4f
MS
1751 }
1752
add74e89
ZJS
1753 if (r < 0)
1754 return _hashmap_free(copy, false, false);
91cdde8a
LP
1755
1756 return copy;
1757}
db1413d7 1758
8a35af80 1759char** _hashmap_get_strv(HashmapBase *h) {
db1413d7 1760 char **sv;
89439d4f
MS
1761 Iterator i;
1762 unsigned idx, n;
db1413d7 1763
107e2163
LP
1764 if (!h)
1765 return new0(char*, 1);
1766
89439d4f 1767 sv = new(char*, n_entries(h)+1);
729e3769 1768 if (!sv)
db1413d7
KS
1769 return NULL;
1770
1771 n = 0;
89439d4f
MS
1772 HASHMAP_FOREACH_IDX(idx, h, i)
1773 sv[n++] = entry_value(h, bucket_at(h, idx));
db1413d7
KS
1774 sv[n] = NULL;
1775
1776 return sv;
1777}
3c1668da 1778
8a35af80 1779void* ordered_hashmap_next(OrderedHashmap *h, const void *key) {
89439d4f
MS
1780 struct ordered_hashmap_entry *e;
1781 unsigned hash, idx;
3c1668da 1782
3c1668da
LP
1783 if (!h)
1784 return NULL;
1785
a3b6fafe 1786 hash = bucket_hash(h, key);
89439d4f
MS
1787 idx = bucket_scan(h, hash, key);
1788 if (idx == IDX_NIL)
3c1668da
LP
1789 return NULL;
1790
89439d4f
MS
1791 e = ordered_bucket_at(h, idx);
1792 if (e->iterate_next == IDX_NIL)
3c1668da 1793 return NULL;
89439d4f
MS
1794 return ordered_bucket_at(h, e->iterate_next)->p.value;
1795}
3c1668da 1796
89439d4f
MS
1797int set_consume(Set *s, void *value) {
1798 int r;
1799
d97c5aea
LP
1800 assert(s);
1801 assert(value);
1802
89439d4f 1803 r = set_put(s, value);
575ccc1b 1804 if (r <= 0)
89439d4f
MS
1805 free(value);
1806
1807 return r;
1808}
1809
11e9fec2 1810int _hashmap_put_strdup_full(Hashmap **h, const struct hash_ops *hash_ops, const char *k, const char *v HASHMAP_DEBUG_PARAMS) {
87da8784
ZJS
1811 int r;
1812
11e9fec2 1813 r = _hashmap_ensure_allocated(h, hash_ops HASHMAP_DEBUG_PASS_ARGS);
87da8784
ZJS
1814 if (r < 0)
1815 return r;
1816
1817 _cleanup_free_ char *kdup = NULL, *vdup = NULL;
25b3e2a8 1818
87da8784 1819 kdup = strdup(k);
25b3e2a8 1820 if (!kdup)
87da8784
ZJS
1821 return -ENOMEM;
1822
25b3e2a8
ZJS
1823 if (v) {
1824 vdup = strdup(v);
1825 if (!vdup)
1826 return -ENOMEM;
1827 }
1828
87da8784
ZJS
1829 r = hashmap_put(*h, kdup, vdup);
1830 if (r < 0) {
25b3e2a8 1831 if (r == -EEXIST && streq_ptr(v, hashmap_get(*h, kdup)))
87da8784
ZJS
1832 return 0;
1833 return r;
1834 }
1835
25b3e2a8
ZJS
1836 /* 0 with non-null vdup would mean vdup is already in the hashmap, which cannot be */
1837 assert(vdup == NULL || r > 0);
1838 if (r > 0)
1839 kdup = vdup = NULL;
87da8784 1840
25b3e2a8 1841 return r;
87da8784
ZJS
1842}
1843
cb649d12 1844int _set_put_strndup_full(Set **s, const struct hash_ops *hash_ops, const char *p, size_t n HASHMAP_DEBUG_PARAMS) {
89439d4f 1845 char *c;
be327321 1846 int r;
89439d4f
MS
1847
1848 assert(s);
1849 assert(p);
1850
11e9fec2 1851 r = _set_ensure_allocated(s, hash_ops HASHMAP_DEBUG_PASS_ARGS);
be327321
ZJS
1852 if (r < 0)
1853 return r;
1854
cb649d12
YW
1855 if (n == SIZE_MAX) {
1856 if (set_contains(*s, (char*) p))
1857 return 0;
454f0f86 1858
cb649d12
YW
1859 c = strdup(p);
1860 } else
1861 c = strndup(p, n);
89439d4f
MS
1862 if (!c)
1863 return -ENOMEM;
1864
be327321 1865 return set_consume(*s, c);
89439d4f
MS
1866}
1867
11e9fec2 1868int _set_put_strdupv_full(Set **s, const struct hash_ops *hash_ops, char **l HASHMAP_DEBUG_PARAMS) {
89439d4f 1869 int n = 0, r;
89439d4f 1870
d97c5aea
LP
1871 assert(s);
1872
89439d4f 1873 STRV_FOREACH(i, l) {
cb649d12 1874 r = _set_put_strndup_full(s, hash_ops, *i, SIZE_MAX HASHMAP_DEBUG_PASS_ARGS);
89439d4f
MS
1875 if (r < 0)
1876 return r;
1877
1878 n += r;
1879 }
1880
1881 return n;
3c1668da 1882}
d97c5aea
LP
1883
1884int set_put_strsplit(Set *s, const char *v, const char *separators, ExtractFlags flags) {
99534007 1885 const char *p = ASSERT_PTR(v);
d97c5aea
LP
1886 int r;
1887
1888 assert(s);
d97c5aea
LP
1889
1890 for (;;) {
1891 char *word;
1892
1893 r = extract_first_word(&p, &word, separators, flags);
1894 if (r <= 0)
1895 return r;
1896
1897 r = set_consume(s, word);
1898 if (r < 0)
1899 return r;
1900 }
1901}
45ea84d8
VC
1902
1903/* expand the cachemem if needed, return true if newly (re)activated. */
319a4f4b 1904static int cachemem_maintain(CacheMem *mem, size_t size) {
45ea84d8
VC
1905 assert(mem);
1906
319a4f4b 1907 if (!GREEDY_REALLOC(mem->ptr, size)) {
45ea84d8
VC
1908 if (size > 0)
1909 return -ENOMEM;
1910 }
1911
afbbc068
ZJS
1912 if (!mem->active) {
1913 mem->active = true;
1914 return true;
1915 }
45ea84d8 1916
afbbc068 1917 return false;
45ea84d8
VC
1918}
1919
1920int iterated_cache_get(IteratedCache *cache, const void ***res_keys, const void ***res_values, unsigned *res_n_entries) {
1921 bool sync_keys = false, sync_values = false;
319a4f4b 1922 size_t size;
45ea84d8
VC
1923 int r;
1924
1925 assert(cache);
1926 assert(cache->hashmap);
1927
1928 size = n_entries(cache->hashmap);
1929
1930 if (res_keys) {
1931 r = cachemem_maintain(&cache->keys, size);
1932 if (r < 0)
1933 return r;
1934
1935 sync_keys = r;
1936 } else
1937 cache->keys.active = false;
1938
1939 if (res_values) {
1940 r = cachemem_maintain(&cache->values, size);
1941 if (r < 0)
1942 return r;
1943
1944 sync_values = r;
1945 } else
1946 cache->values.active = false;
1947
1948 if (cache->hashmap->dirty) {
1949 if (cache->keys.active)
1950 sync_keys = true;
1951 if (cache->values.active)
1952 sync_values = true;
1953
1954 cache->hashmap->dirty = false;
1955 }
1956
1957 if (sync_keys || sync_values) {
1958 unsigned i, idx;
1959 Iterator iter;
1960
1961 i = 0;
1962 HASHMAP_FOREACH_IDX(idx, cache->hashmap, iter) {
1963 struct hashmap_base_entry *e;
1964
1965 e = bucket_at(cache->hashmap, idx);
1966
1967 if (sync_keys)
1968 cache->keys.ptr[i] = e->key;
1969 if (sync_values)
1970 cache->values.ptr[i] = entry_value(cache->hashmap, e);
1971 i++;
1972 }
1973 }
1974
1975 if (res_keys)
1976 *res_keys = cache->keys.ptr;
1977 if (res_values)
1978 *res_values = cache->values.ptr;
1979 if (res_n_entries)
1980 *res_n_entries = size;
1981
1982 return 0;
1983}
1984
8a35af80 1985IteratedCache* iterated_cache_free(IteratedCache *cache) {
45ea84d8
VC
1986 if (cache) {
1987 free(cache->keys.ptr);
1988 free(cache->values.ptr);
45ea84d8
VC
1989 }
1990
b61658fd 1991 return mfree(cache);
45ea84d8 1992}
4dbce717 1993
8d80f275 1994int set_strjoin(Set *s, const char *separator, bool wrap_with_separator, char **ret) {
4dbce717 1995 _cleanup_free_ char *str = NULL;
319a4f4b 1996 size_t separator_len, len = 0;
4dbce717 1997 const char *value;
8d80f275 1998 bool first;
4dbce717
YW
1999
2000 assert(ret);
2001
8d80f275
YW
2002 if (set_isempty(s)) {
2003 *ret = NULL;
2004 return 0;
2005 }
2006
4dbce717
YW
2007 separator_len = strlen_ptr(separator);
2008
8d80f275
YW
2009 if (separator_len == 0)
2010 wrap_with_separator = false;
2011
2012 first = !wrap_with_separator;
2013
4dbce717
YW
2014 SET_FOREACH(value, s) {
2015 size_t l = strlen_ptr(value);
2016
2017 if (l == 0)
2018 continue;
2019
319a4f4b 2020 if (!GREEDY_REALLOC(str, len + l + (first ? 0 : separator_len) + (wrap_with_separator ? separator_len : 0) + 1))
4dbce717
YW
2021 return -ENOMEM;
2022
2023 if (separator_len > 0 && !first) {
2024 memcpy(str + len, separator, separator_len);
2025 len += separator_len;
2026 }
2027
2028 memcpy(str + len, value, l);
2029 len += l;
2030 first = false;
2031 }
8d80f275
YW
2032
2033 if (wrap_with_separator) {
2034 memcpy(str + len, separator, separator_len);
2035 len += separator_len;
2036 }
2037
2038 str[len] = '\0';
4dbce717
YW
2039
2040 *ret = TAKE_PTR(str);
2041 return 0;
2042}
e4304fb8
LP
2043
2044bool set_equal(Set *a, Set *b) {
2045 void *p;
2046
2047 /* Checks whether each entry of 'a' is also in 'b' and vice versa, i.e. the two sets contain the same
2048 * entries */
2049
2050 if (a == b)
2051 return true;
2052
2053 if (set_isempty(a) && set_isempty(b))
2054 return true;
2055
2056 if (set_size(a) != set_size(b)) /* Cheap check that hopefully catches a lot of inequality cases
2057 * already */
2058 return false;
2059
2060 SET_FOREACH(p, a)
2061 if (!set_contains(b, p))
2062 return false;
2063
2064 /* If we have the same hashops, then we don't need to check things backwards given we compared the
2065 * size and that all of a is in b. */
2066 if (a->b.hash_ops == b->b.hash_ops)
2067 return true;
2068
2069 SET_FOREACH(p, b)
2070 if (!set_contains(a, p))
2071 return false;
2072
2073 return true;
2074}
d25d4f18
YW
2075
2076static bool set_fnmatch_one(Set *patterns, const char *needle) {
2077 const char *p;
2078
2079 assert(needle);
2080
8812f8fc
LP
2081 /* Any failure of fnmatch() is treated as equivalent to FNM_NOMATCH, i.e. as non-matching pattern */
2082
d25d4f18
YW
2083 SET_FOREACH(p, patterns)
2084 if (fnmatch(p, needle, 0) == 0)
2085 return true;
2086
2087 return false;
2088}
2089
2090bool set_fnmatch(Set *include_patterns, Set *exclude_patterns, const char *needle) {
2091 assert(needle);
2092
2093 if (set_fnmatch_one(exclude_patterns, needle))
2094 return false;
2095
2096 if (set_isempty(include_patterns))
2097 return true;
2098
2099 return set_fnmatch_one(include_patterns, needle);
2100}