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