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