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