blob: 159bd8ae161894b5adc6a934ebe5bdbd5bb2269c [file] [log] [blame]
Jason Evans6109fe02010-02-10 10:37:56 -08001/*
2 *******************************************************************************
3 * Implementation of (2^1+,2) cuckoo hashing, where 2^1+ indicates that each
4 * hash bucket contains 2^n cells, for n >= 1, and 2 indicates that two hash
5 * functions are employed. The original cuckoo hashing algorithm was described
6 * in:
7 *
8 * Pagh, R., F.F. Rodler (2004) Cuckoo Hashing. Journal of Algorithms
9 * 51(2):122-144.
10 *
11 * Generalization of cuckoo hashing was discussed in:
12 *
13 * Erlingsson, U., M. Manasse, F. McSherry (2006) A cool and practical
14 * alternative to traditional hash tables. In Proceedings of the 7th
15 * Workshop on Distributed Data and Structures (WDAS'06), Santa Clara, CA,
16 * January 2006.
17 *
18 * This implementation uses precisely two hash functions because that is the
19 * fewest that can work, and supporting multiple hashes is an implementation
20 * burden. Here is a reproduction of Figure 1 from Erlingsson et al. (2006)
21 * that shows approximate expected maximum load factors for various
22 * configurations:
23 *
24 * | #cells/bucket |
25 * #hashes | 1 | 2 | 4 | 8 |
26 * --------+-------+-------+-------+-------+
27 * 1 | 0.006 | 0.006 | 0.03 | 0.12 |
28 * 2 | 0.49 | 0.86 |>0.93< |>0.96< |
29 * 3 | 0.91 | 0.97 | 0.98 | 0.999 |
30 * 4 | 0.97 | 0.99 | 0.999 | |
31 *
32 * The number of cells per bucket is chosen such that a bucket fits in one cache
33 * line. So, on 32- and 64-bit systems, we use (8,2) and (4,2) cuckoo hashing,
34 * respectively.
35 *
36 ******************************************************************************/
Jason Evans0657f122011-03-18 17:56:14 -070037#define JEMALLOC_CKH_C_
Jason Evans376b1522010-02-11 14:45:59 -080038#include "jemalloc/internal/jemalloc_internal.h"
Jason Evans6109fe02010-02-10 10:37:56 -080039
40/******************************************************************************/
41/* Function prototypes for non-inline static functions. */
42
Jason Evans962a2972016-10-20 23:59:12 -070043static bool ckh_grow(tsd_t *tsd, ckh_t *ckh);
44static void ckh_shrink(tsd_t *tsd, ckh_t *ckh);
Jason Evans6109fe02010-02-10 10:37:56 -080045
46/******************************************************************************/
47
48/*
49 * Search bucket for key and return the cell number if found; SIZE_T_MAX
50 * otherwise.
51 */
Jason Evans6edc97d2013-12-10 14:23:10 -080052JEMALLOC_INLINE_C size_t
Jason Evans6109fe02010-02-10 10:37:56 -080053ckh_bucket_search(ckh_t *ckh, size_t bucket, const void *key)
54{
55 ckhc_t *cell;
56 unsigned i;
57
58 for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
59 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
60 if (cell->key != NULL && ckh->keycomp(key, cell->key))
61 return ((bucket << LG_CKH_BUCKET_CELLS) + i);
62 }
63
64 return (SIZE_T_MAX);
65}
66
67/*
68 * Search table for key and return cell number if found; SIZE_T_MAX otherwise.
69 */
Jason Evans6edc97d2013-12-10 14:23:10 -080070JEMALLOC_INLINE_C size_t
Jason Evans6109fe02010-02-10 10:37:56 -080071ckh_isearch(ckh_t *ckh, const void *key)
72{
Jason Evansae03bf62013-01-22 12:02:08 -080073 size_t hashes[2], bucket, cell;
Jason Evans6109fe02010-02-10 10:37:56 -080074
75 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -080076
Jason Evansae03bf62013-01-22 12:02:08 -080077 ckh->hash(key, hashes);
Jason Evans6109fe02010-02-10 10:37:56 -080078
79 /* Search primary bucket. */
Jason Evansae03bf62013-01-22 12:02:08 -080080 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
Jason Evans6109fe02010-02-10 10:37:56 -080081 cell = ckh_bucket_search(ckh, bucket, key);
82 if (cell != SIZE_T_MAX)
83 return (cell);
84
85 /* Search secondary bucket. */
Jason Evansae03bf62013-01-22 12:02:08 -080086 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
Jason Evans6109fe02010-02-10 10:37:56 -080087 cell = ckh_bucket_search(ckh, bucket, key);
88 return (cell);
89}
90
Jason Evans6edc97d2013-12-10 14:23:10 -080091JEMALLOC_INLINE_C bool
Jason Evans6109fe02010-02-10 10:37:56 -080092ckh_try_bucket_insert(ckh_t *ckh, size_t bucket, const void *key,
93 const void *data)
94{
95 ckhc_t *cell;
96 unsigned offset, i;
97
98 /*
99 * Cycle through the cells in the bucket, starting at a random position.
100 * The randomness avoids worst-case search overhead as buckets fill up.
101 */
Jason Evans5d6cb6e2016-11-07 10:52:44 -0800102 offset = (unsigned)prng_lg_range_u64(&ckh->prng_state,
103 LG_CKH_BUCKET_CELLS);
Jason Evans6109fe02010-02-10 10:37:56 -0800104 for (i = 0; i < (ZU(1) << LG_CKH_BUCKET_CELLS); i++) {
105 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) +
106 ((i + offset) & ((ZU(1) << LG_CKH_BUCKET_CELLS) - 1))];
107 if (cell->key == NULL) {
108 cell->key = key;
109 cell->data = data;
110 ckh->count++;
111 return (false);
112 }
113 }
114
115 return (true);
116}
117
118/*
119 * No space is available in bucket. Randomly evict an item, then try to find an
120 * alternate location for that item. Iteratively repeat this
121 * eviction/relocation procedure until either success or detection of an
122 * eviction/relocation bucket cycle.
123 */
Jason Evans6edc97d2013-12-10 14:23:10 -0800124JEMALLOC_INLINE_C bool
Jason Evans6109fe02010-02-10 10:37:56 -0800125ckh_evict_reloc_insert(ckh_t *ckh, size_t argbucket, void const **argkey,
126 void const **argdata)
127{
128 const void *key, *data, *tkey, *tdata;
129 ckhc_t *cell;
Jason Evansae03bf62013-01-22 12:02:08 -0800130 size_t hashes[2], bucket, tbucket;
Jason Evans6109fe02010-02-10 10:37:56 -0800131 unsigned i;
132
133 bucket = argbucket;
134 key = *argkey;
135 data = *argdata;
136 while (true) {
137 /*
138 * Choose a random item within the bucket to evict. This is
139 * critical to correct function, because without (eventually)
140 * evicting all items within a bucket during iteration, it
141 * would be possible to get stuck in an infinite loop if there
142 * were an item for which both hashes indicated the same
143 * bucket.
144 */
Jason Evans5d6cb6e2016-11-07 10:52:44 -0800145 i = (unsigned)prng_lg_range_u64(&ckh->prng_state,
Jason Evans9e1810c2016-02-24 12:42:23 -0800146 LG_CKH_BUCKET_CELLS);
Jason Evans6109fe02010-02-10 10:37:56 -0800147 cell = &ckh->tab[(bucket << LG_CKH_BUCKET_CELLS) + i];
148 assert(cell->key != NULL);
149
150 /* Swap cell->{key,data} and {key,data} (evict). */
151 tkey = cell->key; tdata = cell->data;
152 cell->key = key; cell->data = data;
153 key = tkey; data = tdata;
154
155#ifdef CKH_COUNT
156 ckh->nrelocs++;
157#endif
158
159 /* Find the alternate bucket for the evicted item. */
Jason Evansae03bf62013-01-22 12:02:08 -0800160 ckh->hash(key, hashes);
161 tbucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
Jason Evans6109fe02010-02-10 10:37:56 -0800162 if (tbucket == bucket) {
Jason Evansae03bf62013-01-22 12:02:08 -0800163 tbucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets)
164 - 1);
Jason Evans6109fe02010-02-10 10:37:56 -0800165 /*
166 * It may be that (tbucket == bucket) still, if the
167 * item's hashes both indicate this bucket. However,
168 * we are guaranteed to eventually escape this bucket
169 * during iteration, assuming pseudo-random item
170 * selection (true randomness would make infinite
171 * looping a remote possibility). The reason we can
172 * never get trapped forever is that there are two
173 * cases:
174 *
175 * 1) This bucket == argbucket, so we will quickly
176 * detect an eviction cycle and terminate.
177 * 2) An item was evicted to this bucket from another,
178 * which means that at least one item in this bucket
179 * has hashes that indicate distinct buckets.
180 */
181 }
182 /* Check for a cycle. */
183 if (tbucket == argbucket) {
184 *argkey = key;
185 *argdata = data;
186 return (true);
187 }
188
189 bucket = tbucket;
Jason Evans551ebc42014-10-03 10:16:09 -0700190 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
Jason Evans6109fe02010-02-10 10:37:56 -0800191 return (false);
192 }
193}
194
Jason Evans6edc97d2013-12-10 14:23:10 -0800195JEMALLOC_INLINE_C bool
Jason Evans6109fe02010-02-10 10:37:56 -0800196ckh_try_insert(ckh_t *ckh, void const**argkey, void const**argdata)
197{
Jason Evansae03bf62013-01-22 12:02:08 -0800198 size_t hashes[2], bucket;
Jason Evans6109fe02010-02-10 10:37:56 -0800199 const void *key = *argkey;
200 const void *data = *argdata;
201
Jason Evansae03bf62013-01-22 12:02:08 -0800202 ckh->hash(key, hashes);
Jason Evans6109fe02010-02-10 10:37:56 -0800203
204 /* Try to insert in primary bucket. */
Jason Evansae03bf62013-01-22 12:02:08 -0800205 bucket = hashes[0] & ((ZU(1) << ckh->lg_curbuckets) - 1);
Jason Evans551ebc42014-10-03 10:16:09 -0700206 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
Jason Evans6109fe02010-02-10 10:37:56 -0800207 return (false);
208
209 /* Try to insert in secondary bucket. */
Jason Evansae03bf62013-01-22 12:02:08 -0800210 bucket = hashes[1] & ((ZU(1) << ckh->lg_curbuckets) - 1);
Jason Evans551ebc42014-10-03 10:16:09 -0700211 if (!ckh_try_bucket_insert(ckh, bucket, key, data))
Jason Evans6109fe02010-02-10 10:37:56 -0800212 return (false);
213
214 /*
215 * Try to find a place for this item via iterative eviction/relocation.
216 */
217 return (ckh_evict_reloc_insert(ckh, bucket, argkey, argdata));
218}
219
220/*
221 * Try to rebuild the hash table from scratch by inserting all items from the
222 * old table into the new.
223 */
Jason Evans6edc97d2013-12-10 14:23:10 -0800224JEMALLOC_INLINE_C bool
Jason Evans6109fe02010-02-10 10:37:56 -0800225ckh_rebuild(ckh_t *ckh, ckhc_t *aTab)
226{
227 size_t count, i, nins;
228 const void *key, *data;
229
230 count = ckh->count;
231 ckh->count = 0;
232 for (i = nins = 0; nins < count; i++) {
233 if (aTab[i].key != NULL) {
234 key = aTab[i].key;
235 data = aTab[i].data;
236 if (ckh_try_insert(ckh, &key, &data)) {
237 ckh->count = count;
238 return (true);
239 }
240 nins++;
241 }
242 }
243
244 return (false);
245}
246
247static bool
Jason Evans962a2972016-10-20 23:59:12 -0700248ckh_grow(tsd_t *tsd, ckh_t *ckh)
Jason Evans6109fe02010-02-10 10:37:56 -0800249{
250 bool ret;
251 ckhc_t *tab, *ttab;
Jason Evans9e1810c2016-02-24 12:42:23 -0800252 unsigned lg_prevbuckets, lg_curcells;
Jason Evans6109fe02010-02-10 10:37:56 -0800253
254#ifdef CKH_COUNT
255 ckh->ngrows++;
256#endif
257
258 /*
259 * It is possible (though unlikely, given well behaved hashes) that the
260 * table will have to be doubled more than once in order to create a
261 * usable table.
262 */
263 lg_prevbuckets = ckh->lg_curbuckets;
264 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS;
265 while (true) {
Jason Evans38d92102011-03-23 00:37:29 -0700266 size_t usize;
267
Jason Evans6109fe02010-02-10 10:37:56 -0800268 lg_curcells++;
Jason Evans5ff709c2012-04-11 18:13:45 -0700269 usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
Jason Evans0c516a02016-02-25 15:29:49 -0800270 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS)) {
Jason Evans38d92102011-03-23 00:37:29 -0700271 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700272 goto label_return;
Jason Evans38d92102011-03-23 00:37:29 -0700273 }
Jason Evans962a2972016-10-20 23:59:12 -0700274 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE,
275 true, NULL, true, arena_ichoose(tsd, NULL));
Jason Evans6109fe02010-02-10 10:37:56 -0800276 if (tab == NULL) {
277 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700278 goto label_return;
Jason Evans6109fe02010-02-10 10:37:56 -0800279 }
Jason Evans6109fe02010-02-10 10:37:56 -0800280 /* Swap in new table. */
281 ttab = ckh->tab;
282 ckh->tab = tab;
283 tab = ttab;
284 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
285
Jason Evans551ebc42014-10-03 10:16:09 -0700286 if (!ckh_rebuild(ckh, tab)) {
Jason Evans962a2972016-10-20 23:59:12 -0700287 idalloctm(tsd_tsdn(tsd), tab, NULL, true, true);
Jason Evans6109fe02010-02-10 10:37:56 -0800288 break;
289 }
290
291 /* Rebuilding failed, so back out partially rebuilt table. */
Jason Evans962a2972016-10-20 23:59:12 -0700292 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
Jason Evans6109fe02010-02-10 10:37:56 -0800293 ckh->tab = tab;
294 ckh->lg_curbuckets = lg_prevbuckets;
295 }
296
297 ret = false;
Jason Evansa1ee7832012-04-10 15:07:44 -0700298label_return:
Jason Evans6109fe02010-02-10 10:37:56 -0800299 return (ret);
300}
301
302static void
Jason Evans962a2972016-10-20 23:59:12 -0700303ckh_shrink(tsd_t *tsd, ckh_t *ckh)
Jason Evans6109fe02010-02-10 10:37:56 -0800304{
305 ckhc_t *tab, *ttab;
Jason Evans9e1810c2016-02-24 12:42:23 -0800306 size_t usize;
307 unsigned lg_prevbuckets, lg_curcells;
Jason Evans6109fe02010-02-10 10:37:56 -0800308
309 /*
310 * It is possible (though unlikely, given well behaved hashes) that the
311 * table rebuild will fail.
312 */
313 lg_prevbuckets = ckh->lg_curbuckets;
314 lg_curcells = ckh->lg_curbuckets + LG_CKH_BUCKET_CELLS - 1;
Jason Evans5ff709c2012-04-11 18:13:45 -0700315 usize = sa2u(sizeof(ckhc_t) << lg_curcells, CACHELINE);
Jason Evans0c516a02016-02-25 15:29:49 -0800316 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS))
Jason Evans38d92102011-03-23 00:37:29 -0700317 return;
Jason Evans962a2972016-10-20 23:59:12 -0700318 tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true, NULL,
319 true, arena_ichoose(tsd, NULL));
Jason Evans6109fe02010-02-10 10:37:56 -0800320 if (tab == NULL) {
321 /*
322 * An OOM error isn't worth propagating, since it doesn't
323 * prevent this or future operations from proceeding.
324 */
325 return;
326 }
Jason Evans6109fe02010-02-10 10:37:56 -0800327 /* Swap in new table. */
328 ttab = ckh->tab;
329 ckh->tab = tab;
330 tab = ttab;
331 ckh->lg_curbuckets = lg_curcells - LG_CKH_BUCKET_CELLS;
332
Jason Evans551ebc42014-10-03 10:16:09 -0700333 if (!ckh_rebuild(ckh, tab)) {
Jason Evans962a2972016-10-20 23:59:12 -0700334 idalloctm(tsd_tsdn(tsd), tab, NULL, true, true);
Jason Evans6109fe02010-02-10 10:37:56 -0800335#ifdef CKH_COUNT
336 ckh->nshrinks++;
337#endif
338 return;
339 }
340
341 /* Rebuilding failed, so back out partially rebuilt table. */
Jason Evans962a2972016-10-20 23:59:12 -0700342 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
Jason Evans6109fe02010-02-10 10:37:56 -0800343 ckh->tab = tab;
344 ckh->lg_curbuckets = lg_prevbuckets;
345#ifdef CKH_COUNT
346 ckh->nshrinkfails++;
347#endif
348}
349
350bool
Jason Evans962a2972016-10-20 23:59:12 -0700351ckh_new(tsd_t *tsd, ckh_t *ckh, size_t minitems, ckh_hash_t *hash,
Jason Evans5460aa62014-09-22 21:09:23 -0700352 ckh_keycomp_t *keycomp)
Jason Evans6109fe02010-02-10 10:37:56 -0800353{
354 bool ret;
Jason Evans38d92102011-03-23 00:37:29 -0700355 size_t mincells, usize;
Jason Evans6109fe02010-02-10 10:37:56 -0800356 unsigned lg_mincells;
357
358 assert(minitems > 0);
359 assert(hash != NULL);
360 assert(keycomp != NULL);
361
362#ifdef CKH_COUNT
363 ckh->ngrows = 0;
364 ckh->nshrinks = 0;
365 ckh->nshrinkfails = 0;
366 ckh->ninserts = 0;
367 ckh->nrelocs = 0;
368#endif
Jason Evans84f7cdb2012-03-02 15:59:45 -0800369 ckh->prng_state = 42; /* Value doesn't really matter. */
Jason Evans6109fe02010-02-10 10:37:56 -0800370 ckh->count = 0;
371
372 /*
Jason Evanse12eaf92014-12-08 14:40:14 -0800373 * Find the minimum power of 2 that is large enough to fit minitems
Jason Evans6109fe02010-02-10 10:37:56 -0800374 * entries. We are using (2+,2) cuckoo hashing, which has an expected
375 * maximum load factor of at least ~0.86, so 0.75 is a conservative load
Jason Evanse12eaf92014-12-08 14:40:14 -0800376 * factor that will typically allow mincells items to fit without ever
Jason Evans6109fe02010-02-10 10:37:56 -0800377 * growing the table.
378 */
379 assert(LG_CKH_BUCKET_CELLS > 0);
380 mincells = ((minitems + (3 - (minitems % 3))) / 3) << 2;
381 for (lg_mincells = LG_CKH_BUCKET_CELLS;
382 (ZU(1) << lg_mincells) < mincells;
383 lg_mincells++)
384 ; /* Do nothing. */
385 ckh->lg_minbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
386 ckh->lg_curbuckets = lg_mincells - LG_CKH_BUCKET_CELLS;
387 ckh->hash = hash;
388 ckh->keycomp = keycomp;
389
Jason Evans5ff709c2012-04-11 18:13:45 -0700390 usize = sa2u(sizeof(ckhc_t) << lg_mincells, CACHELINE);
Jason Evans0c516a02016-02-25 15:29:49 -0800391 if (unlikely(usize == 0 || usize > HUGE_MAXCLASS)) {
Jason Evans38d92102011-03-23 00:37:29 -0700392 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700393 goto label_return;
Jason Evans38d92102011-03-23 00:37:29 -0700394 }
Jason Evans962a2972016-10-20 23:59:12 -0700395 ckh->tab = (ckhc_t *)ipallocztm(tsd_tsdn(tsd), usize, CACHELINE, true,
396 NULL, true, arena_ichoose(tsd, NULL));
Jason Evans6109fe02010-02-10 10:37:56 -0800397 if (ckh->tab == NULL) {
398 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700399 goto label_return;
Jason Evans6109fe02010-02-10 10:37:56 -0800400 }
Jason Evans6109fe02010-02-10 10:37:56 -0800401
Jason Evans6109fe02010-02-10 10:37:56 -0800402 ret = false;
Jason Evansa1ee7832012-04-10 15:07:44 -0700403label_return:
Jason Evans6109fe02010-02-10 10:37:56 -0800404 return (ret);
405}
406
407void
Jason Evans962a2972016-10-20 23:59:12 -0700408ckh_delete(tsd_t *tsd, ckh_t *ckh)
Jason Evans6109fe02010-02-10 10:37:56 -0800409{
410
411 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800412
413#ifdef CKH_VERBOSE
414 malloc_printf(
Jason Evans5fae7dc2015-07-23 13:56:25 -0700415 "%s(%p): ngrows: %"FMTu64", nshrinks: %"FMTu64","
416 " nshrinkfails: %"FMTu64", ninserts: %"FMTu64","
417 " nrelocs: %"FMTu64"\n", __func__, ckh,
Jason Evans6109fe02010-02-10 10:37:56 -0800418 (unsigned long long)ckh->ngrows,
419 (unsigned long long)ckh->nshrinks,
420 (unsigned long long)ckh->nshrinkfails,
421 (unsigned long long)ckh->ninserts,
422 (unsigned long long)ckh->nrelocs);
423#endif
424
Jason Evans962a2972016-10-20 23:59:12 -0700425 idalloctm(tsd_tsdn(tsd), ckh->tab, NULL, true, true);
Jason Evansba175a22013-01-22 12:14:45 -0800426 if (config_debug)
Chris Petersona82070e2016-03-27 23:28:39 -0700427 memset(ckh, JEMALLOC_FREE_JUNK, sizeof(ckh_t));
Jason Evans6109fe02010-02-10 10:37:56 -0800428}
429
430size_t
431ckh_count(ckh_t *ckh)
432{
433
434 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800435
436 return (ckh->count);
437}
438
439bool
440ckh_iter(ckh_t *ckh, size_t *tabind, void **key, void **data)
441{
442 size_t i, ncells;
443
444 for (i = *tabind, ncells = (ZU(1) << (ckh->lg_curbuckets +
445 LG_CKH_BUCKET_CELLS)); i < ncells; i++) {
446 if (ckh->tab[i].key != NULL) {
447 if (key != NULL)
448 *key = (void *)ckh->tab[i].key;
449 if (data != NULL)
450 *data = (void *)ckh->tab[i].data;
451 *tabind = i + 1;
452 return (false);
453 }
454 }
455
456 return (true);
457}
458
459bool
Jason Evans962a2972016-10-20 23:59:12 -0700460ckh_insert(tsd_t *tsd, ckh_t *ckh, const void *key, const void *data)
Jason Evans6109fe02010-02-10 10:37:56 -0800461{
462 bool ret;
463
464 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800465 assert(ckh_search(ckh, key, NULL, NULL));
466
467#ifdef CKH_COUNT
468 ckh->ninserts++;
469#endif
470
471 while (ckh_try_insert(ckh, &key, &data)) {
Jason Evans962a2972016-10-20 23:59:12 -0700472 if (ckh_grow(tsd, ckh)) {
Jason Evans6109fe02010-02-10 10:37:56 -0800473 ret = true;
Jason Evansa1ee7832012-04-10 15:07:44 -0700474 goto label_return;
Jason Evans6109fe02010-02-10 10:37:56 -0800475 }
476 }
477
478 ret = false;
Jason Evansa1ee7832012-04-10 15:07:44 -0700479label_return:
Jason Evans6109fe02010-02-10 10:37:56 -0800480 return (ret);
481}
482
483bool
Jason Evans962a2972016-10-20 23:59:12 -0700484ckh_remove(tsd_t *tsd, ckh_t *ckh, const void *searchkey, void **key,
Jason Evans5460aa62014-09-22 21:09:23 -0700485 void **data)
Jason Evans6109fe02010-02-10 10:37:56 -0800486{
487 size_t cell;
488
489 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800490
491 cell = ckh_isearch(ckh, searchkey);
492 if (cell != SIZE_T_MAX) {
493 if (key != NULL)
494 *key = (void *)ckh->tab[cell].key;
495 if (data != NULL)
496 *data = (void *)ckh->tab[cell].data;
497 ckh->tab[cell].key = NULL;
498 ckh->tab[cell].data = NULL; /* Not necessary. */
499
500 ckh->count--;
501 /* Try to halve the table if it is less than 1/4 full. */
502 if (ckh->count < (ZU(1) << (ckh->lg_curbuckets
503 + LG_CKH_BUCKET_CELLS - 2)) && ckh->lg_curbuckets
504 > ckh->lg_minbuckets) {
505 /* Ignore error due to OOM. */
Jason Evans962a2972016-10-20 23:59:12 -0700506 ckh_shrink(tsd, ckh);
Jason Evans6109fe02010-02-10 10:37:56 -0800507 }
508
509 return (false);
510 }
511
512 return (true);
513}
514
515bool
516ckh_search(ckh_t *ckh, const void *searchkey, void **key, void **data)
517{
518 size_t cell;
519
520 assert(ckh != NULL);
Jason Evans6109fe02010-02-10 10:37:56 -0800521
522 cell = ckh_isearch(ckh, searchkey);
523 if (cell != SIZE_T_MAX) {
524 if (key != NULL)
525 *key = (void *)ckh->tab[cell].key;
526 if (data != NULL)
527 *data = (void *)ckh->tab[cell].data;
528 return (false);
529 }
530
531 return (true);
532}
533
534void
Jason Evansae03bf62013-01-22 12:02:08 -0800535ckh_string_hash(const void *key, size_t r_hash[2])
Jason Evans6109fe02010-02-10 10:37:56 -0800536{
Jason Evans6109fe02010-02-10 10:37:56 -0800537
Jason Evansae03bf62013-01-22 12:02:08 -0800538 hash(key, strlen((const char *)key), 0x94122f33U, r_hash);
Jason Evans6109fe02010-02-10 10:37:56 -0800539}
540
541bool
542ckh_string_keycomp(const void *k1, const void *k2)
543{
544
545 assert(k1 != NULL);
546 assert(k2 != NULL);
547
548 return (strcmp((char *)k1, (char *)k2) ? false : true);
549}
550
551void
Jason Evansae03bf62013-01-22 12:02:08 -0800552ckh_pointer_hash(const void *key, size_t r_hash[2])
Jason Evans6109fe02010-02-10 10:37:56 -0800553{
Jason Evans355b4382010-09-20 19:20:48 -0700554 union {
555 const void *v;
Jason Evansae03bf62013-01-22 12:02:08 -0800556 size_t i;
Jason Evans355b4382010-09-20 19:20:48 -0700557 } u;
Jason Evans6109fe02010-02-10 10:37:56 -0800558
Jason Evans355b4382010-09-20 19:20:48 -0700559 assert(sizeof(u.v) == sizeof(u.i));
Jason Evans355b4382010-09-20 19:20:48 -0700560 u.v = key;
Jason Evansae03bf62013-01-22 12:02:08 -0800561 hash(&u.i, sizeof(u.i), 0xd983396eU, r_hash);
Jason Evans6109fe02010-02-10 10:37:56 -0800562}
563
564bool
565ckh_pointer_keycomp(const void *k1, const void *k2)
566{
567
568 return ((k1 == k2) ? true : false);
569}