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