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
33 #if defined(CK_F_PR_LOAD_64) && defined(CK_F_PR_STORE_64)
34 #define CK_HT_TYPE uint64_t
35 #define CK_HT_TYPE_LOAD ck_pr_load_64
36 #define CK_HT_TYPE_STORE ck_pr_store_64
37 #define CK_HT_TYPE_MAX UINT64_MAX
39 #define CK_HT_TYPE uint32_t
40 #define CK_HT_TYPE_LOAD ck_pr_load_32
41 #define CK_HT_TYPE_STORE ck_pr_store_32
42 #define CK_HT_TYPE_MAX UINT32_MAX
47 #include <ck_malloc.h>
49 #include <ck_stdint.h>
50 #include <ck_stdbool.h>
51 #include <ck_stddef.h>
56 typedef struct ck_ht_hash ck_ht_hash_t;
58 #define CK_HT_MODE_DIRECT 1U
59 #define CK_HT_MODE_BYTESTRING 2U
60 #define CK_HT_WORKLOAD_DELETE 4U
62 #if defined(CK_MD_POINTER_PACK_ENABLE) && defined(CK_MD_VMA_BITS)
64 #define CK_HT_KEY_LENGTH ((sizeof(void *) * 8) - CK_MD_VMA_BITS)
65 #define CK_HT_KEY_MASK ((1U << CK_HT_KEY_LENGTH) - 1)
67 #define CK_HT_KEY_LENGTH 65535U
73 uintptr_t value CK_CC_PACKED;
78 CK_HT_TYPE key_length;
82 typedef struct ck_ht_entry ck_ht_entry_t;
85 * The user is free to define their own stub values.
87 #ifndef CK_HT_KEY_EMPTY
88 #define CK_HT_KEY_EMPTY ((uintptr_t)0)
91 #ifndef CK_HT_KEY_TOMBSTONE
92 #define CK_HT_KEY_TOMBSTONE (~CK_HT_KEY_EMPTY)
96 * Hash callback function. First argument is updated to contain a hash value,
97 * second argument is the key, third argument is key length and final argument
98 * is the hash table seed value.
100 typedef void ck_ht_hash_cb_t(ck_ht_hash_t *, const void *, size_t, uint64_t);
105 struct ck_ht_map *map;
110 typedef struct ck_ht ck_ht_t;
113 uint64_t probe_maximum;
117 struct ck_ht_iterator {
118 struct ck_ht_entry *current;
121 typedef struct ck_ht_iterator ck_ht_iterator_t;
123 #define CK_HT_ITERATOR_INITIALIZER { NULL, 0 }
125 CK_CC_INLINE static void
126 ck_ht_iterator_init(struct ck_ht_iterator *iterator)
129 iterator->current = NULL;
130 iterator->offset = 0;
134 CK_CC_INLINE static bool
135 ck_ht_entry_empty(ck_ht_entry_t *entry)
138 return entry->key == CK_HT_KEY_EMPTY;
141 CK_CC_INLINE static void
142 ck_ht_entry_key_set_direct(ck_ht_entry_t *entry, uintptr_t key)
149 CK_CC_INLINE static void
150 ck_ht_entry_key_set(ck_ht_entry_t *entry, const void *key, uint16_t key_length)
154 entry->key = (uintptr_t)key | ((uintptr_t)key_length << CK_MD_VMA_BITS);
156 entry->key = (uintptr_t)key;
157 entry->key_length = key_length;
163 CK_CC_INLINE static void *
164 ck_ht_entry_key(ck_ht_entry_t *entry)
168 return (void *)(entry->key & (((uintptr_t)1 << CK_MD_VMA_BITS) - 1));
170 return (void *)entry->key;
174 CK_CC_INLINE static uint16_t
175 ck_ht_entry_key_length(ck_ht_entry_t *entry)
179 return entry->key >> CK_MD_VMA_BITS;
181 return entry->key_length;
185 CK_CC_INLINE static void *
186 ck_ht_entry_value(ck_ht_entry_t *entry)
190 return (void *)(entry->value & (((uintptr_t)1 << CK_MD_VMA_BITS) - 1));
192 return (void *)entry->value;
196 CK_CC_INLINE static void
197 ck_ht_entry_set(struct ck_ht_entry *entry,
205 entry->key = (uintptr_t)key | ((uintptr_t)key_length << CK_MD_VMA_BITS);
206 entry->value = (uintptr_t)value | ((uintptr_t)(h.value >> 32) << CK_MD_VMA_BITS);
208 entry->key = (uintptr_t)key;
209 entry->value = (uintptr_t)value;
210 entry->key_length = key_length;
211 entry->hash = h.value;
217 CK_CC_INLINE static void
218 ck_ht_entry_set_direct(struct ck_ht_entry *entry,
225 entry->value = value;
228 entry->hash = h.value;
235 CK_CC_INLINE static uintptr_t
236 ck_ht_entry_key_direct(ck_ht_entry_t *entry)
242 CK_CC_INLINE static uintptr_t
243 ck_ht_entry_value_direct(ck_ht_entry_t *entry)
250 * Iteration must occur without any concurrent mutations on
253 bool ck_ht_next(ck_ht_t *, ck_ht_iterator_t *, ck_ht_entry_t **entry);
255 void ck_ht_stat(ck_ht_t *, struct ck_ht_stat *);
256 void ck_ht_hash(ck_ht_hash_t *, ck_ht_t *, const void *, uint16_t);
257 void ck_ht_hash_direct(ck_ht_hash_t *, ck_ht_t *, uintptr_t);
258 bool ck_ht_init(ck_ht_t *, unsigned int, ck_ht_hash_cb_t *,
259 struct ck_malloc *, CK_HT_TYPE, uint64_t);
260 void ck_ht_destroy(ck_ht_t *);
261 bool ck_ht_set_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *);
262 bool ck_ht_put_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *);
263 bool ck_ht_get_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *);
264 bool ck_ht_gc(struct ck_ht *, unsigned long, unsigned long);
265 bool ck_ht_grow_spmc(ck_ht_t *, CK_HT_TYPE);
266 bool ck_ht_remove_spmc(ck_ht_t *, ck_ht_hash_t, ck_ht_entry_t *);
267 bool ck_ht_reset_spmc(ck_ht_t *);
268 bool ck_ht_reset_size_spmc(ck_ht_t *, CK_HT_TYPE);
269 CK_HT_TYPE ck_ht_count(ck_ht_t *);