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