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