2 * Copyright 2012-2015 Samy Al Bahra.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * This implementation borrows several techniques from Josh Dybnis's
32 * nbds library which can be found at http://code.google.com/p/nbds
34 * This release currently only includes support for 64-bit platforms.
35 * We can address 32-bit platforms in a future release.
40 #include <ck_stdint.h>
41 #include <ck_stdbool.h>
42 #include <ck_string.h>
44 #include "ck_ht_hash.h"
45 #include "ck_internal.h"
47 #ifndef CK_HT_BUCKET_LENGTH
50 #define CK_HT_BUCKET_SHIFT 2ULL
52 #define CK_HT_BUCKET_SHIFT 1ULL
55 #define CK_HT_BUCKET_LENGTH (1U << CK_HT_BUCKET_SHIFT)
56 #define CK_HT_BUCKET_MASK (CK_HT_BUCKET_LENGTH - 1)
59 #ifndef CK_HT_PROBE_DEFAULT
60 #define CK_HT_PROBE_DEFAULT 64ULL
63 #if defined(CK_F_PR_LOAD_8) && defined(CK_F_PR_STORE_8)
64 #define CK_HT_WORD uint8_t
65 #define CK_HT_WORD_MAX UINT8_MAX
66 #define CK_HT_STORE(x, y) ck_pr_store_8(x, y)
67 #define CK_HT_LOAD(x) ck_pr_load_8(x)
68 #elif defined(CK_F_PR_LOAD_16) && defined(CK_F_PR_STORE_16)
69 #define CK_HT_WORD uint16_t
70 #define CK_HT_WORD_MAX UINT16_MAX
71 #define CK_HT_STORE(x, y) ck_pr_store_16(x, y)
72 #define CK_HT_LOAD(x) ck_pr_load_16(x)
73 #elif defined(CK_F_PR_LOAD_32) && defined(CK_F_PR_STORE_32)
74 #define CK_HT_WORD uint32_t
75 #define CK_HT_WORD_MAX UINT32_MAX
76 #define CK_HT_STORE(x, y) ck_pr_store_32(x, y)
77 #define CK_HT_LOAD(x) ck_pr_load_32(x)
79 #error "ck_ht is not supported on your platform."
85 CK_HT_TYPE probe_maximum;
86 CK_HT_TYPE probe_length;
87 CK_HT_TYPE probe_limit;
93 CK_HT_WORD *probe_bound;
94 struct ck_ht_entry *entries;
98 ck_ht_stat(struct ck_ht *table,
99 struct ck_ht_stat *st)
101 struct ck_ht_map *map = table->map;
103 st->n_entries = map->n_entries;
104 st->probe_maximum = map->probe_maximum;
109 ck_ht_hash(struct ck_ht_hash *h,
115 table->h(h, key, key_length, table->seed);
120 ck_ht_hash_direct(struct ck_ht_hash *h,
125 ck_ht_hash(h, table, &key, sizeof(key));
130 ck_ht_hash_wrapper(struct ck_ht_hash *h,
136 h->value = (unsigned long)MurmurHash64A(key, length, seed);
140 static struct ck_ht_map *
141 ck_ht_map_create(struct ck_ht *table, CK_HT_TYPE entries)
143 struct ck_ht_map *map;
148 n_entries = ck_internal_power_2(entries);
149 if (n_entries < CK_HT_BUCKET_LENGTH)
150 n_entries = CK_HT_BUCKET_LENGTH;
152 size = sizeof(struct ck_ht_map) +
153 (sizeof(struct ck_ht_entry) * n_entries + CK_MD_CACHELINE - 1);
155 if (table->mode & CK_HT_WORKLOAD_DELETE) {
156 prefix = sizeof(CK_HT_WORD) * n_entries;
162 map = table->m->malloc(size);
166 map->mode = table->mode;
168 map->probe_limit = ck_internal_max_64(n_entries >>
169 (CK_HT_BUCKET_SHIFT + 2), CK_HT_PROBE_DEFAULT);
172 map->probe_maximum = 0;
173 map->capacity = n_entries;
174 map->step = ck_internal_bsf_64(map->capacity);
175 map->mask = map->capacity - 1;
177 map->entries = (struct ck_ht_entry *)(((uintptr_t)&map[1] + prefix +
178 CK_MD_CACHELINE - 1) & ~(CK_MD_CACHELINE - 1));
180 if (table->mode & CK_HT_WORKLOAD_DELETE) {
181 map->probe_bound = (CK_HT_WORD *)&map[1];
182 memset(map->probe_bound, 0, prefix);
184 map->probe_bound = NULL;
187 memset(map->entries, 0, sizeof(struct ck_ht_entry) * n_entries);
193 ck_ht_map_bound_set(struct ck_ht_map *m,
197 CK_HT_TYPE offset = h.value & m->mask;
199 if (n_probes > m->probe_maximum)
200 CK_HT_TYPE_STORE(&m->probe_maximum, n_probes);
202 if (m->probe_bound != NULL && m->probe_bound[offset] < n_probes) {
203 if (n_probes >= CK_HT_WORD_MAX)
204 n_probes = CK_HT_WORD_MAX;
206 CK_HT_STORE(&m->probe_bound[offset], n_probes);
213 static inline CK_HT_TYPE
214 ck_ht_map_bound_get(struct ck_ht_map *m, struct ck_ht_hash h)
216 CK_HT_TYPE offset = h.value & m->mask;
217 CK_HT_TYPE r = CK_HT_WORD_MAX;
219 if (m->probe_bound != NULL) {
220 r = CK_HT_LOAD(&m->probe_bound[offset]);
221 if (r == CK_HT_WORD_MAX)
222 r = CK_HT_TYPE_LOAD(&m->probe_maximum);
224 r = CK_HT_TYPE_LOAD(&m->probe_maximum);
231 ck_ht_map_destroy(struct ck_malloc *m, struct ck_ht_map *map, bool defer)
234 m->free(map, map->size, defer);
239 ck_ht_map_probe_next(struct ck_ht_map *map, size_t offset, ck_ht_hash_t h, size_t probes)
243 unsigned long level = (unsigned long)probes >> CK_HT_BUCKET_SHIFT;
245 r.value = (h.value >> map->step) >> level;
246 stride = (r.value & ~CK_HT_BUCKET_MASK) << 1
247 | (r.value & CK_HT_BUCKET_MASK);
249 return (offset + level +
250 (stride | CK_HT_BUCKET_LENGTH)) & map->mask;
254 ck_ht_init(struct ck_ht *table,
262 if (m == NULL || m->malloc == NULL || m->free == NULL)
270 table->h = ck_ht_hash_wrapper;
275 table->map = ck_ht_map_create(table, entries);
276 return table->map != NULL;
279 static struct ck_ht_entry *
280 ck_ht_map_probe_wr(struct ck_ht_map *map,
282 ck_ht_entry_t *snapshot,
283 ck_ht_entry_t **available,
286 CK_HT_TYPE *probe_limit,
287 CK_HT_TYPE *probe_wr)
289 struct ck_ht_entry *bucket, *cursor;
290 struct ck_ht_entry *first = NULL;
292 CK_HT_TYPE probes = 0;
295 if (probe_limit == NULL) {
296 limit = ck_ht_map_bound_get(map, h);
298 limit = CK_HT_TYPE_MAX;
301 offset = h.value & map->mask;
302 for (i = 0; i < map->probe_limit; i++) {
304 * Probe on a complete cache line first. Scan forward and wrap around to
305 * the beginning of the cache line. Only when the complete cache line has
306 * been scanned do we move on to the next row.
308 bucket = (void *)((uintptr_t)(map->entries + offset) &
309 ~(CK_MD_CACHELINE - 1));
311 for (j = 0; j < CK_HT_BUCKET_LENGTH; j++) {
314 if (probes++ > limit)
317 cursor = bucket + ((j + offset) & (CK_HT_BUCKET_LENGTH - 1));
320 * It is probably worth it to encapsulate probe state
321 * in order to prevent a complete reprobe sequence in
322 * the case of intermittent writers.
324 if (cursor->key == CK_HT_KEY_TOMBSTONE) {
333 if (cursor->key == CK_HT_KEY_EMPTY)
336 if (cursor->key == (uintptr_t)key)
339 if (map->mode & CK_HT_MODE_BYTESTRING) {
343 * Check memoized portion of hash value before
344 * expensive full-length comparison.
346 k = ck_ht_entry_key_length(cursor);
351 if ((cursor->value >> CK_MD_VMA_BITS) != ((h.value >> 32) & CK_HT_KEY_MASK))
354 if (cursor->hash != h.value)
358 pointer = ck_ht_entry_key(cursor);
359 if (memcmp(pointer, key, key_length) == 0)
364 offset = ck_ht_map_probe_next(map, offset, h, probes);
370 if (probe_limit != NULL) {
371 *probe_limit = probes;
372 } else if (first == NULL) {
378 if (cursor != NULL) {
386 ck_ht_gc(struct ck_ht *ht, unsigned long cycles, unsigned long seed)
388 CK_HT_WORD *bounds = NULL;
389 struct ck_ht_map *map = ht->map;
390 CK_HT_TYPE maximum, i;
393 if (map->n_entries == 0) {
394 CK_HT_TYPE_STORE(&map->probe_maximum, 0);
395 if (map->probe_bound != NULL)
396 memset(map->probe_bound, 0, sizeof(CK_HT_WORD) * map->capacity);
404 if (map->probe_bound != NULL) {
405 size = sizeof(CK_HT_WORD) * map->capacity;
406 bounds = ht->m->malloc(size);
410 memset(bounds, 0, size);
413 maximum = map->probe_maximum;
416 for (i = 0; i < map->capacity; i++) {
417 struct ck_ht_entry *entry, *priority, snapshot;
419 CK_HT_TYPE probes_wr;
422 entry = &map->entries[(i + seed) & map->mask];
423 if (entry->key == CK_HT_KEY_EMPTY ||
424 entry->key == CK_HT_KEY_TOMBSTONE) {
428 if (ht->mode & CK_HT_MODE_BYTESTRING) {
430 h.value = entry->hash;
432 ht->h(&h, ck_ht_entry_key(entry), ck_ht_entry_key_length(entry),
435 entry = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
436 ck_ht_entry_key(entry),
437 ck_ht_entry_key_length(entry),
441 h.value = entry->hash;
443 ht->h(&h, &entry->key, sizeof(entry->key), ht->seed);
445 entry = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
451 offset = h.value & map->mask;
453 if (priority != NULL) {
454 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
457 CK_HT_TYPE_STORE(&priority->key_length, entry->key_length);
458 CK_HT_TYPE_STORE(&priority->hash, entry->hash);
460 ck_pr_store_ptr_unsafe(&priority->value, (void *)entry->value);
462 ck_pr_store_ptr_unsafe(&priority->key, (void *)entry->key);
464 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
466 ck_pr_store_ptr_unsafe(&entry->key, (void *)CK_HT_KEY_TOMBSTONE);
471 if (probes_wr > maximum)
474 if (probes_wr >= CK_HT_WORD_MAX)
475 probes_wr = CK_HT_WORD_MAX;
477 if (bounds != NULL && probes_wr > bounds[offset])
478 bounds[offset] = probes_wr;
479 } else if (--cycles == 0)
483 if (maximum != map->probe_maximum)
484 CK_HT_TYPE_STORE(&map->probe_maximum, maximum);
486 if (bounds != NULL) {
487 for (i = 0; i < map->capacity; i++)
488 CK_HT_STORE(&map->probe_bound[i], bounds[i]);
490 ht->m->free(bounds, size, false);
496 static struct ck_ht_entry *
497 ck_ht_map_probe_rd(struct ck_ht_map *map,
499 ck_ht_entry_t *snapshot,
503 struct ck_ht_entry *bucket, *cursor;
505 CK_HT_TYPE probes = 0;
506 CK_HT_TYPE probe_maximum;
510 CK_HT_TYPE d_prime = 0;
514 probe_maximum = ck_ht_map_bound_get(map, h);
515 offset = h.value & map->mask;
517 for (i = 0; i < map->probe_limit; i++) {
519 * Probe on a complete cache line first. Scan forward and wrap around to
520 * the beginning of the cache line. Only when the complete cache line has
521 * been scanned do we move on to the next row.
523 bucket = (void *)((uintptr_t)(map->entries + offset) &
524 ~(CK_MD_CACHELINE - 1));
526 for (j = 0; j < CK_HT_BUCKET_LENGTH; j++) {
529 if (probes++ > probe_maximum)
532 cursor = bucket + ((j + offset) & (CK_HT_BUCKET_LENGTH - 1));
535 snapshot->key = (uintptr_t)ck_pr_load_ptr(&cursor->key);
537 snapshot->value = (uintptr_t)ck_pr_load_ptr(&cursor->value);
539 d = CK_HT_TYPE_LOAD(&map->deletions);
540 snapshot->key = (uintptr_t)ck_pr_load_ptr(&cursor->key);
542 snapshot->key_length = CK_HT_TYPE_LOAD(&cursor->key_length);
543 snapshot->hash = CK_HT_TYPE_LOAD(&cursor->hash);
544 snapshot->value = (uintptr_t)ck_pr_load_ptr(&cursor->value);
548 * It is probably worth it to encapsulate probe state
549 * in order to prevent a complete reprobe sequence in
550 * the case of intermittent writers.
552 if (snapshot->key == CK_HT_KEY_TOMBSTONE)
555 if (snapshot->key == CK_HT_KEY_EMPTY)
558 if (snapshot->key == (uintptr_t)key)
561 if (map->mode & CK_HT_MODE_BYTESTRING) {
565 * Check memoized portion of hash value before
566 * expensive full-length comparison.
568 k = ck_ht_entry_key_length(snapshot);
572 if ((snapshot->value >> CK_MD_VMA_BITS) != ((h.value >> 32) & CK_HT_KEY_MASK))
575 if (snapshot->hash != h.value)
578 d_prime = CK_HT_TYPE_LOAD(&map->deletions);
581 * It is possible that the slot was
582 * replaced, initiate a re-probe.
588 pointer = ck_ht_entry_key(snapshot);
589 if (memcmp(pointer, key, key_length) == 0)
594 offset = ck_ht_map_probe_next(map, offset, h, probes);
604 ck_ht_count(struct ck_ht *table)
606 struct ck_ht_map *map = ck_pr_load_ptr(&table->map);
608 return CK_HT_TYPE_LOAD(&map->n_entries);
612 ck_ht_next(struct ck_ht *table,
613 struct ck_ht_iterator *i,
614 struct ck_ht_entry **entry)
616 struct ck_ht_map *map = table->map;
619 if (i->offset >= map->capacity)
623 key = map->entries[i->offset].key;
624 if (key != CK_HT_KEY_EMPTY && key != CK_HT_KEY_TOMBSTONE)
626 } while (++i->offset < map->capacity);
628 if (i->offset >= map->capacity)
631 *entry = map->entries + i->offset++;
636 ck_ht_reset_size_spmc(struct ck_ht *table, CK_HT_TYPE size)
638 struct ck_ht_map *map, *update;
641 update = ck_ht_map_create(table, size);
645 ck_pr_store_ptr_unsafe(&table->map, update);
646 ck_ht_map_destroy(table->m, map, true);
651 ck_ht_reset_spmc(struct ck_ht *table)
653 struct ck_ht_map *map = table->map;
655 return ck_ht_reset_size_spmc(table, map->capacity);
659 ck_ht_grow_spmc(struct ck_ht *table, CK_HT_TYPE capacity)
661 struct ck_ht_map *map, *update;
662 struct ck_ht_entry *bucket, *previous;
664 size_t k, i, j, offset;
670 if (map->capacity >= capacity)
673 update = ck_ht_map_create(table, capacity);
677 for (k = 0; k < map->capacity; k++) {
678 previous = &map->entries[k];
680 if (previous->key == CK_HT_KEY_EMPTY || previous->key == CK_HT_KEY_TOMBSTONE)
683 if (table->mode & CK_HT_MODE_BYTESTRING) {
688 key = ck_ht_entry_key(previous);
689 key_length = ck_ht_entry_key_length(previous);
693 h.value = previous->hash;
695 table->h(&h, key, key_length, table->seed);
699 h.value = previous->hash;
701 table->h(&h, &previous->key, sizeof(previous->key), table->seed);
705 offset = h.value & update->mask;
708 for (i = 0; i < update->probe_limit; i++) {
709 bucket = (void *)((uintptr_t)(update->entries + offset) & ~(CK_MD_CACHELINE - 1));
711 for (j = 0; j < CK_HT_BUCKET_LENGTH; j++) {
712 struct ck_ht_entry *cursor = bucket + ((j + offset) & (CK_HT_BUCKET_LENGTH - 1));
715 if (CK_CC_LIKELY(cursor->key == CK_HT_KEY_EMPTY)) {
718 ck_ht_map_bound_set(update, h, probes);
723 if (j < CK_HT_BUCKET_LENGTH)
726 offset = ck_ht_map_probe_next(update, offset, h, probes);
729 if (i == update->probe_limit) {
731 * We have hit the probe limit, the map needs to be even
734 ck_ht_map_destroy(table->m, update, false);
741 ck_pr_store_ptr_unsafe(&table->map, update);
742 ck_ht_map_destroy(table->m, map, true);
747 ck_ht_remove_spmc(struct ck_ht *table,
749 ck_ht_entry_t *entry)
751 struct ck_ht_map *map;
752 struct ck_ht_entry *candidate, snapshot;
756 if (table->mode & CK_HT_MODE_BYTESTRING) {
757 candidate = ck_ht_map_probe_rd(map, h, &snapshot,
758 ck_ht_entry_key(entry),
759 ck_ht_entry_key_length(entry));
761 candidate = ck_ht_map_probe_rd(map, h, &snapshot,
766 /* No matching entry was found. */
767 if (candidate == NULL || snapshot.key == CK_HT_KEY_EMPTY)
772 ck_pr_store_ptr_unsafe(&candidate->key, (void *)CK_HT_KEY_TOMBSTONE);
774 CK_HT_TYPE_STORE(&map->n_entries, map->n_entries - 1);
779 ck_ht_get_spmc(struct ck_ht *table,
781 ck_ht_entry_t *entry)
783 struct ck_ht_entry *candidate, snapshot;
784 struct ck_ht_map *map;
785 CK_HT_TYPE d, d_prime;
788 map = ck_pr_load_ptr(&table->map);
791 * Platforms that cannot read key and key_length atomically must reprobe
792 * on the scan of any single entry.
794 d = CK_HT_TYPE_LOAD(&map->deletions);
796 if (table->mode & CK_HT_MODE_BYTESTRING) {
797 candidate = ck_ht_map_probe_rd(map, h, &snapshot,
798 ck_ht_entry_key(entry), ck_ht_entry_key_length(entry));
800 candidate = ck_ht_map_probe_rd(map, h, &snapshot,
801 (void *)entry->key, sizeof(entry->key));
804 d_prime = CK_HT_TYPE_LOAD(&map->deletions);
807 * It is possible we have read (K, V'). Only valid states are
808 * (K, V), (K', V') and (T, V). Restart load operation in face
809 * of concurrent deletions or replacements.
814 if (candidate == NULL || snapshot.key == CK_HT_KEY_EMPTY)
822 ck_ht_set_spmc(struct ck_ht *table,
824 ck_ht_entry_t *entry)
826 struct ck_ht_entry snapshot, *candidate, *priority;
827 struct ck_ht_map *map;
828 CK_HT_TYPE probes, probes_wr;
834 if (table->mode & CK_HT_MODE_BYTESTRING) {
835 candidate = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
836 ck_ht_entry_key(entry),
837 ck_ht_entry_key_length(entry),
838 &probes, &probes_wr);
840 candidate = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
843 &probes, &probes_wr);
846 if (priority != NULL) {
851 if (candidate != NULL)
854 if (ck_ht_grow_spmc(table, map->capacity << 1) == false)
858 if (candidate == NULL) {
859 candidate = priority;
863 if (candidate->key != CK_HT_KEY_EMPTY &&
864 priority != NULL && candidate != priority) {
866 * Entry is moved into another position in probe sequence.
867 * We avoid a state of (K, B) (where [K, B] -> [K', B]) by
868 * guaranteeing a forced reprobe before transitioning from K to
869 * T. (K, B) implies (K, B, D') so we will reprobe successfully
870 * from this transient state.
875 CK_HT_TYPE_STORE(&priority->key_length, entry->key_length);
876 CK_HT_TYPE_STORE(&priority->hash, entry->hash);
880 * Readers must observe version counter change before they
881 * observe re-use. If they observe re-use, it is at most
884 if (priority->value == CK_HT_KEY_TOMBSTONE) {
885 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
889 ck_pr_store_ptr_unsafe(&priority->value, (void *)entry->value);
891 ck_pr_store_ptr_unsafe(&priority->key, (void *)entry->key);
895 * Make sure that readers who observe the tombstone would
896 * also observe counter change.
898 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
901 ck_pr_store_ptr_unsafe(&candidate->key, (void *)CK_HT_KEY_TOMBSTONE);
905 * In this case we are inserting a new entry or replacing
906 * an existing entry. Yes, this can be combined into above branch,
907 * but isn't because you are actually looking at dying code
908 * (ck_ht is effectively deprecated and is being replaced soon).
910 bool replace = candidate->key != CK_HT_KEY_EMPTY &&
911 candidate->key != CK_HT_KEY_TOMBSTONE;
913 if (priority != NULL) {
914 if (priority->key == CK_HT_KEY_TOMBSTONE) {
915 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
919 candidate = priority;
924 ck_pr_store_ptr_unsafe(&candidate->value, (void *)entry->value);
926 ck_pr_store_ptr_unsafe(&candidate->key, (void *)entry->key);
928 CK_HT_TYPE_STORE(&candidate->key_length, entry->key_length);
929 CK_HT_TYPE_STORE(&candidate->hash, entry->hash);
930 ck_pr_store_ptr_unsafe(&candidate->value, (void *)entry->value);
932 ck_pr_store_ptr_unsafe(&candidate->key, (void *)entry->key);
936 * If we are insert a new entry then increment number
937 * of entries associated with map.
939 if (replace == false)
940 CK_HT_TYPE_STORE(&map->n_entries, map->n_entries + 1);
943 ck_ht_map_bound_set(map, h, probes);
945 /* Enforce a load factor of 0.5. */
946 if (map->n_entries * 2 > map->capacity)
947 ck_ht_grow_spmc(table, map->capacity << 1);
950 entry->key = CK_HT_KEY_EMPTY;
959 ck_ht_put_spmc(struct ck_ht *table,
961 ck_ht_entry_t *entry)
963 struct ck_ht_entry snapshot, *candidate, *priority;
964 struct ck_ht_map *map;
965 CK_HT_TYPE probes, probes_wr;
970 if (table->mode & CK_HT_MODE_BYTESTRING) {
971 candidate = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
972 ck_ht_entry_key(entry),
973 ck_ht_entry_key_length(entry),
974 &probes, &probes_wr);
976 candidate = ck_ht_map_probe_wr(map, h, &snapshot, &priority,
979 &probes, &probes_wr);
982 if (candidate != NULL || priority != NULL)
985 if (ck_ht_grow_spmc(table, map->capacity << 1) == false)
989 if (priority != NULL) {
990 /* Version counter is updated before re-use. */
991 CK_HT_TYPE_STORE(&map->deletions, map->deletions + 1);
994 /* Re-use tombstone if one was found. */
995 candidate = priority;
997 } else if (candidate->key != CK_HT_KEY_EMPTY &&
998 candidate->key != CK_HT_KEY_TOMBSTONE) {
1000 * If the snapshot key is non-empty and the value field is not
1001 * a tombstone then an identical key was found. As store does
1002 * not implement replacement, we will fail.
1007 ck_ht_map_bound_set(map, h, probes);
1010 ck_pr_store_ptr_unsafe(&candidate->value, (void *)entry->value);
1011 ck_pr_fence_store();
1012 ck_pr_store_ptr_unsafe(&candidate->key, (void *)entry->key);
1014 CK_HT_TYPE_STORE(&candidate->key_length, entry->key_length);
1015 CK_HT_TYPE_STORE(&candidate->hash, entry->hash);
1016 ck_pr_store_ptr_unsafe(&candidate->value, (void *)entry->value);
1017 ck_pr_fence_store();
1018 ck_pr_store_ptr_unsafe(&candidate->key, (void *)entry->key);
1021 CK_HT_TYPE_STORE(&map->n_entries, map->n_entries + 1);
1023 /* Enforce a load factor of 0.5. */
1024 if (map->n_entries * 2 > map->capacity)
1025 ck_ht_grow_spmc(table, map->capacity << 1);
1031 ck_ht_destroy(struct ck_ht *table)
1034 ck_ht_map_destroy(table->m, table->map, false);