2 * Copyright 2014-2015 Olivier Houchard.
3 * Copyright 2012-2015 Samy Al Bahra.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <ck_limits.h>
33 #include <ck_stdint.h>
34 #include <ck_stdbool.h>
35 #include <ck_string.h>
37 #include "ck_internal.h"
39 #ifndef CK_RHS_PROBE_L1_SHIFT
40 #define CK_RHS_PROBE_L1_SHIFT 3ULL
41 #endif /* CK_RHS_PROBE_L1_SHIFT */
43 #define CK_RHS_PROBE_L1 (1 << CK_RHS_PROBE_L1_SHIFT)
44 #define CK_RHS_PROBE_L1_MASK (CK_RHS_PROBE_L1 - 1)
46 #ifndef CK_RHS_PROBE_L1_DEFAULT
47 #define CK_RHS_PROBE_L1_DEFAULT CK_MD_CACHELINE
50 #define CK_RHS_VMA_MASK ((uintptr_t)((1ULL << CK_MD_VMA_BITS) - 1))
51 #define CK_RHS_VMA(x) \
52 ((void *)((uintptr_t)(x) & CK_RHS_VMA_MASK))
54 #define CK_RHS_EMPTY NULL
55 #define CK_RHS_G (1024)
56 #define CK_RHS_G_MASK (CK_RHS_G - 1)
58 #if defined(CK_F_PR_LOAD_8) && defined(CK_F_PR_STORE_8)
59 #define CK_RHS_WORD uint8_t
60 #define CK_RHS_WORD_MAX UINT8_MAX
61 #define CK_RHS_STORE(x, y) ck_pr_store_8(x, y)
62 #define CK_RHS_LOAD(x) ck_pr_load_8(x)
63 #elif defined(CK_F_PR_LOAD_16) && defined(CK_F_PR_STORE_16)
64 #define CK_RHS_WORD uint16_t
65 #define CK_RHS_WORD_MAX UINT16_MAX
66 #define CK_RHS_STORE(x, y) ck_pr_store_16(x, y)
67 #define CK_RHS_LOAD(x) ck_pr_load_16(x)
68 #elif defined(CK_F_PR_LOAD_32) && defined(CK_F_PR_STORE_32)
69 #define CK_RHS_WORD uint32_t
70 #define CK_RHS_WORD_MAX UINT32_MAX
71 #define CK_RHS_STORE(x, y) ck_pr_store_32(x, y)
72 #define CK_RHS_LOAD(x) ck_pr_load_32(x)
74 #error "ck_rhs is not supported on your platform."
77 #define CK_RHS_MAX_WANTED 0xffff
79 enum ck_rhs_probe_behavior {
80 CK_RHS_PROBE = 0, /* Default behavior. */
81 CK_RHS_PROBE_RH, /* Short-circuit if RH slot found. */
82 CK_RHS_PROBE_INSERT, /* Short-circuit on probe bound if tombstone found. */
84 CK_RHS_PROBE_ROBIN_HOOD,/* Look for the first slot available for the entry we are about to replace, only used to internally implement Robin Hood */
85 CK_RHS_PROBE_NO_RH, /* Don't do the RH dance */
87 struct ck_rhs_entry_desc {
89 unsigned short wanted;
90 CK_RHS_WORD probe_bound;
95 struct ck_rhs_no_entry_desc {
97 unsigned short wanted;
98 CK_RHS_WORD probe_bound;
102 typedef long ck_rhs_probe_cb_t(struct ck_rhs *hs,
103 struct ck_rhs_map *map,
104 unsigned long *n_probes,
109 unsigned long probe_limit,
110 enum ck_rhs_probe_behavior behavior);
113 unsigned int generation[CK_RHS_G];
114 unsigned int probe_maximum;
117 unsigned int probe_limit;
118 unsigned long n_entries;
119 unsigned long capacity;
121 unsigned long max_entries;
124 struct ck_rhs_entry_desc *descs;
125 struct ck_rhs_no_entry {
126 const void **entries;
127 struct ck_rhs_no_entry_desc *descs;
131 ck_rhs_probe_cb_t *probe_func;
134 static CK_CC_INLINE const void *
135 ck_rhs_entry(struct ck_rhs_map *map, long offset)
138 if (map->read_mostly)
139 return (map->entries.no_entries.entries[offset]);
141 return (map->entries.descs[offset].entry);
144 static CK_CC_INLINE const void **
145 ck_rhs_entry_addr(struct ck_rhs_map *map, long offset)
148 if (map->read_mostly)
149 return (&map->entries.no_entries.entries[offset]);
151 return (&map->entries.descs[offset].entry);
154 static CK_CC_INLINE struct ck_rhs_entry_desc *
155 ck_rhs_desc(struct ck_rhs_map *map, long offset)
158 if (CK_CC_UNLIKELY(map->read_mostly))
159 return ((struct ck_rhs_entry_desc *)(void *)&map->entries.no_entries.descs[offset]);
161 return (&map->entries.descs[offset]);
164 static CK_CC_INLINE void
165 ck_rhs_wanted_inc(struct ck_rhs_map *map, long offset)
168 if (CK_CC_UNLIKELY(map->read_mostly))
169 map->entries.no_entries.descs[offset].wanted++;
171 map->entries.descs[offset].wanted++;
174 static CK_CC_INLINE unsigned int
175 ck_rhs_probes(struct ck_rhs_map *map, long offset)
178 if (CK_CC_UNLIKELY(map->read_mostly))
179 return (map->entries.no_entries.descs[offset].probes);
181 return (map->entries.descs[offset].probes);
184 static CK_CC_INLINE void
185 ck_rhs_set_probes(struct ck_rhs_map *map, long offset, unsigned int value)
188 if (CK_CC_UNLIKELY(map->read_mostly))
189 map->entries.no_entries.descs[offset].probes = value;
191 map->entries.descs[offset].probes = value;
194 static CK_CC_INLINE CK_RHS_WORD
195 ck_rhs_probe_bound(struct ck_rhs_map *map, long offset)
198 if (CK_CC_UNLIKELY(map->read_mostly))
199 return (map->entries.no_entries.descs[offset].probe_bound);
201 return (map->entries.descs[offset].probe_bound);
204 static CK_CC_INLINE CK_RHS_WORD *
205 ck_rhs_probe_bound_addr(struct ck_rhs_map *map, long offset)
208 if (CK_CC_UNLIKELY(map->read_mostly))
209 return (&map->entries.no_entries.descs[offset].probe_bound);
211 return (&map->entries.descs[offset].probe_bound);
215 static CK_CC_INLINE bool
216 ck_rhs_in_rh(struct ck_rhs_map *map, long offset)
219 if (CK_CC_UNLIKELY(map->read_mostly))
220 return (map->entries.no_entries.descs[offset].in_rh);
222 return (map->entries.descs[offset].in_rh);
225 static CK_CC_INLINE void
226 ck_rhs_set_rh(struct ck_rhs_map *map, long offset)
229 if (CK_CC_UNLIKELY(map->read_mostly))
230 map->entries.no_entries.descs[offset].in_rh = true;
232 map->entries.descs[offset].in_rh = true;
235 static CK_CC_INLINE void
236 ck_rhs_unset_rh(struct ck_rhs_map *map, long offset)
239 if (CK_CC_UNLIKELY(map->read_mostly))
240 map->entries.no_entries.descs[offset].in_rh = false;
242 map->entries.descs[offset].in_rh = false;
246 #define CK_RHS_DEFAULT_LOAD_FACTOR 50
248 static ck_rhs_probe_cb_t ck_rhs_map_probe;
249 static ck_rhs_probe_cb_t ck_rhs_map_probe_rm;
252 ck_rhs_set_load_factor(struct ck_rhs *hs, unsigned int load_factor)
254 struct ck_rhs_map *map = hs->map;
256 if (load_factor == 0 || load_factor > 100)
259 hs->load_factor = load_factor;
260 map->max_entries = (map->capacity * (unsigned long)hs->load_factor) / 100;
261 while (map->n_entries > map->max_entries) {
262 if (ck_rhs_grow(hs, map->capacity << 1) == false)
270 ck_rhs_iterator_init(struct ck_rhs_iterator *iterator)
273 iterator->cursor = NULL;
274 iterator->offset = 0;
279 ck_rhs_next(struct ck_rhs *hs, struct ck_rhs_iterator *i, void **key)
281 struct ck_rhs_map *map = hs->map;
284 if (i->offset >= map->capacity)
288 value = CK_CC_DECONST_PTR(ck_rhs_entry(map, i->offset));
289 if (value != CK_RHS_EMPTY) {
291 if (hs->mode & CK_RHS_MODE_OBJECT)
292 value = CK_RHS_VMA(value);
298 } while (++i->offset < map->capacity);
304 ck_rhs_stat(struct ck_rhs *hs, struct ck_rhs_stat *st)
306 struct ck_rhs_map *map = hs->map;
308 st->n_entries = map->n_entries;
309 st->probe_maximum = map->probe_maximum;
314 ck_rhs_count(struct ck_rhs *hs)
317 return hs->map->n_entries;
321 ck_rhs_map_destroy(struct ck_malloc *m, struct ck_rhs_map *map, bool defer)
324 m->free(map, map->size, defer);
329 ck_rhs_destroy(struct ck_rhs *hs)
332 ck_rhs_map_destroy(hs->m, hs->map, false);
336 static struct ck_rhs_map *
337 ck_rhs_map_create(struct ck_rhs *hs, unsigned long entries)
339 struct ck_rhs_map *map;
340 unsigned long size, n_entries, limit;
342 n_entries = ck_internal_power_2(entries);
343 if (n_entries < CK_RHS_PROBE_L1)
344 n_entries = CK_RHS_PROBE_L1;
346 if (hs->mode & CK_RHS_MODE_READ_MOSTLY)
347 size = sizeof(struct ck_rhs_map) +
348 (sizeof(void *) * n_entries +
349 sizeof(struct ck_rhs_no_entry_desc) * n_entries +
350 2 * CK_MD_CACHELINE - 1);
352 size = sizeof(struct ck_rhs_map) +
353 (sizeof(struct ck_rhs_entry_desc) * n_entries +
354 CK_MD_CACHELINE - 1);
355 map = hs->m->malloc(size);
358 map->read_mostly = !!(hs->mode & CK_RHS_MODE_READ_MOSTLY);
361 /* We should probably use a more intelligent heuristic for default probe length. */
362 limit = ck_internal_max(n_entries >> (CK_RHS_PROBE_L1_SHIFT + 2), CK_RHS_PROBE_L1_DEFAULT);
363 if (limit > UINT_MAX)
366 map->probe_limit = (unsigned int)limit;
367 map->probe_maximum = 0;
368 map->capacity = n_entries;
369 map->step = ck_internal_bsf(n_entries);
370 map->mask = n_entries - 1;
373 map->max_entries = (map->capacity * (unsigned long)hs->load_factor) / 100;
374 /* Align map allocation to cache line. */
375 if (map->read_mostly) {
376 map->entries.no_entries.entries = (void *)(((uintptr_t)&map[1] +
377 CK_MD_CACHELINE - 1) & ~(CK_MD_CACHELINE - 1));
378 map->entries.no_entries.descs = (void *)(((uintptr_t)map->entries.no_entries.entries + (sizeof(void *) * n_entries) + CK_MD_CACHELINE - 1) &~ (CK_MD_CACHELINE - 1));
379 memset(map->entries.no_entries.entries, 0,
380 sizeof(void *) * n_entries);
381 memset(map->entries.no_entries.descs, 0,
382 sizeof(struct ck_rhs_no_entry_desc) * n_entries);
383 map->offset_mask = (CK_MD_CACHELINE / sizeof(void *)) - 1;
384 map->probe_func = ck_rhs_map_probe_rm;
387 map->entries.descs = (void *)(((uintptr_t)&map[1] +
388 CK_MD_CACHELINE - 1) & ~(CK_MD_CACHELINE - 1));
389 memset(map->entries.descs, 0, sizeof(struct ck_rhs_entry_desc) * n_entries);
390 map->offset_mask = (CK_MD_CACHELINE / sizeof(struct ck_rhs_entry_desc)) - 1;
391 map->probe_func = ck_rhs_map_probe;
393 memset(map->generation, 0, sizeof map->generation);
395 /* Commit entries purge with respect to map publication. */
401 ck_rhs_reset_size(struct ck_rhs *hs, unsigned long capacity)
403 struct ck_rhs_map *map, *previous;
406 map = ck_rhs_map_create(hs, capacity);
410 ck_pr_store_ptr(&hs->map, map);
411 ck_rhs_map_destroy(hs->m, previous, true);
416 ck_rhs_reset(struct ck_rhs *hs)
418 struct ck_rhs_map *previous;
421 return ck_rhs_reset_size(hs, previous->capacity);
424 static inline unsigned long
425 ck_rhs_map_probe_next(struct ck_rhs_map *map,
426 unsigned long offset,
427 unsigned long probes)
430 if (probes & map->offset_mask) {
431 offset = (offset &~ map->offset_mask) +
432 ((offset + 1) & map->offset_mask);
435 return (offset + probes) & map->mask;
438 static inline unsigned long
439 ck_rhs_map_probe_prev(struct ck_rhs_map *map, unsigned long offset,
440 unsigned long probes)
443 if (probes & map->offset_mask) {
444 offset = (offset &~ map->offset_mask) + ((offset - 1) &
448 return ((offset - probes) & map->mask);
453 ck_rhs_map_bound_set(struct ck_rhs_map *m,
455 unsigned long n_probes)
457 unsigned long offset = h & m->mask;
458 struct ck_rhs_entry_desc *desc;
460 if (n_probes > m->probe_maximum)
461 ck_pr_store_uint(&m->probe_maximum, n_probes);
462 if (!(m->read_mostly)) {
463 desc = &m->entries.descs[offset];
465 if (desc->probe_bound < n_probes) {
466 if (n_probes > CK_RHS_WORD_MAX)
467 n_probes = CK_RHS_WORD_MAX;
469 CK_RHS_STORE(&desc->probe_bound, n_probes);
477 static inline unsigned int
478 ck_rhs_map_bound_get(struct ck_rhs_map *m, unsigned long h)
480 unsigned long offset = h & m->mask;
481 unsigned int r = CK_RHS_WORD_MAX;
484 r = ck_pr_load_uint(&m->probe_maximum);
486 r = CK_RHS_LOAD(&m->entries.descs[offset].probe_bound);
487 if (r == CK_RHS_WORD_MAX)
488 r = ck_pr_load_uint(&m->probe_maximum);
494 ck_rhs_grow(struct ck_rhs *hs,
495 unsigned long capacity)
497 struct ck_rhs_map *map, *update;
498 const void *previous, *prev_saved;
499 unsigned long k, offset, probes;
503 if (map->capacity > capacity)
506 update = ck_rhs_map_create(hs, capacity);
510 for (k = 0; k < map->capacity; k++) {
513 prev_saved = previous = ck_rhs_entry(map, k);
514 if (previous == CK_RHS_EMPTY)
518 if (hs->mode & CK_RHS_MODE_OBJECT)
519 previous = CK_RHS_VMA(previous);
522 h = hs->hf(previous, hs->seed);
523 offset = h & update->mask;
527 const void **cursor = ck_rhs_entry_addr(update, offset);
529 if (probes++ == update->probe_limit) {
531 * We have hit the probe limit, map needs to be even larger.
533 ck_rhs_map_destroy(hs->m, update, false);
538 if (CK_CC_LIKELY(*cursor == CK_RHS_EMPTY)) {
539 *cursor = prev_saved;
541 ck_rhs_set_probes(update, offset, probes);
542 ck_rhs_map_bound_set(update, h, probes);
544 } else if (ck_rhs_probes(update, offset) < probes) {
545 const void *tmp = prev_saved;
546 unsigned int old_probes;
547 prev_saved = previous = *cursor;
549 if (hs->mode & CK_RHS_MODE_OBJECT)
550 previous = CK_RHS_VMA(previous);
553 ck_rhs_map_bound_set(update, h, probes);
554 h = hs->hf(previous, hs->seed);
555 old_probes = ck_rhs_probes(update, offset);
556 ck_rhs_set_probes(update, offset, probes);
557 probes = old_probes - 1;
560 ck_rhs_wanted_inc(update, offset);
561 offset = ck_rhs_map_probe_next(update, offset, probes);
567 ck_pr_store_ptr(&hs->map, update);
568 ck_rhs_map_destroy(hs->m, map, true);
573 ck_rhs_rebuild(struct ck_rhs *hs)
576 return ck_rhs_grow(hs, hs->map->capacity);
580 ck_rhs_map_probe_rm(struct ck_rhs *hs,
581 struct ck_rhs_map *map,
582 unsigned long *n_probes,
587 unsigned long probe_limit,
588 enum ck_rhs_probe_behavior behavior)
593 unsigned long offset, probes, opl;
596 /* If we are storing object pointers, then we may leverage pointer packing. */
597 unsigned long hv = 0;
599 if (hs->mode & CK_RHS_MODE_OBJECT) {
600 hv = (h >> 25) & CK_RHS_KEY_MASK;
601 compare = CK_RHS_VMA(key);
609 if (behavior != CK_RHS_PROBE_ROBIN_HOOD) {
611 offset = h & map->mask;
613 /* Restart from the bucket we were previously in */
615 offset = ck_rhs_map_probe_next(map, *priority,
621 if (probes++ == probe_limit) {
622 if (probe_limit == opl || pr != -1) {
627 * If no eligible slot has been found yet, continue probe
628 * sequence with original probe limit.
632 k = ck_pr_load_ptr(&map->entries.no_entries.entries[offset]);
633 if (k == CK_RHS_EMPTY)
636 if (behavior != CK_RHS_PROBE_NO_RH) {
637 struct ck_rhs_entry_desc *desc = (void *)&map->entries.no_entries.descs[offset];
640 desc->in_rh == false && desc->probes < probes) {
644 if (behavior == CK_RHS_PROBE_RH ||
645 behavior == CK_RHS_PROBE_ROBIN_HOOD) {
652 if (behavior != CK_RHS_PROBE_ROBIN_HOOD) {
654 if (hs->mode & CK_RHS_MODE_OBJECT) {
655 if (((uintptr_t)k >> CK_MD_VMA_BITS) != hv) {
656 offset = ck_rhs_map_probe_next(map, offset, probes);
667 if (hs->compare == NULL) {
668 offset = ck_rhs_map_probe_next(map, offset, probes);
672 if (hs->compare(k, key) == true)
675 offset = ck_rhs_map_probe_next(map, offset, probes);
678 if (probes > probe_limit) {
692 ck_rhs_map_probe(struct ck_rhs *hs,
693 struct ck_rhs_map *map,
694 unsigned long *n_probes,
699 unsigned long probe_limit,
700 enum ck_rhs_probe_behavior behavior)
705 unsigned long offset, probes, opl;
708 /* If we are storing object pointers, then we may leverage pointer packing. */
709 unsigned long hv = 0;
711 if (hs->mode & CK_RHS_MODE_OBJECT) {
712 hv = (h >> 25) & CK_RHS_KEY_MASK;
713 compare = CK_RHS_VMA(key);
722 if (behavior != CK_RHS_PROBE_ROBIN_HOOD) {
724 offset = h & map->mask;
726 /* Restart from the bucket we were previously in */
728 offset = ck_rhs_map_probe_next(map, *priority,
732 if (behavior == CK_RHS_PROBE_INSERT)
733 probe_limit = ck_rhs_map_bound_get(map, h);
736 if (probes++ == probe_limit) {
737 if (probe_limit == opl || pr != -1) {
742 * If no eligible slot has been found yet, continue probe
743 * sequence with original probe limit.
747 k = ck_pr_load_ptr(&map->entries.descs[offset].entry);
748 if (k == CK_RHS_EMPTY)
750 if ((behavior != CK_RHS_PROBE_NO_RH)) {
751 struct ck_rhs_entry_desc *desc = &map->entries.descs[offset];
754 desc->in_rh == false && desc->probes < probes) {
758 if (behavior == CK_RHS_PROBE_RH ||
759 behavior == CK_RHS_PROBE_ROBIN_HOOD) {
766 if (behavior != CK_RHS_PROBE_ROBIN_HOOD) {
768 if (hs->mode & CK_RHS_MODE_OBJECT) {
769 if (((uintptr_t)k >> CK_MD_VMA_BITS) != hv) {
770 offset = ck_rhs_map_probe_next(map, offset, probes);
781 if (hs->compare == NULL) {
782 offset = ck_rhs_map_probe_next(map, offset, probes);
786 if (hs->compare(k, key) == true)
789 offset = ck_rhs_map_probe_next(map, offset, probes);
792 if (probes > probe_limit) {
805 static inline const void *
806 ck_rhs_marshal(unsigned int mode, const void *key, unsigned long h)
811 if (mode & CK_RHS_MODE_OBJECT) {
812 insert = (void *)((uintptr_t)CK_RHS_VMA(key) | ((h >> 25) << CK_MD_VMA_BITS));
827 ck_rhs_gc(struct ck_rhs *hs)
830 struct ck_rhs_map *map = hs->map;
832 unsigned int max_probes = 0;
833 for (i = 0; i < map->capacity; i++) {
834 if (ck_rhs_probes(map, i) > max_probes)
835 max_probes = ck_rhs_probes(map, i);
837 map->probe_maximum = max_probes;
842 ck_rhs_add_wanted(struct ck_rhs *hs, long end_offset, long old_slot,
845 struct ck_rhs_map *map = hs->map;
847 unsigned int probes = 1;
848 bool found_slot = false;
849 struct ck_rhs_entry_desc *desc;
851 offset = h & map->mask;
855 while (offset != end_offset) {
856 if (offset == old_slot)
859 desc = ck_rhs_desc(map, offset);
860 if (desc->wanted < CK_RHS_MAX_WANTED)
863 offset = ck_rhs_map_probe_next(map, offset, probes);
869 ck_rhs_remove_wanted(struct ck_rhs *hs, long offset, long limit)
871 struct ck_rhs_map *map = hs->map;
872 int probes = ck_rhs_probes(map, offset);
873 bool do_remove = true;
874 struct ck_rhs_entry_desc *desc;
878 offset = ck_rhs_map_probe_prev(map, offset, probes);
882 desc = ck_rhs_desc(map, offset);
883 if (desc->wanted != CK_RHS_MAX_WANTED)
891 ck_rhs_get_first_offset(struct ck_rhs_map *map, unsigned long offset, unsigned int probes)
893 while (probes > (unsigned long)map->offset_mask + 1) {
894 offset -= ((probes - 1) &~ map->offset_mask);
896 offset = (offset &~ map->offset_mask) +
897 ((offset - map->offset_mask) & map->offset_mask);
898 probes -= map->offset_mask + 1;
900 return ((offset &~ map->offset_mask) + ((offset - (probes - 1)) & map->offset_mask));
903 #define CK_RHS_MAX_RH 512
906 ck_rhs_put_robin_hood(struct ck_rhs *hs,
907 long orig_slot, struct ck_rhs_entry_desc *desc)
910 const void *object, *insert;
911 unsigned long n_probes;
912 struct ck_rhs_map *map;
916 long prevs[CK_RHS_MAX_RH];
917 unsigned int prevs_nb = 0;
922 n_probes = desc->probes;
924 key = CK_CC_DECONST_PTR(ck_rhs_entry(map, first));
927 if (hs->mode & CK_RHS_MODE_OBJECT)
928 key = CK_RHS_VMA(key);
931 ck_rhs_set_rh(map, orig_slot);
933 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object,
934 map->probe_limit, prevs_nb == CK_RHS_MAX_RH ?
935 CK_RHS_PROBE_NO_RH : CK_RHS_PROBE_ROBIN_HOOD);
937 if (slot == -1 && first == -1) {
938 if (ck_rhs_grow(hs, map->capacity << 1) == false) {
941 for (i = 0; i < prevs_nb; i++)
942 ck_rhs_unset_rh(map, prevs[i]);
951 desc = ck_rhs_desc(map, first);
952 int old_probes = desc->probes;
954 desc->probes = n_probes;
955 h = ck_rhs_get_first_offset(map, first, n_probes);
956 ck_rhs_map_bound_set(map, h, n_probes);
958 prevs[prevs_nb++] = prev;
959 n_probes = old_probes;
962 /* An empty slot was found. */
963 h = ck_rhs_get_first_offset(map, slot, n_probes);
964 ck_rhs_map_bound_set(map, h, n_probes);
965 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), insert);
966 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
967 ck_pr_fence_atomic_store();
968 ck_rhs_set_probes(map, slot, n_probes);
970 ck_rhs_add_wanted(hs, slot, orig_slot, h);
972 while (prevs_nb > 0) {
973 prev = prevs[--prevs_nb];
974 ck_pr_store_ptr(ck_rhs_entry_addr(map, orig_slot),
975 ck_rhs_entry(map, prev));
976 h = ck_rhs_get_first_offset(map, orig_slot,
978 ck_rhs_add_wanted(hs, orig_slot, prev, h);
979 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
980 ck_pr_fence_atomic_store();
983 desc = ck_rhs_desc(map, orig_slot);
989 ck_rhs_do_backward_shift_delete(struct ck_rhs *hs, long slot)
991 struct ck_rhs_map *map = hs->map;
992 struct ck_rhs_entry_desc *desc, *new_desc = NULL;
995 desc = ck_rhs_desc(map, slot);
996 h = ck_rhs_remove_wanted(hs, slot, -1);
997 while (desc->wanted > 0) {
998 unsigned long offset = 0, tmp_offset;
999 unsigned long wanted_probes = 1;
1000 unsigned int probe = 0;
1001 unsigned int max_probes;
1003 /* Find a successor */
1004 while (wanted_probes < map->probe_maximum) {
1005 probe = wanted_probes;
1006 offset = ck_rhs_map_probe_next(map, slot, probe);
1007 while (probe < map->probe_maximum) {
1008 new_desc = ck_rhs_desc(map, offset);
1009 if (new_desc->probes == probe + 1)
1012 offset = ck_rhs_map_probe_next(map, offset,
1015 if (probe < map->probe_maximum)
1019 if (!(wanted_probes < map->probe_maximum)) {
1023 desc->probes = wanted_probes;
1024 h = ck_rhs_remove_wanted(hs, offset, slot);
1025 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot),
1026 ck_rhs_entry(map, offset));
1027 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
1028 ck_pr_fence_atomic_store();
1029 if (wanted_probes < CK_RHS_WORD_MAX) {
1030 struct ck_rhs_entry_desc *hdesc = ck_rhs_desc(map, h);
1031 if (hdesc->wanted == 1)
1032 CK_RHS_STORE(&hdesc->probe_bound,
1034 else if (hdesc->probe_bound == CK_RHS_WORD_MAX ||
1035 hdesc->probe_bound == new_desc->probes) {
1037 if (hdesc->probe_bound == CK_RHS_WORD_MAX)
1038 max_probes = map->probe_maximum;
1040 max_probes = hdesc->probe_bound;
1043 tmp_offset = ck_rhs_map_probe_next(map, offset,
1045 while (probe < max_probes) {
1046 if (h == (unsigned long)ck_rhs_get_first_offset(map, tmp_offset, probe))
1049 tmp_offset = ck_rhs_map_probe_next(map, tmp_offset, probe);
1051 if (probe == max_probes)
1052 CK_RHS_STORE(&hdesc->probe_bound,
1056 if (desc->wanted < CK_RHS_MAX_WANTED)
1061 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), CK_RHS_EMPTY);
1062 if ((desc->probes - 1) < CK_RHS_WORD_MAX)
1063 CK_RHS_STORE(ck_rhs_probe_bound_addr(map, h),
1069 ck_rhs_fas(struct ck_rhs *hs,
1077 unsigned long n_probes;
1078 struct ck_rhs_map *map = hs->map;
1079 struct ck_rhs_entry_desc *desc, *desc2;
1083 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object,
1084 ck_rhs_map_bound_get(map, h), CK_RHS_PROBE);
1086 /* Replacement semantics presume existence. */
1090 insert = ck_rhs_marshal(hs->mode, key, h);
1095 desc = ck_rhs_desc(map, slot);
1096 desc2 = ck_rhs_desc(map, first);
1098 ret = ck_rhs_put_robin_hood(hs, first, desc2);
1099 desc->in_rh = false;
1100 if (CK_CC_UNLIKELY(ret == 1))
1102 else if (CK_CC_UNLIKELY(ret != 0))
1104 ck_pr_store_ptr(ck_rhs_entry_addr(map, first), insert);
1105 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
1106 ck_pr_fence_atomic_store();
1107 desc2->probes = n_probes;
1108 ck_rhs_add_wanted(hs, first, -1, h);
1109 ck_rhs_do_backward_shift_delete(hs, slot);
1111 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), insert);
1112 ck_rhs_set_probes(map, slot, n_probes);
1114 *previous = CK_CC_DECONST_PTR(object);
1119 * An apply function takes two arguments. The first argument is a pointer to a
1120 * pre-existing object. The second argument is a pointer to the fifth argument
1121 * passed to ck_hs_apply. If a non-NULL pointer is passed to the first argument
1122 * and the return value of the apply function is NULL, then the pre-existing
1123 * value is deleted. If the return pointer is the same as the one passed to the
1124 * apply function then no changes are made to the hash table. If the first
1125 * argument is non-NULL and the return pointer is different than that passed to
1126 * the apply function, then the pre-existing value is replaced. For
1127 * replacement, it is required that the value itself is identical to the
1131 ck_rhs_apply(struct ck_rhs *hs,
1134 ck_rhs_apply_fn_t *fn,
1138 const void *object, *delta = false;
1139 unsigned long n_probes;
1141 struct ck_rhs_map *map;
1142 bool delta_set = false;
1147 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object, map->probe_limit, CK_RHS_PROBE_INSERT);
1148 if (slot == -1 && first == -1) {
1149 if (ck_rhs_grow(hs, map->capacity << 1) == false)
1155 delta = fn(CK_CC_DECONST_PTR(object), cl);
1159 if (delta == NULL) {
1161 * The apply function has requested deletion. If the object doesn't exist,
1164 if (CK_CC_UNLIKELY(object == NULL))
1167 /* Otherwise, delete it. */
1168 ck_rhs_do_backward_shift_delete(hs, slot);
1172 /* The apply function has not requested hash set modification so exit early. */
1173 if (delta == object)
1176 /* A modification or insertion has been requested. */
1177 ck_rhs_map_bound_set(map, h, n_probes);
1179 insert = ck_rhs_marshal(hs->mode, delta, h);
1182 * This follows the same semantics as ck_hs_set, please refer to that
1183 * function for documentation.
1185 struct ck_rhs_entry_desc *desc = NULL, *desc2;
1187 desc = ck_rhs_desc(map, slot);
1190 desc2 = ck_rhs_desc(map, first);
1191 int ret = ck_rhs_put_robin_hood(hs, first, desc2);
1193 desc->in_rh = false;
1195 if (CK_CC_UNLIKELY(ret == 1))
1197 if (CK_CC_UNLIKELY(ret == -1))
1199 /* If an earlier bucket was found, then store entry there. */
1200 ck_pr_store_ptr(ck_rhs_entry_addr(map, first), insert);
1201 desc2->probes = n_probes;
1203 * If a duplicate key was found, then delete it after
1204 * signaling concurrent probes to restart. Optionally,
1205 * it is possible to install tombstone after grace
1206 * period if we can guarantee earlier position of
1209 ck_rhs_add_wanted(hs, first, -1, h);
1210 if (object != NULL) {
1211 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
1212 ck_pr_fence_atomic_store();
1213 ck_rhs_do_backward_shift_delete(hs, slot);
1217 * If we are storing into same slot, then atomic store is sufficient
1220 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), insert);
1221 ck_rhs_set_probes(map, slot, n_probes);
1223 ck_rhs_add_wanted(hs, slot, -1, h);
1226 if (object == NULL) {
1228 if ((map->n_entries ) > map->max_entries)
1229 ck_rhs_grow(hs, map->capacity << 1);
1235 ck_rhs_set(struct ck_rhs *hs,
1243 unsigned long n_probes;
1244 struct ck_rhs_map *map;
1251 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object, map->probe_limit, CK_RHS_PROBE_INSERT);
1252 if (slot == -1 && first == -1) {
1253 if (ck_rhs_grow(hs, map->capacity << 1) == false)
1258 ck_rhs_map_bound_set(map, h, n_probes);
1259 insert = ck_rhs_marshal(hs->mode, key, h);
1262 struct ck_rhs_entry_desc *desc = NULL, *desc2;
1264 desc = ck_rhs_desc(map, slot);
1267 desc2 = ck_rhs_desc(map, first);
1268 int ret = ck_rhs_put_robin_hood(hs, first, desc2);
1270 desc->in_rh = false;
1272 if (CK_CC_UNLIKELY(ret == 1))
1274 if (CK_CC_UNLIKELY(ret == -1))
1276 /* If an earlier bucket was found, then store entry there. */
1277 ck_pr_store_ptr(ck_rhs_entry_addr(map, first), insert);
1278 desc2->probes = n_probes;
1280 * If a duplicate key was found, then delete it after
1281 * signaling concurrent probes to restart. Optionally,
1282 * it is possible to install tombstone after grace
1283 * period if we can guarantee earlier position of
1286 ck_rhs_add_wanted(hs, first, -1, h);
1287 if (object != NULL) {
1288 ck_pr_inc_uint(&map->generation[h & CK_RHS_G_MASK]);
1289 ck_pr_fence_atomic_store();
1290 ck_rhs_do_backward_shift_delete(hs, slot);
1295 * If we are storing into same slot, then atomic store is sufficient
1298 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), insert);
1299 ck_rhs_set_probes(map, slot, n_probes);
1301 ck_rhs_add_wanted(hs, slot, -1, h);
1304 if (object == NULL) {
1306 if ((map->n_entries ) > map->max_entries)
1307 ck_rhs_grow(hs, map->capacity << 1);
1310 *previous = CK_CC_DECONST_PTR(object);
1315 ck_rhs_put_internal(struct ck_rhs *hs,
1318 enum ck_rhs_probe_behavior behavior)
1323 unsigned long n_probes;
1324 struct ck_rhs_map *map;
1329 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object,
1330 map->probe_limit, behavior);
1332 if (slot == -1 && first == -1) {
1333 if (ck_rhs_grow(hs, map->capacity << 1) == false)
1339 /* Fail operation if a match was found. */
1343 ck_rhs_map_bound_set(map, h, n_probes);
1344 insert = ck_rhs_marshal(hs->mode, key, h);
1347 struct ck_rhs_entry_desc *desc = ck_rhs_desc(map, first);
1348 int ret = ck_rhs_put_robin_hood(hs, first, desc);
1349 if (CK_CC_UNLIKELY(ret == 1))
1350 return ck_rhs_put_internal(hs, h, key, behavior);
1351 else if (CK_CC_UNLIKELY(ret == -1))
1353 /* Insert key into first bucket in probe sequence. */
1354 ck_pr_store_ptr(ck_rhs_entry_addr(map, first), insert);
1355 desc->probes = n_probes;
1356 ck_rhs_add_wanted(hs, first, -1, h);
1358 /* An empty slot was found. */
1359 ck_pr_store_ptr(ck_rhs_entry_addr(map, slot), insert);
1360 ck_rhs_set_probes(map, slot, n_probes);
1361 ck_rhs_add_wanted(hs, slot, -1, h);
1365 if ((map->n_entries ) > map->max_entries)
1366 ck_rhs_grow(hs, map->capacity << 1);
1371 ck_rhs_put(struct ck_rhs *hs,
1376 return ck_rhs_put_internal(hs, h, key, CK_RHS_PROBE_INSERT);
1380 ck_rhs_put_unique(struct ck_rhs *hs,
1385 return ck_rhs_put_internal(hs, h, key, CK_RHS_PROBE_RH);
1389 ck_rhs_get(struct ck_rhs *hs,
1395 struct ck_rhs_map *map;
1396 unsigned long n_probes;
1397 unsigned int g, g_p, probe;
1398 unsigned int *generation;
1401 map = ck_pr_load_ptr(&hs->map);
1402 generation = &map->generation[h & CK_RHS_G_MASK];
1403 g = ck_pr_load_uint(generation);
1404 probe = ck_rhs_map_bound_get(map, h);
1408 map->probe_func(hs, map, &n_probes, &first, h, key, &object, probe, CK_RHS_PROBE_NO_RH);
1411 g_p = ck_pr_load_uint(generation);
1414 return CK_CC_DECONST_PTR(object);
1418 ck_rhs_remove(struct ck_rhs *hs,
1424 struct ck_rhs_map *map = hs->map;
1425 unsigned long n_probes;
1427 slot = map->probe_func(hs, map, &n_probes, &first, h, key, &object,
1428 ck_rhs_map_bound_get(map, h), CK_RHS_PROBE_NO_RH);
1433 ck_rhs_do_backward_shift_delete(hs, slot);
1434 return CK_CC_DECONST_PTR(object);
1438 ck_rhs_move(struct ck_rhs *hs,
1439 struct ck_rhs *source,
1440 ck_rhs_hash_cb_t *hf,
1441 ck_rhs_compare_cb_t *compare,
1442 struct ck_malloc *m)
1445 if (m == NULL || m->malloc == NULL || m->free == NULL || hf == NULL)
1448 hs->mode = source->mode;
1449 hs->seed = source->seed;
1450 hs->map = source->map;
1451 hs->load_factor = source->load_factor;
1454 hs->compare = compare;
1459 ck_rhs_init(struct ck_rhs *hs,
1461 ck_rhs_hash_cb_t *hf,
1462 ck_rhs_compare_cb_t *compare,
1463 struct ck_malloc *m,
1464 unsigned long n_entries,
1468 if (m == NULL || m->malloc == NULL || m->free == NULL || hf == NULL)
1475 hs->compare = compare;
1476 hs->load_factor = CK_RHS_DEFAULT_LOAD_FACTOR;
1478 hs->map = ck_rhs_map_create(hs, n_entries);
1479 return hs->map != NULL;