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