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