xref: /titanic_52/usr/src/uts/common/sys/avl_impl.h (revision f6e214c7418f43af38bd8c3a557e3d0a1d311cfa)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_AVL_IMPL_H
28 #define	_AVL_IMPL_H
29 
30 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 /*
33  * This is a private header file.  Applications should not directly include
34  * this file.
35  */
36 
37 #include <sys/types.h>
38 
39 #ifdef	__cplusplus
40 extern "C" {
41 #endif
42 
43 
44 /*
45  * generic AVL tree implementation for kernel use
46  *
47  * There are 5 pieces of information stored for each node in an AVL tree
48  *
49  * 	pointer to less than child
50  * 	pointer to greater than child
51  * 	a pointer to the parent of this node
52  *	an indication  [0/1]  of which child I am of my parent
53  * 	a "balance" (-1, 0, +1)  indicating which child tree is taller
54  *
55  * Since they only need 3 bits, the last two fields are packed into the
56  * bottom bits of the parent pointer on 64 bit machines to save on space.
57  */
58 
59 #ifndef _LP64
60 
61 struct avl_node {
62 	struct avl_node *avl_child[2];	/* left/right children */
63 	struct avl_node *avl_parent;	/* this node's parent */
64 	unsigned short avl_child_index;	/* my index in parent's avl_child[] */
65 	short avl_balance;		/* balance value: -1, 0, +1 */
66 };
67 
68 #define	AVL_XPARENT(n)		((n)->avl_parent)
69 #define	AVL_SETPARENT(n, p)	((n)->avl_parent = (p))
70 
71 #define	AVL_XCHILD(n)		((n)->avl_child_index)
72 #define	AVL_SETCHILD(n, c)	((n)->avl_child_index = (unsigned short)(c))
73 
74 #define	AVL_XBALANCE(n)		((n)->avl_balance)
75 #define	AVL_SETBALANCE(n, b)	((n)->avl_balance = (short)(b))
76 
77 #else /* _LP64 */
78 
79 /*
80  * for 64 bit machines, avl_pcb contains parent pointer, balance and child_index
81  * values packed in the following manner:
82  *
83  * |63                                  3|        2        |1          0 |
84  * |-------------------------------------|-----------------|-------------|
85  * |      avl_parent hi order bits       | avl_child_index | avl_balance |
86  * |                                     |                 |     + 1     |
87  * |-------------------------------------|-----------------|-------------|
88  *
89  */
90 struct avl_node {
91 	struct avl_node *avl_child[2];	/* left/right children nodes */
92 	uintptr_t avl_pcb;		/* parent, child_index, balance */
93 };
94 
95 /*
96  * macros to extract/set fields in avl_pcb
97  *
98  * pointer to the parent of the current node is the high order bits
99  */
100 #define	AVL_XPARENT(n)		((struct avl_node *)((n)->avl_pcb & ~7))
101 #define	AVL_SETPARENT(n, p)						\
102 	((n)->avl_pcb = (((n)->avl_pcb & 7) | (uintptr_t)(p)))
103 
104 /*
105  * index of this node in its parent's avl_child[]: bit #2
106  */
107 #define	AVL_XCHILD(n)		(((n)->avl_pcb >> 2) & 1)
108 #define	AVL_SETCHILD(n, c)						\
109 	((n)->avl_pcb = (uintptr_t)(((n)->avl_pcb & ~4) | ((c) << 2)))
110 
111 /*
112  * balance indication for a node, lowest 2 bits. A valid balance is
113  * -1, 0, or +1, and is encoded by adding 1 to the value to get the
114  * unsigned values of 0, 1, 2.
115  */
116 #define	AVL_XBALANCE(n)		((int)(((n)->avl_pcb & 3) - 1))
117 #define	AVL_SETBALANCE(n, b)						\
118 	((n)->avl_pcb = (uintptr_t)((((n)->avl_pcb & ~3) | ((b) + 1))))
119 
120 #endif /* _LP64 */
121 
122 
123 
124 /*
125  * switch between a node and data pointer for a given tree
126  * the value of "o" is tree->avl_offset
127  */
128 #define	AVL_NODE2DATA(n, o)	((void *)((uintptr_t)(n) - (o)))
129 #define	AVL_DATA2NODE(d, o)	((struct avl_node *)((uintptr_t)(d) + (o)))
130 
131 
132 
133 /*
134  * macros used to create/access an avl_index_t
135  */
136 #define	AVL_INDEX2NODE(x)	((avl_node_t *)((x) & ~1))
137 #define	AVL_INDEX2CHILD(x)	((x) & 1)
138 #define	AVL_MKINDEX(n, c)	((avl_index_t)(n) | (c))
139 
140 
141 /*
142  * The tree structure. The fields avl_root, avl_compar, and avl_offset come
143  * first since they are needed for avl_find().  We want them to fit into
144  * a single 64 byte cache line to make avl_find() as fast as possible.
145  */
146 struct avl_tree {
147 	struct avl_node *avl_root;	/* root node in tree */
148 	int (*avl_compar)(const void *, const void *);
149 	size_t avl_offset;		/* offsetof(type, avl_link_t field) */
150 	ulong_t avl_numnodes;		/* number of nodes in the tree */
151 	size_t avl_size;		/* sizeof user type struct */
152 };
153 
154 
155 /*
156  * This will only by used via AVL_NEXT() or AVL_PREV()
157  */
158 extern void *avl_walk(struct avl_tree *, void *, int);
159 
160 #ifdef	__cplusplus
161 }
162 #endif
163 
164 #endif	/* _AVL_IMPL_H */
165