1219820Sjeff/*-
2219820Sjeff * Copyright (c) 2010 Isilon Systems, Inc.
3219820Sjeff * Copyright (c) 2010 iX Systems, Inc.
4219820Sjeff * Copyright (c) 2010 Panasas, Inc.
5329973Shselasky * Copyright (c) 2013-2018 Mellanox Technologies, Ltd.
6219820Sjeff * All rights reserved.
7219820Sjeff *
8219820Sjeff * Redistribution and use in source and binary forms, with or without
9219820Sjeff * modification, are permitted provided that the following conditions
10219820Sjeff * are met:
11219820Sjeff * 1. Redistributions of source code must retain the above copyright
12219820Sjeff *    notice unmodified, this list of conditions, and the following
13219820Sjeff *    disclaimer.
14219820Sjeff * 2. Redistributions in binary form must reproduce the above copyright
15219820Sjeff *    notice, this list of conditions and the following disclaimer in the
16219820Sjeff *    documentation and/or other materials provided with the distribution.
17219820Sjeff *
18219820Sjeff * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19219820Sjeff * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20219820Sjeff * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21219820Sjeff * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22219820Sjeff * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23219820Sjeff * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24219820Sjeff * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25219820Sjeff * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26219820Sjeff * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27219820Sjeff * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28289644Shselasky *
29289644Shselasky * $FreeBSD: stable/11/sys/compat/linuxkpi/common/include/linux/radix-tree.h 364383 2020-08-19 12:26:23Z hselasky $
30219820Sjeff */
31219820Sjeff#ifndef	_LINUX_RADIX_TREE_H_
32219820Sjeff#define	_LINUX_RADIX_TREE_H_
33219820Sjeff
34290335Shselasky#include <linux/types.h>
35290335Shselasky
36219820Sjeff#define	RADIX_TREE_MAP_SHIFT	6
37329973Shselasky#define	RADIX_TREE_MAP_SIZE	(1UL << RADIX_TREE_MAP_SHIFT)
38329973Shselasky#define	RADIX_TREE_MAP_MASK	(RADIX_TREE_MAP_SIZE - 1UL)
39329973Shselasky#define	RADIX_TREE_MAX_HEIGHT \
40329973Shselasky	howmany(sizeof(long) * NBBY, RADIX_TREE_MAP_SHIFT)
41219820Sjeff
42329973Shselasky#define	RADIX_TREE_ENTRY_MASK 3UL
43329973Shselasky#define	RADIX_TREE_EXCEPTIONAL_ENTRY 2UL
44329973Shselasky#define	RADIX_TREE_EXCEPTIONAL_SHIFT 2
45329973Shselasky
46219820Sjeffstruct radix_tree_node {
47219820Sjeff	void		*slots[RADIX_TREE_MAP_SIZE];
48219820Sjeff	int		count;
49219820Sjeff};
50219820Sjeff
51219820Sjeffstruct radix_tree_root {
52219820Sjeff	struct radix_tree_node	*rnode;
53219820Sjeff	gfp_t			gfp_mask;
54219820Sjeff	int			height;
55219820Sjeff};
56219820Sjeff
57329973Shselaskystruct radix_tree_iter {
58329973Shselasky	unsigned long index;
59329973Shselasky};
60329973Shselasky
61219820Sjeff#define	RADIX_TREE_INIT(mask)						\
62219820Sjeff	    { .rnode = NULL, .gfp_mask = mask, .height = 0 };
63219820Sjeff#define	INIT_RADIX_TREE(root, mask)					\
64219820Sjeff	    { (root)->rnode = NULL; (root)->gfp_mask = mask; (root)->height = 0; }
65219820Sjeff#define	RADIX_TREE(name, mask)						\
66219820Sjeff	    struct radix_tree_root name = RADIX_TREE_INIT(mask)
67219820Sjeff
68329973Shselasky#define	radix_tree_for_each_slot(slot, root, iter, start) \
69329973Shselasky	for ((iter)->index = (start);			  \
70329973Shselasky	     radix_tree_iter_find(root, iter, &(slot)); (iter)->index++)
71329973Shselasky
72329973Shselaskystatic inline int
73329973Shselaskyradix_tree_exception(void *arg)
74329973Shselasky{
75329973Shselasky	return ((uintptr_t)arg & RADIX_TREE_ENTRY_MASK);
76329973Shselasky}
77329973Shselasky
78219820Sjeffvoid	*radix_tree_lookup(struct radix_tree_root *, unsigned long);
79219820Sjeffvoid	*radix_tree_delete(struct radix_tree_root *, unsigned long);
80219820Sjeffint	radix_tree_insert(struct radix_tree_root *, unsigned long, void *);
81364383Shselaskyint	radix_tree_store(struct radix_tree_root *, unsigned long, void **);
82329973Shselaskybool	radix_tree_iter_find(struct radix_tree_root *, struct radix_tree_iter *, void ***);
83335412Shselaskyvoid	radix_tree_iter_delete(struct radix_tree_root *, struct radix_tree_iter *, void **);
84219820Sjeff
85219820Sjeff#endif	/* _LINUX_RADIX_TREE_H_ */
86