acl  3.5.3.0
avl_impl.h
浏览该文件的文档.
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 ACL_AVL_IMPL_H
28 #define ACL_AVL_IMPL_H
29 
30 /*
31  * This is a private header file. Applications should not directly include
32  * this file.
33  */
34 #include "acl_define.h"
35 
36 #include <sys/types.h>
37 #include <stddef.h>
38 #ifdef ACL_UNIX
39 #if 1
40 # include <stdint.h> /* uintptr_t */
41 #endif
42 # define HAS_UINTPTR
43 #elif defined(_WIN32) || defined(_WIN64)
44 # include <stddef.h>
45 # define HAS_UINTPTR
46 #endif
47 
48 #ifndef HAS_UINTPTR
49 # define HAS_UINTPTR
50 # define _UINTPTR_T_DEFINED /* for win32 */
51 # ifdef UINTPTR_ULONG_INT
52 typedef unsigned long int uintptr_t;
53 # else
54 typedef unsigned int uintptr_t;
55 # endif
56 #endif
57 
58 #ifndef SUNOS5
59 typedef unsigned int ulong_t;
60 #endif
61 typedef char acl_boolean_t;
62 
63 #define B_FALSE 0
64 #define B_TRUE 1
65 
66 #ifdef __cplusplus
67 extern "C" {
68 #endif
69 
70 /*
71  * generic AVL tree implementation for kernel use
72  *
73  * There are 5 pieces of information stored for each node in an AVL tree
74  *
75  * pointer to less than child
76  * pointer to greater than child
77  * a pointer to the parent of this node
78  * an indication [0/1] of which child I am of my parent
79  * a "balance" (-1, 0, +1) indicating which child tree is taller
80  *
81  * Since they only need 3 bits, the last two fields are packed into the
82  * bottom bits of the parent pointer on 64 bit machines to save on space.
83  */
84 
85 #ifndef _LP64
86 
87 struct avl_node {
88  struct avl_node *avl_child[2]; /* left/right children */
89  struct avl_node *avl_parent; /* this node's parent */
90  unsigned short avl_child_index; /* my index in parent's avl_child[] */
91  short avl_balance; /* balance value: -1, 0, +1 */
92 };
93 
94 #define AVL_XPARENT(n) ((n)->avl_parent)
95 #define AVL_SETPARENT(n, p) ((n)->avl_parent = (p))
96 
97 #define AVL_XCHILD(n) ((n)->avl_child_index)
98 #define AVL_SETCHILD(n, c) ((n)->avl_child_index = (unsigned short)(c))
99 
100 #define AVL_XBALANCE(n) ((n)->avl_balance)
101 #define AVL_SETBALANCE(n, b) ((n)->avl_balance = (short)(b))
102 
103 #else /* _LP64 */
104 
105 /*
106  * for 64 bit machines, avl_pcb contains parent pointer, balance and child_index
107  * values packed in the following manner:
108  *
109  * |63 3| 2 |1 0 |
110  * |-------------------------------------|-----------------|-------------|
111  * | avl_parent hi order bits | avl_child_index | avl_balance |
112  * | | | + 1 |
113  * |-------------------------------------|-----------------|-------------|
114  *
115  */
116 
117 struct avl_node {
118  struct avl_node *avl_child[2]; /* left/right children nodes */
119  uintptr_t avl_pcb; /* parent, child_index, balance */
120 };
121 
122 /*
123  * macros to extract/set fields in avl_pcb
124  *
125  * pointer to the parent of the current node is the high order bits
126  */
127 #define AVL_XPARENT(n) ((struct avl_node *)((n)->avl_pcb & ~7))
128 #define AVL_SETPARENT(n, p) \
129  ((n)->avl_pcb = (((n)->avl_pcb & 7) | (uintptr_t)(p)))
130 
131 /*
132  * index of this node in its parent's avl_child[]: bit #2
133  */
134 #define AVL_XCHILD(n) (((n)->avl_pcb >> 2) & 1)
135 #define AVL_SETCHILD(n, c) \
136  ((n)->avl_pcb = (uintptr_t)(((n)->avl_pcb & ~4) | ((c) << 2)))
137 
138 /*
139  * balance indication for a node, lowest 2 bits. A valid balance is
140  * -1, 0, or +1, and is encoded by adding 1 to the value to get the
141  * unsigned values of 0, 1, 2.
142  */
143 #define AVL_XBALANCE(n) ((int)(((n)->avl_pcb & 3) - 1))
144 #define AVL_SETBALANCE(n, b) \
145  ((n)->avl_pcb = (uintptr_t)((((n)->avl_pcb & ~3) | ((b) + 1))))
146 
147 #endif /* _LP64 */
148 
149 
150 
151 /*
152  * switch between a node and data pointer for a given tree
153  * the value of "o" is tree->avl_offset
154  */
155 #define AVL_NODE2DATA(n, o) ((void *)((uintptr_t)(n) - (o)))
156 #define AVL_DATA2NODE(d, o) ((struct avl_node *)((uintptr_t)(d) + (o)))
157 
158 /*
159  * macros used to create/access an avl_index_t
160  */
161 #define AVL_INDEX2NODE(x) ((avl_node_t *)((x) & ~1))
162 #define AVL_INDEX2CHILD(x) ((x) & 1)
163 #define AVL_MKINDEX(n, c) ((avl_index_t)(n) | (c))
164 
165 
166 /*
167  * The tree structure. The fields avl_root, avl_compar, and avl_offset come
168  * first since they are needed for avl_find(). We want them to fit into
169  * a single 64 byte cache line to make avl_find() as fast as possible.
170  */
171 struct avl_tree {
172  struct avl_node *avl_root; /* root node in tree */
173  int (*avl_compar)(const void *, const void *);
174  size_t avl_offset; /* offsetof(type, avl_link_t field) */
175  ulong_t avl_numnodes; /* number of nodes in the tree */
176  size_t avl_size; /* sizeof user type struct */
177 };
178 
179 
180 /*
181  * This will only by used via AVL_NEXT() or AVL_PREV()
182  */
183 ACL_API void *avl_walk(struct avl_tree *, void *, int);
184 
185 #ifdef __cplusplus
186 }
187 #endif
188 
189 #endif /* _AVL_IMPL_H */
size_t avl_offset
Definition: avl_impl.h:174
ulong_t avl_numnodes
Definition: avl_impl.h:175
struct avl_node * avl_root
Definition: avl_impl.h:172
unsigned short avl_child_index
Definition: avl_impl.h:90
unsigned int ulong_t
Definition: avl_impl.h:59
short avl_balance
Definition: avl_impl.h:91
struct avl_node * avl_child[2]
Definition: avl_impl.h:88
size_t avl_size
Definition: avl_impl.h:176
char acl_boolean_t
Definition: avl_impl.h:61
ACL_API void * avl_walk(struct avl_tree *, void *, int)
int(* avl_compar)(const void *, const void *)
Definition: avl_impl.h:173
unsigned int uintptr_t
Definition: avl_impl.h:54
struct avl_node * avl_parent
Definition: avl_impl.h:89