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