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