subr_thmap.c revision 1.5.6.1 1 1.5.6.1 martin /* $NetBSD: subr_thmap.c,v 1.5.6.1 2020/05/25 17:19:37 martin Exp $ */
2 1.2 christos
3 1.1 rmind /*-
4 1.1 rmind * Copyright (c) 2018 Mindaugas Rasiukevicius <rmind at noxt eu>
5 1.1 rmind * All rights reserved.
6 1.1 rmind *
7 1.1 rmind * Redistribution and use in source and binary forms, with or without
8 1.1 rmind * modification, are permitted provided that the following conditions
9 1.1 rmind * are met:
10 1.1 rmind * 1. Redistributions of source code must retain the above copyright
11 1.1 rmind * notice, this list of conditions and the following disclaimer.
12 1.1 rmind * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 rmind * notice, this list of conditions and the following disclaimer in the
14 1.1 rmind * documentation and/or other materials provided with the distribution.
15 1.1 rmind *
16 1.1 rmind * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.1 rmind * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.1 rmind * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.1 rmind * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.1 rmind * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.1 rmind * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.1 rmind * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.1 rmind * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.1 rmind * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.1 rmind * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.1 rmind * SUCH DAMAGE.
27 1.1 rmind *
28 1.1 rmind * Upstream: https://github.com/rmind/thmap/
29 1.1 rmind */
30 1.1 rmind
31 1.1 rmind /*
32 1.1 rmind * Concurrent trie-hash map.
33 1.1 rmind *
34 1.1 rmind * The data structure is conceptually a radix trie on hashed keys.
35 1.1 rmind * Keys are hashed using a 32-bit function. The root level is a special
36 1.1 rmind * case: it is managed using the compare-and-swap (CAS) atomic operation
37 1.1 rmind * and has a fanout of 64. The subsequent levels are constructed using
38 1.1 rmind * intermediate nodes with a fanout of 16 (using 4 bits). As more levels
39 1.1 rmind * are created, more blocks of the 32-bit hash value might be generated
40 1.1 rmind * by incrementing the seed parameter of the hash function.
41 1.1 rmind *
42 1.1 rmind * Concurrency
43 1.1 rmind *
44 1.1 rmind * - READERS: Descending is simply walking through the slot values of
45 1.1 rmind * the intermediate nodes. It is lock-free as there is no intermediate
46 1.1 rmind * state: the slot is either empty or has a pointer to the child node.
47 1.1 rmind * The main assumptions here are the following:
48 1.1 rmind *
49 1.1 rmind * i) modifications must preserve consistency with the respect to the
50 1.1 rmind * readers i.e. the readers can only see the valid node values;
51 1.1 rmind *
52 1.1 rmind * ii) any invalid view must "fail" the reads, e.g. by making them
53 1.1 rmind * re-try from the root; this is a case for deletions and is achieved
54 1.1 rmind * using the NODE_DELETED flag.
55 1.1 rmind *
56 1.5.6.1 martin * iii) the node destruction must be synchronized with the readers,
57 1.1 rmind * e.g. by using the Epoch-based reclamation or other techniques.
58 1.1 rmind *
59 1.1 rmind * - WRITERS AND LOCKING: Each intermediate node has a spin-lock (which
60 1.1 rmind * is implemented using the NODE_LOCKED bit) -- it provides mutual
61 1.1 rmind * exclusion amongst concurrent writers. The lock order for the nodes
62 1.1 rmind * is "bottom-up" i.e. they are locked as we ascend the trie. A key
63 1.1 rmind * constraint here is that parent pointer never changes.
64 1.1 rmind *
65 1.1 rmind * - DELETES: In addition to writer's locking, the deletion keeps the
66 1.1 rmind * intermediate nodes in a valid state and sets the NODE_DELETED flag,
67 1.1 rmind * to indicate that the readers must re-start the walk from the root.
68 1.1 rmind * As the levels are collapsed, NODE_DELETED gets propagated up-tree.
69 1.1 rmind * The leaf nodes just stay as-is until they are reclaimed.
70 1.1 rmind *
71 1.1 rmind * - ROOT LEVEL: The root level is a special case, as it is implemented
72 1.1 rmind * as an array (rather than intermediate node). The root-level slot can
73 1.1 rmind * only be set using CAS and it can only be set to a valid intermediate
74 1.1 rmind * node. The root-level slot can only be cleared when the node it points
75 1.1 rmind * at becomes empty, is locked and marked as NODE_DELETED (this causes
76 1.1 rmind * the insert/delete operations to re-try until the slot is set to NULL).
77 1.1 rmind *
78 1.1 rmind * References:
79 1.1 rmind *
80 1.1 rmind * W. Litwin, 1981, Trie Hashing.
81 1.1 rmind * Proceedings of the 1981 ACM SIGMOD, p. 19-29
82 1.1 rmind * https://dl.acm.org/citation.cfm?id=582322
83 1.1 rmind *
84 1.1 rmind * P. L. Lehman and S. B. Yao.
85 1.1 rmind * Efficient locking for concurrent operations on B-trees.
86 1.1 rmind * ACM TODS, 6(4):650-670, 1981
87 1.1 rmind * https://www.csd.uoc.gr/~hy460/pdf/p650-lehman.pdf
88 1.1 rmind */
89 1.1 rmind
90 1.1 rmind #ifdef _KERNEL
91 1.1 rmind #include <sys/cdefs.h>
92 1.1 rmind #include <sys/param.h>
93 1.1 rmind #include <sys/types.h>
94 1.1 rmind #include <sys/thmap.h>
95 1.1 rmind #include <sys/kmem.h>
96 1.1 rmind #include <sys/lock.h>
97 1.1 rmind #include <sys/atomic.h>
98 1.1 rmind #include <sys/hash.h>
99 1.2 christos #define THMAP_RCSID(a) __KERNEL_RCSID(0, a)
100 1.1 rmind #else
101 1.1 rmind #include <stdio.h>
102 1.1 rmind #include <stdlib.h>
103 1.1 rmind #include <stdbool.h>
104 1.1 rmind #include <stddef.h>
105 1.1 rmind #include <inttypes.h>
106 1.1 rmind #include <string.h>
107 1.1 rmind #include <limits.h>
108 1.2 christos #define THMAP_RCSID(a) __RCSID(a)
109 1.1 rmind
110 1.1 rmind #include "thmap.h"
111 1.1 rmind #include "utils.h"
112 1.1 rmind #endif
113 1.1 rmind
114 1.5.6.1 martin THMAP_RCSID("$NetBSD: subr_thmap.c,v 1.5.6.1 2020/05/25 17:19:37 martin Exp $");
115 1.2 christos
116 1.1 rmind /*
117 1.1 rmind * NetBSD kernel wrappers
118 1.1 rmind */
119 1.1 rmind #ifdef _KERNEL
120 1.1 rmind #define ASSERT KASSERT
121 1.5.6.1 martin #define atomic_thread_fence(x) membar_sync()
122 1.5.6.1 martin #define atomic_compare_exchange_weak_explicit_32(p, e, n, m1, m2) \
123 1.5.6.1 martin (atomic_cas_32((p), *(e), (n)) == *(e))
124 1.5.6.1 martin #define atomic_compare_exchange_weak_explicit_ptr(p, e, n, m1, m2) \
125 1.5.6.1 martin (atomic_cas_ptr((p), *(void **)(e), (void *)(n)) == *(void **)(e))
126 1.5.6.1 martin #define atomic_exchange_explicit(o, n, m1) atomic_swap_ptr((o), (n))
127 1.1 rmind #define murmurhash3 murmurhash2
128 1.1 rmind #endif
129 1.1 rmind
130 1.1 rmind /*
131 1.1 rmind * The root level fanout is 64 (indexed by the last 6 bits of the hash
132 1.1 rmind * value XORed with the length). Each subsequent level, represented by
133 1.1 rmind * intermediate nodes, has a fanout of 16 (using 4 bits).
134 1.1 rmind *
135 1.1 rmind * The hash function produces 32-bit values.
136 1.1 rmind */
137 1.1 rmind
138 1.1 rmind #define HASHVAL_BITS (32)
139 1.1 rmind #define HASHVAL_MOD (HASHVAL_BITS - 1)
140 1.1 rmind #define HASHVAL_SHIFT (5)
141 1.1 rmind
142 1.1 rmind #define ROOT_BITS (6)
143 1.1 rmind #define ROOT_SIZE (1 << ROOT_BITS)
144 1.1 rmind #define ROOT_MASK (ROOT_SIZE - 1)
145 1.1 rmind #define ROOT_MSBITS (HASHVAL_BITS - ROOT_BITS)
146 1.1 rmind
147 1.1 rmind #define LEVEL_BITS (4)
148 1.1 rmind #define LEVEL_SIZE (1 << LEVEL_BITS)
149 1.1 rmind #define LEVEL_MASK (LEVEL_SIZE - 1)
150 1.1 rmind
151 1.1 rmind /*
152 1.1 rmind * Instead of raw pointers, we use offsets from the base address.
153 1.1 rmind * This accommodates the use of this data structure in shared memory,
154 1.1 rmind * where mappings can be in different address spaces.
155 1.1 rmind *
156 1.1 rmind * The pointers must be aligned, since pointer tagging is used to
157 1.1 rmind * differentiate the intermediate nodes from leaves. We reserve the
158 1.1 rmind * least significant bit.
159 1.1 rmind */
160 1.1 rmind typedef uintptr_t thmap_ptr_t;
161 1.5.6.1 martin typedef uintptr_t atomic_thmap_ptr_t; // C11 _Atomic
162 1.1 rmind
163 1.1 rmind #define THMAP_NULL ((thmap_ptr_t)0)
164 1.1 rmind
165 1.1 rmind #define THMAP_LEAF_BIT (0x1)
166 1.1 rmind
167 1.1 rmind #define THMAP_ALIGNED_P(p) (((uintptr_t)(p) & 3) == 0)
168 1.1 rmind #define THMAP_ALIGN(p) ((uintptr_t)(p) & ~(uintptr_t)3)
169 1.1 rmind #define THMAP_INODE_P(p) (((uintptr_t)(p) & THMAP_LEAF_BIT) == 0)
170 1.1 rmind
171 1.1 rmind #define THMAP_GETPTR(th, p) ((void *)((th)->baseptr + (uintptr_t)(p)))
172 1.1 rmind #define THMAP_GETOFF(th, p) ((thmap_ptr_t)((uintptr_t)(p) - (th)->baseptr))
173 1.1 rmind #define THMAP_NODE(th, p) THMAP_GETPTR(th, THMAP_ALIGN(p))
174 1.1 rmind
175 1.1 rmind /*
176 1.1 rmind * State field.
177 1.1 rmind */
178 1.1 rmind
179 1.1 rmind #define NODE_LOCKED (1U << 31) // lock (writers)
180 1.1 rmind #define NODE_DELETED (1U << 30) // node deleted
181 1.1 rmind #define NODE_COUNT(s) ((s) & 0x3fffffff) // slot count mask
182 1.1 rmind
183 1.1 rmind /*
184 1.1 rmind * There are two types of nodes:
185 1.1 rmind * - Intermediate nodes -- arrays pointing to another level or a leaf;
186 1.1 rmind * - Leaves, which store a key-value pair.
187 1.1 rmind */
188 1.1 rmind
189 1.1 rmind typedef struct {
190 1.5.6.1 martin uint32_t state; // C11 _Atomic
191 1.5.6.1 martin thmap_ptr_t parent;
192 1.5.6.1 martin atomic_thmap_ptr_t slots[LEVEL_SIZE];
193 1.1 rmind } thmap_inode_t;
194 1.1 rmind
195 1.1 rmind #define THMAP_INODE_LEN sizeof(thmap_inode_t)
196 1.1 rmind
197 1.1 rmind typedef struct {
198 1.1 rmind thmap_ptr_t key;
199 1.1 rmind size_t len;
200 1.1 rmind void * val;
201 1.1 rmind } thmap_leaf_t;
202 1.1 rmind
203 1.1 rmind typedef struct {
204 1.1 rmind unsigned rslot; // root-level slot index
205 1.1 rmind unsigned level; // current level in the tree
206 1.1 rmind unsigned hashidx; // current hash index (block of bits)
207 1.1 rmind uint32_t hashval; // current hash value
208 1.1 rmind } thmap_query_t;
209 1.1 rmind
210 1.1 rmind typedef struct {
211 1.1 rmind uintptr_t addr;
212 1.1 rmind size_t len;
213 1.1 rmind void * next;
214 1.1 rmind } thmap_gc_t;
215 1.1 rmind
216 1.1 rmind #define THMAP_ROOT_LEN (sizeof(thmap_ptr_t) * ROOT_SIZE)
217 1.1 rmind
218 1.1 rmind struct thmap {
219 1.5.6.1 martin uintptr_t baseptr;
220 1.5.6.1 martin atomic_thmap_ptr_t * root;
221 1.5.6.1 martin unsigned flags;
222 1.5.6.1 martin const thmap_ops_t * ops;
223 1.5.6.1 martin thmap_gc_t * gc_list; // C11 _Atomic
224 1.1 rmind };
225 1.1 rmind
226 1.1 rmind static void stage_mem_gc(thmap_t *, uintptr_t, size_t);
227 1.1 rmind
228 1.1 rmind /*
229 1.1 rmind * A few low-level helper routines.
230 1.1 rmind */
231 1.1 rmind
232 1.1 rmind static uintptr_t
233 1.1 rmind alloc_wrapper(size_t len)
234 1.1 rmind {
235 1.4 rmind return (uintptr_t)kmem_intr_alloc(len, KM_NOSLEEP);
236 1.1 rmind }
237 1.1 rmind
238 1.1 rmind static void
239 1.1 rmind free_wrapper(uintptr_t addr, size_t len)
240 1.1 rmind {
241 1.1 rmind kmem_intr_free((void *)addr, len);
242 1.1 rmind }
243 1.1 rmind
244 1.1 rmind static const thmap_ops_t thmap_default_ops = {
245 1.1 rmind .alloc = alloc_wrapper,
246 1.1 rmind .free = free_wrapper
247 1.1 rmind };
248 1.1 rmind
249 1.1 rmind /*
250 1.1 rmind * NODE LOCKING.
251 1.1 rmind */
252 1.1 rmind
253 1.2 christos #ifdef DIAGNOSTIC
254 1.1 rmind static inline bool
255 1.5.6.1 martin node_locked_p(thmap_inode_t *node)
256 1.1 rmind {
257 1.5.6.1 martin return (atomic_load_relaxed(&node->state) & NODE_LOCKED) != 0;
258 1.1 rmind }
259 1.1 rmind #endif
260 1.1 rmind
261 1.1 rmind static void
262 1.1 rmind lock_node(thmap_inode_t *node)
263 1.1 rmind {
264 1.1 rmind unsigned bcount = SPINLOCK_BACKOFF_MIN;
265 1.1 rmind uint32_t s;
266 1.1 rmind again:
267 1.5.6.1 martin s = atomic_load_relaxed(&node->state);
268 1.1 rmind if (s & NODE_LOCKED) {
269 1.1 rmind SPINLOCK_BACKOFF(bcount);
270 1.1 rmind goto again;
271 1.1 rmind }
272 1.5.6.1 martin /* Acquire from prior release in unlock_node.() */
273 1.5.6.1 martin if (!atomic_compare_exchange_weak_explicit_32(&node->state,
274 1.5.6.1 martin &s, s | NODE_LOCKED, memory_order_acquire, memory_order_relaxed)) {
275 1.1 rmind bcount = SPINLOCK_BACKOFF_MIN;
276 1.1 rmind goto again;
277 1.1 rmind }
278 1.1 rmind }
279 1.1 rmind
280 1.1 rmind static void
281 1.1 rmind unlock_node(thmap_inode_t *node)
282 1.1 rmind {
283 1.5.6.1 martin uint32_t s = atomic_load_relaxed(&node->state) & ~NODE_LOCKED;
284 1.1 rmind
285 1.1 rmind ASSERT(node_locked_p(node));
286 1.5.6.1 martin /* Release to subsequent acquire in lock_node(). */
287 1.5.6.1 martin atomic_store_release(&node->state, s);
288 1.1 rmind }
289 1.1 rmind
290 1.1 rmind /*
291 1.1 rmind * HASH VALUE AND KEY OPERATIONS.
292 1.1 rmind */
293 1.1 rmind
294 1.1 rmind static inline void
295 1.1 rmind hashval_init(thmap_query_t *query, const void * restrict key, size_t len)
296 1.1 rmind {
297 1.1 rmind const uint32_t hashval = murmurhash3(key, len, 0);
298 1.1 rmind
299 1.1 rmind query->rslot = ((hashval >> ROOT_MSBITS) ^ len) & ROOT_MASK;
300 1.1 rmind query->level = 0;
301 1.1 rmind query->hashval = hashval;
302 1.1 rmind query->hashidx = 0;
303 1.1 rmind }
304 1.1 rmind
305 1.1 rmind /*
306 1.1 rmind * hashval_getslot: given the key, compute the hash (if not already cached)
307 1.1 rmind * and return the offset for the current level.
308 1.1 rmind */
309 1.1 rmind static unsigned
310 1.1 rmind hashval_getslot(thmap_query_t *query, const void * restrict key, size_t len)
311 1.1 rmind {
312 1.1 rmind const unsigned offset = query->level * LEVEL_BITS;
313 1.1 rmind const unsigned shift = offset & HASHVAL_MOD;
314 1.1 rmind const unsigned i = offset >> HASHVAL_SHIFT;
315 1.1 rmind
316 1.1 rmind if (query->hashidx != i) {
317 1.1 rmind /* Generate a hash value for a required range. */
318 1.1 rmind query->hashval = murmurhash3(key, len, i);
319 1.1 rmind query->hashidx = i;
320 1.1 rmind }
321 1.1 rmind return (query->hashval >> shift) & LEVEL_MASK;
322 1.1 rmind }
323 1.1 rmind
324 1.1 rmind static unsigned
325 1.1 rmind hashval_getleafslot(const thmap_t *thmap,
326 1.1 rmind const thmap_leaf_t *leaf, unsigned level)
327 1.1 rmind {
328 1.1 rmind const void *key = THMAP_GETPTR(thmap, leaf->key);
329 1.1 rmind const unsigned offset = level * LEVEL_BITS;
330 1.1 rmind const unsigned shift = offset & HASHVAL_MOD;
331 1.1 rmind const unsigned i = offset >> HASHVAL_SHIFT;
332 1.1 rmind
333 1.1 rmind return (murmurhash3(key, leaf->len, i) >> shift) & LEVEL_MASK;
334 1.1 rmind }
335 1.1 rmind
336 1.1 rmind static inline unsigned
337 1.1 rmind hashval_getl0slot(const thmap_t *thmap, const thmap_query_t *query,
338 1.1 rmind const thmap_leaf_t *leaf)
339 1.1 rmind {
340 1.1 rmind if (__predict_true(query->hashidx == 0)) {
341 1.1 rmind return query->hashval & LEVEL_MASK;
342 1.1 rmind }
343 1.1 rmind return hashval_getleafslot(thmap, leaf, 0);
344 1.1 rmind }
345 1.1 rmind
346 1.1 rmind static bool
347 1.1 rmind key_cmp_p(const thmap_t *thmap, const thmap_leaf_t *leaf,
348 1.1 rmind const void * restrict key, size_t len)
349 1.1 rmind {
350 1.1 rmind const void *leafkey = THMAP_GETPTR(thmap, leaf->key);
351 1.1 rmind return len == leaf->len && memcmp(key, leafkey, len) == 0;
352 1.1 rmind }
353 1.1 rmind
354 1.1 rmind /*
355 1.1 rmind * INTER-NODE OPERATIONS.
356 1.1 rmind */
357 1.1 rmind
358 1.1 rmind static thmap_inode_t *
359 1.1 rmind node_create(thmap_t *thmap, thmap_inode_t *parent)
360 1.1 rmind {
361 1.1 rmind thmap_inode_t *node;
362 1.1 rmind uintptr_t p;
363 1.1 rmind
364 1.1 rmind p = thmap->ops->alloc(THMAP_INODE_LEN);
365 1.1 rmind if (!p) {
366 1.1 rmind return NULL;
367 1.1 rmind }
368 1.1 rmind node = THMAP_GETPTR(thmap, p);
369 1.1 rmind ASSERT(THMAP_ALIGNED_P(node));
370 1.1 rmind
371 1.1 rmind memset(node, 0, THMAP_INODE_LEN);
372 1.1 rmind if (parent) {
373 1.5.6.1 martin /* Not yet published, no need for ordering. */
374 1.5.6.1 martin atomic_store_relaxed(&node->state, NODE_LOCKED);
375 1.1 rmind node->parent = THMAP_GETOFF(thmap, parent);
376 1.1 rmind }
377 1.1 rmind return node;
378 1.1 rmind }
379 1.1 rmind
380 1.1 rmind static void
381 1.1 rmind node_insert(thmap_inode_t *node, unsigned slot, thmap_ptr_t child)
382 1.1 rmind {
383 1.1 rmind ASSERT(node_locked_p(node) || node->parent == THMAP_NULL);
384 1.5.6.1 martin ASSERT((atomic_load_relaxed(&node->state) & NODE_DELETED) == 0);
385 1.5.6.1 martin ASSERT(atomic_load_relaxed(&node->slots[slot]) == THMAP_NULL);
386 1.1 rmind
387 1.5.6.1 martin ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) < LEVEL_SIZE);
388 1.1 rmind
389 1.5.6.1 martin /*
390 1.5.6.1 martin * If node is public already, caller is responsible for issuing
391 1.5.6.1 martin * release fence; if node is not public, no ordering is needed.
392 1.5.6.1 martin * Hence relaxed ordering.
393 1.5.6.1 martin */
394 1.5.6.1 martin atomic_store_relaxed(&node->slots[slot], child);
395 1.5.6.1 martin atomic_store_relaxed(&node->state,
396 1.5.6.1 martin atomic_load_relaxed(&node->state) + 1);
397 1.1 rmind }
398 1.1 rmind
399 1.1 rmind static void
400 1.1 rmind node_remove(thmap_inode_t *node, unsigned slot)
401 1.1 rmind {
402 1.1 rmind ASSERT(node_locked_p(node));
403 1.5.6.1 martin ASSERT((atomic_load_relaxed(&node->state) & NODE_DELETED) == 0);
404 1.5.6.1 martin ASSERT(atomic_load_relaxed(&node->slots[slot]) != THMAP_NULL);
405 1.1 rmind
406 1.5.6.1 martin ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) > 0);
407 1.5.6.1 martin ASSERT(NODE_COUNT(atomic_load_relaxed(&node->state)) <= LEVEL_SIZE);
408 1.1 rmind
409 1.5.6.1 martin /* Element will be GC-ed later; no need for ordering here. */
410 1.5.6.1 martin atomic_store_relaxed(&node->slots[slot], THMAP_NULL);
411 1.5.6.1 martin atomic_store_relaxed(&node->state,
412 1.5.6.1 martin atomic_load_relaxed(&node->state) - 1);
413 1.1 rmind }
414 1.1 rmind
415 1.1 rmind /*
416 1.1 rmind * LEAF OPERATIONS.
417 1.1 rmind */
418 1.1 rmind
419 1.1 rmind static thmap_leaf_t *
420 1.1 rmind leaf_create(const thmap_t *thmap, const void *key, size_t len, void *val)
421 1.1 rmind {
422 1.1 rmind thmap_leaf_t *leaf;
423 1.1 rmind uintptr_t leaf_off, key_off;
424 1.1 rmind
425 1.1 rmind leaf_off = thmap->ops->alloc(sizeof(thmap_leaf_t));
426 1.1 rmind if (!leaf_off) {
427 1.1 rmind return NULL;
428 1.1 rmind }
429 1.1 rmind leaf = THMAP_GETPTR(thmap, leaf_off);
430 1.1 rmind ASSERT(THMAP_ALIGNED_P(leaf));
431 1.1 rmind
432 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
433 1.1 rmind /*
434 1.1 rmind * Copy the key.
435 1.1 rmind */
436 1.1 rmind key_off = thmap->ops->alloc(len);
437 1.1 rmind if (!key_off) {
438 1.1 rmind thmap->ops->free(leaf_off, sizeof(thmap_leaf_t));
439 1.1 rmind return NULL;
440 1.1 rmind }
441 1.1 rmind memcpy(THMAP_GETPTR(thmap, key_off), key, len);
442 1.1 rmind leaf->key = key_off;
443 1.1 rmind } else {
444 1.1 rmind /* Otherwise, we use a reference. */
445 1.1 rmind leaf->key = (uintptr_t)key;
446 1.1 rmind }
447 1.1 rmind leaf->len = len;
448 1.1 rmind leaf->val = val;
449 1.1 rmind return leaf;
450 1.1 rmind }
451 1.1 rmind
452 1.1 rmind static void
453 1.1 rmind leaf_free(const thmap_t *thmap, thmap_leaf_t *leaf)
454 1.1 rmind {
455 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
456 1.1 rmind thmap->ops->free(leaf->key, leaf->len);
457 1.1 rmind }
458 1.1 rmind thmap->ops->free(THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
459 1.1 rmind }
460 1.1 rmind
461 1.1 rmind static thmap_leaf_t *
462 1.1 rmind get_leaf(const thmap_t *thmap, thmap_inode_t *parent, unsigned slot)
463 1.1 rmind {
464 1.1 rmind thmap_ptr_t node;
465 1.1 rmind
466 1.5.6.1 martin /* Consume from prior release in thmap_put(). */
467 1.5.6.1 martin node = atomic_load_consume(&parent->slots[slot]);
468 1.1 rmind if (THMAP_INODE_P(node)) {
469 1.1 rmind return NULL;
470 1.1 rmind }
471 1.1 rmind return THMAP_NODE(thmap, node);
472 1.1 rmind }
473 1.1 rmind
474 1.1 rmind /*
475 1.1 rmind * ROOT OPERATIONS.
476 1.1 rmind */
477 1.1 rmind
478 1.5.6.1 martin /*
479 1.5.6.1 martin * root_try_put: Try to set a root pointer at query->rslot.
480 1.5.6.1 martin *
481 1.5.6.1 martin * => Implies release operation on success.
482 1.5.6.1 martin * => Implies no ordering on failure.
483 1.5.6.1 martin */
484 1.1 rmind static inline bool
485 1.1 rmind root_try_put(thmap_t *thmap, const thmap_query_t *query, thmap_leaf_t *leaf)
486 1.1 rmind {
487 1.5.6.1 martin thmap_ptr_t expected;
488 1.1 rmind const unsigned i = query->rslot;
489 1.1 rmind thmap_inode_t *node;
490 1.1 rmind thmap_ptr_t nptr;
491 1.1 rmind unsigned slot;
492 1.1 rmind
493 1.1 rmind /*
494 1.5.6.1 martin * Must pre-check first. No ordering required because we will
495 1.5.6.1 martin * check again before taking any actions, and start over if
496 1.5.6.1 martin * this changes from null.
497 1.1 rmind */
498 1.5.6.1 martin if (atomic_load_relaxed(&thmap->root[i])) {
499 1.1 rmind return false;
500 1.1 rmind }
501 1.1 rmind
502 1.1 rmind /*
503 1.1 rmind * Create an intermediate node. Since there is no parent set,
504 1.5.6.1 martin * it will be created unlocked and the CAS operation will
505 1.5.6.1 martin * release it to readers.
506 1.1 rmind */
507 1.1 rmind node = node_create(thmap, NULL);
508 1.1 rmind slot = hashval_getl0slot(thmap, query, leaf);
509 1.1 rmind node_insert(node, slot, THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT);
510 1.1 rmind nptr = THMAP_GETOFF(thmap, node);
511 1.1 rmind again:
512 1.5.6.1 martin if (atomic_load_relaxed(&thmap->root[i])) {
513 1.1 rmind thmap->ops->free(nptr, THMAP_INODE_LEN);
514 1.1 rmind return false;
515 1.1 rmind }
516 1.5.6.1 martin /* Release to subsequent consume in find_edge_node(). */
517 1.5.6.1 martin expected = THMAP_NULL;
518 1.5.6.1 martin if (!atomic_compare_exchange_weak_explicit_ptr(&thmap->root[i], &expected,
519 1.5.6.1 martin nptr, memory_order_release, memory_order_relaxed)) {
520 1.1 rmind goto again;
521 1.1 rmind }
522 1.1 rmind return true;
523 1.1 rmind }
524 1.1 rmind
525 1.1 rmind /*
526 1.1 rmind * find_edge_node: given the hash, traverse the tree to find the edge node.
527 1.1 rmind *
528 1.1 rmind * => Returns an aligned (clean) pointer to the parent node.
529 1.1 rmind * => Returns the slot number and sets current level.
530 1.1 rmind */
531 1.1 rmind static thmap_inode_t *
532 1.1 rmind find_edge_node(const thmap_t *thmap, thmap_query_t *query,
533 1.1 rmind const void * restrict key, size_t len, unsigned *slot)
534 1.1 rmind {
535 1.5.6.1 martin thmap_ptr_t root_slot;
536 1.1 rmind thmap_inode_t *parent;
537 1.1 rmind thmap_ptr_t node;
538 1.1 rmind unsigned off;
539 1.1 rmind
540 1.1 rmind ASSERT(query->level == 0);
541 1.1 rmind
542 1.5.6.1 martin /* Consume from prior release in root_try_put(). */
543 1.5.6.1 martin root_slot = atomic_load_consume(&thmap->root[query->rslot]);
544 1.1 rmind parent = THMAP_NODE(thmap, root_slot);
545 1.1 rmind if (!parent) {
546 1.1 rmind return NULL;
547 1.1 rmind }
548 1.1 rmind descend:
549 1.1 rmind off = hashval_getslot(query, key, len);
550 1.5.6.1 martin /* Consume from prior release in thmap_put(). */
551 1.5.6.1 martin node = atomic_load_consume(&parent->slots[off]);
552 1.1 rmind
553 1.1 rmind /* Descend the tree until we find a leaf or empty slot. */
554 1.1 rmind if (node && THMAP_INODE_P(node)) {
555 1.1 rmind parent = THMAP_NODE(thmap, node);
556 1.1 rmind query->level++;
557 1.1 rmind goto descend;
558 1.1 rmind }
559 1.5.6.1 martin /*
560 1.5.6.1 martin * NODE_DELETED does not become stale until GC runs, which
561 1.5.6.1 martin * cannot happen while we are in the middle of an operation,
562 1.5.6.1 martin * hence relaxed ordering.
563 1.5.6.1 martin */
564 1.5.6.1 martin if (atomic_load_relaxed(&parent->state) & NODE_DELETED) {
565 1.1 rmind return NULL;
566 1.1 rmind }
567 1.1 rmind *slot = off;
568 1.1 rmind return parent;
569 1.1 rmind }
570 1.1 rmind
571 1.1 rmind /*
572 1.1 rmind * find_edge_node_locked: traverse the tree, like find_edge_node(),
573 1.1 rmind * but attempt to lock the edge node.
574 1.1 rmind *
575 1.1 rmind * => Returns NULL if the deleted node is found. This indicates that
576 1.1 rmind * the caller must re-try from the root, as the root slot might have
577 1.1 rmind * changed too.
578 1.1 rmind */
579 1.1 rmind static thmap_inode_t *
580 1.1 rmind find_edge_node_locked(const thmap_t *thmap, thmap_query_t *query,
581 1.1 rmind const void * restrict key, size_t len, unsigned *slot)
582 1.1 rmind {
583 1.1 rmind thmap_inode_t *node;
584 1.1 rmind thmap_ptr_t target;
585 1.1 rmind retry:
586 1.1 rmind /*
587 1.1 rmind * Find the edge node and lock it! Re-check the state since
588 1.1 rmind * the tree might change by the time we acquire the lock.
589 1.1 rmind */
590 1.1 rmind node = find_edge_node(thmap, query, key, len, slot);
591 1.1 rmind if (!node) {
592 1.1 rmind /* The root slot is empty -- let the caller decide. */
593 1.1 rmind query->level = 0;
594 1.1 rmind return NULL;
595 1.1 rmind }
596 1.1 rmind lock_node(node);
597 1.5.6.1 martin if (__predict_false(atomic_load_relaxed(&node->state) & NODE_DELETED)) {
598 1.1 rmind /*
599 1.1 rmind * The node has been deleted. The tree might have a new
600 1.1 rmind * shape now, therefore we must re-start from the root.
601 1.1 rmind */
602 1.1 rmind unlock_node(node);
603 1.1 rmind query->level = 0;
604 1.1 rmind return NULL;
605 1.1 rmind }
606 1.5.6.1 martin target = atomic_load_relaxed(&node->slots[*slot]);
607 1.1 rmind if (__predict_false(target && THMAP_INODE_P(target))) {
608 1.1 rmind /*
609 1.1 rmind * The target slot has been changed and it is now an
610 1.1 rmind * intermediate node. Re-start from the top internode.
611 1.1 rmind */
612 1.1 rmind unlock_node(node);
613 1.1 rmind query->level = 0;
614 1.1 rmind goto retry;
615 1.1 rmind }
616 1.1 rmind return node;
617 1.1 rmind }
618 1.1 rmind
619 1.1 rmind /*
620 1.1 rmind * thmap_get: lookup a value given the key.
621 1.1 rmind */
622 1.1 rmind void *
623 1.1 rmind thmap_get(thmap_t *thmap, const void *key, size_t len)
624 1.1 rmind {
625 1.1 rmind thmap_query_t query;
626 1.1 rmind thmap_inode_t *parent;
627 1.1 rmind thmap_leaf_t *leaf;
628 1.1 rmind unsigned slot;
629 1.1 rmind
630 1.1 rmind hashval_init(&query, key, len);
631 1.1 rmind parent = find_edge_node(thmap, &query, key, len, &slot);
632 1.1 rmind if (!parent) {
633 1.1 rmind return NULL;
634 1.1 rmind }
635 1.1 rmind leaf = get_leaf(thmap, parent, slot);
636 1.1 rmind if (!leaf) {
637 1.1 rmind return NULL;
638 1.1 rmind }
639 1.1 rmind if (!key_cmp_p(thmap, leaf, key, len)) {
640 1.1 rmind return NULL;
641 1.1 rmind }
642 1.1 rmind return leaf->val;
643 1.1 rmind }
644 1.1 rmind
645 1.1 rmind /*
646 1.1 rmind * thmap_put: insert a value given the key.
647 1.1 rmind *
648 1.1 rmind * => If the key is already present, return the associated value.
649 1.1 rmind * => Otherwise, on successful insert, return the given value.
650 1.1 rmind */
651 1.1 rmind void *
652 1.1 rmind thmap_put(thmap_t *thmap, const void *key, size_t len, void *val)
653 1.1 rmind {
654 1.1 rmind thmap_query_t query;
655 1.1 rmind thmap_leaf_t *leaf, *other;
656 1.1 rmind thmap_inode_t *parent, *child;
657 1.1 rmind unsigned slot, other_slot;
658 1.1 rmind thmap_ptr_t target;
659 1.1 rmind
660 1.1 rmind /*
661 1.5.6.1 martin * First, pre-allocate and initialize the leaf node.
662 1.1 rmind */
663 1.1 rmind leaf = leaf_create(thmap, key, len, val);
664 1.1 rmind if (__predict_false(!leaf)) {
665 1.1 rmind return NULL;
666 1.1 rmind }
667 1.1 rmind hashval_init(&query, key, len);
668 1.1 rmind retry:
669 1.1 rmind /*
670 1.1 rmind * Try to insert into the root first, if its slot is empty.
671 1.1 rmind */
672 1.1 rmind if (root_try_put(thmap, &query, leaf)) {
673 1.1 rmind /* Success: the leaf was inserted; no locking involved. */
674 1.1 rmind return val;
675 1.1 rmind }
676 1.1 rmind
677 1.1 rmind /*
678 1.5.6.1 martin * Release node via store in node_insert (*) to subsequent
679 1.5.6.1 martin * consume in get_leaf() or find_edge_node().
680 1.5.6.1 martin */
681 1.5.6.1 martin atomic_thread_fence(memory_order_release);
682 1.5.6.1 martin
683 1.5.6.1 martin /*
684 1.1 rmind * Find the edge node and the target slot.
685 1.1 rmind */
686 1.1 rmind parent = find_edge_node_locked(thmap, &query, key, len, &slot);
687 1.1 rmind if (!parent) {
688 1.1 rmind goto retry;
689 1.1 rmind }
690 1.5.6.1 martin target = atomic_load_relaxed(&parent->slots[slot]); // tagged offset
691 1.1 rmind if (THMAP_INODE_P(target)) {
692 1.1 rmind /*
693 1.5.6.1 martin * Empty slot: simply insert the new leaf. The release
694 1.1 rmind * fence is already issued for us.
695 1.1 rmind */
696 1.1 rmind target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
697 1.5.6.1 martin node_insert(parent, slot, target); /* (*) */
698 1.1 rmind goto out;
699 1.1 rmind }
700 1.1 rmind
701 1.1 rmind /*
702 1.1 rmind * Collision or duplicate.
703 1.1 rmind */
704 1.1 rmind other = THMAP_NODE(thmap, target);
705 1.1 rmind if (key_cmp_p(thmap, other, key, len)) {
706 1.1 rmind /*
707 1.1 rmind * Duplicate. Free the pre-allocated leaf and
708 1.1 rmind * return the present value.
709 1.1 rmind */
710 1.1 rmind leaf_free(thmap, leaf);
711 1.1 rmind val = other->val;
712 1.1 rmind goto out;
713 1.1 rmind }
714 1.1 rmind descend:
715 1.1 rmind /*
716 1.1 rmind * Collision -- expand the tree. Create an intermediate node
717 1.1 rmind * which will be locked (NODE_LOCKED) for us. At this point,
718 1.1 rmind * we advance to the next level.
719 1.1 rmind */
720 1.1 rmind child = node_create(thmap, parent);
721 1.1 rmind if (__predict_false(!child)) {
722 1.1 rmind leaf_free(thmap, leaf);
723 1.1 rmind val = NULL;
724 1.1 rmind goto out;
725 1.1 rmind }
726 1.1 rmind query.level++;
727 1.1 rmind
728 1.1 rmind /*
729 1.5.6.1 martin * Insert the other (colliding) leaf first. The new child is
730 1.5.6.1 martin * not yet published, so memory order is relaxed.
731 1.1 rmind */
732 1.1 rmind other_slot = hashval_getleafslot(thmap, other, query.level);
733 1.1 rmind target = THMAP_GETOFF(thmap, other) | THMAP_LEAF_BIT;
734 1.1 rmind node_insert(child, other_slot, target);
735 1.1 rmind
736 1.1 rmind /*
737 1.1 rmind * Insert the intermediate node into the parent node.
738 1.1 rmind * It becomes the new parent for the our new leaf.
739 1.1 rmind *
740 1.5.6.1 martin * Ensure that stores to the child (and leaf) reach global
741 1.5.6.1 martin * visibility before it gets inserted to the parent, as
742 1.5.6.1 martin * consumed by get_leaf() or find_edge_node().
743 1.1 rmind */
744 1.5.6.1 martin atomic_store_release(&parent->slots[slot], THMAP_GETOFF(thmap, child));
745 1.1 rmind
746 1.1 rmind unlock_node(parent);
747 1.1 rmind ASSERT(node_locked_p(child));
748 1.1 rmind parent = child;
749 1.1 rmind
750 1.1 rmind /*
751 1.1 rmind * Get the new slot and check for another collision
752 1.1 rmind * at the next level.
753 1.1 rmind */
754 1.1 rmind slot = hashval_getslot(&query, key, len);
755 1.1 rmind if (slot == other_slot) {
756 1.1 rmind /* Another collision -- descend and expand again. */
757 1.1 rmind goto descend;
758 1.1 rmind }
759 1.1 rmind
760 1.5.6.1 martin /*
761 1.5.6.1 martin * Insert our new leaf once we expanded enough. The release
762 1.5.6.1 martin * fence is already issued for us.
763 1.5.6.1 martin */
764 1.1 rmind target = THMAP_GETOFF(thmap, leaf) | THMAP_LEAF_BIT;
765 1.5.6.1 martin node_insert(parent, slot, target); /* (*) */
766 1.1 rmind out:
767 1.1 rmind unlock_node(parent);
768 1.1 rmind return val;
769 1.1 rmind }
770 1.1 rmind
771 1.1 rmind /*
772 1.1 rmind * thmap_del: remove the entry given the key.
773 1.1 rmind */
774 1.1 rmind void *
775 1.1 rmind thmap_del(thmap_t *thmap, const void *key, size_t len)
776 1.1 rmind {
777 1.1 rmind thmap_query_t query;
778 1.1 rmind thmap_leaf_t *leaf;
779 1.1 rmind thmap_inode_t *parent;
780 1.1 rmind unsigned slot;
781 1.1 rmind void *val;
782 1.1 rmind
783 1.1 rmind hashval_init(&query, key, len);
784 1.1 rmind parent = find_edge_node_locked(thmap, &query, key, len, &slot);
785 1.1 rmind if (!parent) {
786 1.1 rmind /* Root slot empty: not found. */
787 1.1 rmind return NULL;
788 1.1 rmind }
789 1.1 rmind leaf = get_leaf(thmap, parent, slot);
790 1.1 rmind if (!leaf || !key_cmp_p(thmap, leaf, key, len)) {
791 1.1 rmind /* Not found. */
792 1.1 rmind unlock_node(parent);
793 1.1 rmind return NULL;
794 1.1 rmind }
795 1.1 rmind
796 1.1 rmind /* Remove the leaf. */
797 1.5.6.1 martin ASSERT(THMAP_NODE(thmap, atomic_load_relaxed(&parent->slots[slot]))
798 1.5.6.1 martin == leaf);
799 1.1 rmind node_remove(parent, slot);
800 1.1 rmind
801 1.1 rmind /*
802 1.1 rmind * Collapse the levels if removing the last item.
803 1.1 rmind */
804 1.5.6.1 martin while (query.level &&
805 1.5.6.1 martin NODE_COUNT(atomic_load_relaxed(&parent->state)) == 0) {
806 1.1 rmind thmap_inode_t *node = parent;
807 1.1 rmind
808 1.5.6.1 martin ASSERT(atomic_load_relaxed(&node->state) == NODE_LOCKED);
809 1.1 rmind
810 1.1 rmind /*
811 1.1 rmind * Ascend one level up.
812 1.1 rmind * => Mark our current parent as deleted.
813 1.1 rmind * => Lock the parent one level up.
814 1.1 rmind */
815 1.1 rmind query.level--;
816 1.1 rmind slot = hashval_getslot(&query, key, len);
817 1.1 rmind parent = THMAP_NODE(thmap, node->parent);
818 1.1 rmind ASSERT(parent != NULL);
819 1.1 rmind
820 1.1 rmind lock_node(parent);
821 1.5.6.1 martin ASSERT((atomic_load_relaxed(&parent->state) & NODE_DELETED)
822 1.5.6.1 martin == 0);
823 1.1 rmind
824 1.5.6.1 martin /*
825 1.5.6.1 martin * Lock is exclusive, so nobody else can be writing at
826 1.5.6.1 martin * the same time, and no need for atomic R/M/W, but
827 1.5.6.1 martin * readers may read without the lock and so need atomic
828 1.5.6.1 martin * load/store. No ordering here needed because the
829 1.5.6.1 martin * entry itself stays valid until GC.
830 1.5.6.1 martin */
831 1.5.6.1 martin atomic_store_relaxed(&node->state,
832 1.5.6.1 martin atomic_load_relaxed(&node->state) | NODE_DELETED);
833 1.5.6.1 martin unlock_node(node); // memory_order_release
834 1.1 rmind
835 1.5.6.1 martin ASSERT(THMAP_NODE(thmap,
836 1.5.6.1 martin atomic_load_relaxed(&parent->slots[slot])) == node);
837 1.1 rmind node_remove(parent, slot);
838 1.1 rmind
839 1.1 rmind /* Stage the removed node for G/C. */
840 1.1 rmind stage_mem_gc(thmap, THMAP_GETOFF(thmap, node), THMAP_INODE_LEN);
841 1.1 rmind }
842 1.1 rmind
843 1.1 rmind /*
844 1.1 rmind * If the top node is empty, then we need to remove it from the
845 1.1 rmind * root level. Mark the node as deleted and clear the slot.
846 1.1 rmind *
847 1.1 rmind * Note: acquiring the lock on the top node effectively prevents
848 1.1 rmind * the root slot from changing.
849 1.1 rmind */
850 1.5.6.1 martin if (NODE_COUNT(atomic_load_relaxed(&parent->state)) == 0) {
851 1.1 rmind const unsigned rslot = query.rslot;
852 1.5.6.1 martin const thmap_ptr_t nptr =
853 1.5.6.1 martin atomic_load_relaxed(&thmap->root[rslot]);
854 1.1 rmind
855 1.1 rmind ASSERT(query.level == 0);
856 1.1 rmind ASSERT(parent->parent == THMAP_NULL);
857 1.1 rmind ASSERT(THMAP_GETOFF(thmap, parent) == nptr);
858 1.1 rmind
859 1.1 rmind /* Mark as deleted and remove from the root-level slot. */
860 1.5.6.1 martin atomic_store_relaxed(&parent->state,
861 1.5.6.1 martin atomic_load_relaxed(&parent->state) | NODE_DELETED);
862 1.5.6.1 martin atomic_store_relaxed(&thmap->root[rslot], THMAP_NULL);
863 1.1 rmind
864 1.1 rmind stage_mem_gc(thmap, nptr, THMAP_INODE_LEN);
865 1.1 rmind }
866 1.1 rmind unlock_node(parent);
867 1.1 rmind
868 1.1 rmind /*
869 1.1 rmind * Save the value and stage the leaf for G/C.
870 1.1 rmind */
871 1.1 rmind val = leaf->val;
872 1.1 rmind if ((thmap->flags & THMAP_NOCOPY) == 0) {
873 1.1 rmind stage_mem_gc(thmap, leaf->key, leaf->len);
874 1.1 rmind }
875 1.1 rmind stage_mem_gc(thmap, THMAP_GETOFF(thmap, leaf), sizeof(thmap_leaf_t));
876 1.1 rmind return val;
877 1.1 rmind }
878 1.1 rmind
879 1.1 rmind /*
880 1.1 rmind * G/C routines.
881 1.1 rmind */
882 1.1 rmind
883 1.1 rmind static void
884 1.1 rmind stage_mem_gc(thmap_t *thmap, uintptr_t addr, size_t len)
885 1.1 rmind {
886 1.1 rmind thmap_gc_t *head, *gc;
887 1.1 rmind
888 1.4 rmind gc = kmem_intr_alloc(sizeof(thmap_gc_t), KM_NOSLEEP);
889 1.1 rmind gc->addr = addr;
890 1.1 rmind gc->len = len;
891 1.1 rmind retry:
892 1.5.6.1 martin head = atomic_load_relaxed(&thmap->gc_list);
893 1.5.6.1 martin gc->next = head; // not yet published
894 1.5.6.1 martin
895 1.5.6.1 martin /* Release to subsequent acquire in thmap_stage_gc(). */
896 1.5.6.1 martin if (!atomic_compare_exchange_weak_explicit_ptr(&thmap->gc_list, &head, gc,
897 1.5.6.1 martin memory_order_release, memory_order_relaxed)) {
898 1.1 rmind goto retry;
899 1.1 rmind }
900 1.1 rmind }
901 1.1 rmind
902 1.1 rmind void *
903 1.1 rmind thmap_stage_gc(thmap_t *thmap)
904 1.1 rmind {
905 1.5.6.1 martin /* Acquire from prior release in stage_mem_gc(). */
906 1.5.6.1 martin return atomic_exchange_explicit(&thmap->gc_list, NULL,
907 1.5.6.1 martin memory_order_acquire);
908 1.1 rmind }
909 1.1 rmind
910 1.1 rmind void
911 1.1 rmind thmap_gc(thmap_t *thmap, void *ref)
912 1.1 rmind {
913 1.1 rmind thmap_gc_t *gc = ref;
914 1.1 rmind
915 1.1 rmind while (gc) {
916 1.1 rmind thmap_gc_t *next = gc->next;
917 1.1 rmind thmap->ops->free(gc->addr, gc->len);
918 1.1 rmind kmem_intr_free(gc, sizeof(thmap_gc_t));
919 1.1 rmind gc = next;
920 1.1 rmind }
921 1.1 rmind }
922 1.1 rmind
923 1.1 rmind /*
924 1.1 rmind * thmap_create: construct a new trie-hash map object.
925 1.1 rmind */
926 1.1 rmind thmap_t *
927 1.1 rmind thmap_create(uintptr_t baseptr, const thmap_ops_t *ops, unsigned flags)
928 1.1 rmind {
929 1.1 rmind thmap_t *thmap;
930 1.1 rmind uintptr_t root;
931 1.1 rmind
932 1.1 rmind /*
933 1.1 rmind * Setup the map object.
934 1.1 rmind */
935 1.1 rmind if (!THMAP_ALIGNED_P(baseptr)) {
936 1.1 rmind return NULL;
937 1.1 rmind }
938 1.1 rmind thmap = kmem_zalloc(sizeof(thmap_t), KM_SLEEP);
939 1.1 rmind if (!thmap) {
940 1.1 rmind return NULL;
941 1.1 rmind }
942 1.1 rmind thmap->baseptr = baseptr;
943 1.1 rmind thmap->ops = ops ? ops : &thmap_default_ops;
944 1.1 rmind thmap->flags = flags;
945 1.1 rmind
946 1.1 rmind if ((thmap->flags & THMAP_SETROOT) == 0) {
947 1.1 rmind /* Allocate the root level. */
948 1.1 rmind root = thmap->ops->alloc(THMAP_ROOT_LEN);
949 1.1 rmind thmap->root = THMAP_GETPTR(thmap, root);
950 1.1 rmind if (!thmap->root) {
951 1.1 rmind kmem_free(thmap, sizeof(thmap_t));
952 1.1 rmind return NULL;
953 1.1 rmind }
954 1.1 rmind memset(thmap->root, 0, THMAP_ROOT_LEN);
955 1.5.6.1 martin atomic_thread_fence(memory_order_release); /* XXX */
956 1.1 rmind }
957 1.1 rmind return thmap;
958 1.1 rmind }
959 1.1 rmind
960 1.1 rmind int
961 1.1 rmind thmap_setroot(thmap_t *thmap, uintptr_t root_off)
962 1.1 rmind {
963 1.1 rmind if (thmap->root) {
964 1.1 rmind return -1;
965 1.1 rmind }
966 1.1 rmind thmap->root = THMAP_GETPTR(thmap, root_off);
967 1.5.6.1 martin atomic_thread_fence(memory_order_release); /* XXX */
968 1.1 rmind return 0;
969 1.1 rmind }
970 1.1 rmind
971 1.1 rmind uintptr_t
972 1.1 rmind thmap_getroot(const thmap_t *thmap)
973 1.1 rmind {
974 1.1 rmind return THMAP_GETOFF(thmap, thmap->root);
975 1.1 rmind }
976 1.1 rmind
977 1.1 rmind void
978 1.1 rmind thmap_destroy(thmap_t *thmap)
979 1.1 rmind {
980 1.1 rmind uintptr_t root = THMAP_GETOFF(thmap, thmap->root);
981 1.1 rmind void *ref;
982 1.1 rmind
983 1.1 rmind ref = thmap_stage_gc(thmap);
984 1.1 rmind thmap_gc(thmap, ref);
985 1.1 rmind
986 1.1 rmind if ((thmap->flags & THMAP_SETROOT) == 0) {
987 1.1 rmind thmap->ops->free(root, THMAP_ROOT_LEN);
988 1.1 rmind }
989 1.1 rmind kmem_free(thmap, sizeof(thmap_t));
990 1.1 rmind }
991