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 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28*0Sstevel@tonic-gate /* All Rights Reserved */ 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate 31*0Sstevel@tonic-gate #ifndef _SYS_BITMAP_H 32*0Sstevel@tonic-gate #define _SYS_BITMAP_H 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" /* SVr4.0 1.6 */ 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate #ifdef __cplusplus 37*0Sstevel@tonic-gate extern "C" { 38*0Sstevel@tonic-gate #endif 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate #include <sys/feature_tests.h> 41*0Sstevel@tonic-gate #if defined(__GNUC__) && defined(_ASM_INLINES) 42*0Sstevel@tonic-gate #include <asm/bitmap.h> 43*0Sstevel@tonic-gate #endif 44*0Sstevel@tonic-gate 45*0Sstevel@tonic-gate /* 46*0Sstevel@tonic-gate * Operations on bitmaps of arbitrary size 47*0Sstevel@tonic-gate * A bitmap is a vector of 1 or more ulong_t's. 48*0Sstevel@tonic-gate * The user of the package is responsible for range checks and keeping 49*0Sstevel@tonic-gate * track of sizes. 50*0Sstevel@tonic-gate */ 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate #ifdef _LP64 53*0Sstevel@tonic-gate #define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */ 54*0Sstevel@tonic-gate #define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */ 55*0Sstevel@tonic-gate #else 56*0Sstevel@tonic-gate #define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */ 57*0Sstevel@tonic-gate #endif 58*0Sstevel@tonic-gate 59*0Sstevel@tonic-gate #define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */ 60*0Sstevel@tonic-gate #define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */ 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gate #ifdef _LP64 63*0Sstevel@tonic-gate #define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */ 64*0Sstevel@tonic-gate #define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */ 65*0Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */ 66*0Sstevel@tonic-gate #else 67*0Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffff 68*0Sstevel@tonic-gate #endif 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate /* 71*0Sstevel@tonic-gate * bitmap is a ulong_t *, bitindex an index_t 72*0Sstevel@tonic-gate * 73*0Sstevel@tonic-gate * The macros BT_WIM and BT_BIW internal; there is no need 74*0Sstevel@tonic-gate * for users of this package to use them. 75*0Sstevel@tonic-gate */ 76*0Sstevel@tonic-gate 77*0Sstevel@tonic-gate /* 78*0Sstevel@tonic-gate * word in map 79*0Sstevel@tonic-gate */ 80*0Sstevel@tonic-gate #define BT_WIM(bitmap, bitindex) \ 81*0Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT]) 82*0Sstevel@tonic-gate /* 83*0Sstevel@tonic-gate * bit in word 84*0Sstevel@tonic-gate */ 85*0Sstevel@tonic-gate #define BT_BIW(bitindex) \ 86*0Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK)) 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate #ifdef _LP64 89*0Sstevel@tonic-gate #define BT_WIM32(bitmap, bitindex) \ 90*0Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT32]) 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gate #define BT_BIW32(bitindex) \ 93*0Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK32)) 94*0Sstevel@tonic-gate #endif 95*0Sstevel@tonic-gate 96*0Sstevel@tonic-gate /* 97*0Sstevel@tonic-gate * These are public macros 98*0Sstevel@tonic-gate * 99*0Sstevel@tonic-gate * BT_BITOUL == n bits to n ulong_t's 100*0Sstevel@tonic-gate */ 101*0Sstevel@tonic-gate #define BT_BITOUL(nbits) \ 102*0Sstevel@tonic-gate (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL) 103*0Sstevel@tonic-gate #define BT_SIZEOFMAP(nbits) \ 104*0Sstevel@tonic-gate (BT_BITOUL(nbits) * sizeof (ulong_t)) 105*0Sstevel@tonic-gate #define BT_TEST(bitmap, bitindex) \ 106*0Sstevel@tonic-gate ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0) 107*0Sstevel@tonic-gate #define BT_SET(bitmap, bitindex) \ 108*0Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); } 109*0Sstevel@tonic-gate #define BT_CLEAR(bitmap, bitindex) \ 110*0Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); } 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate #ifdef _LP64 113*0Sstevel@tonic-gate #define BT_BITOUL32(nbits) \ 114*0Sstevel@tonic-gate (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32) 115*0Sstevel@tonic-gate #define BT_SIZEOFMAP32(nbits) \ 116*0Sstevel@tonic-gate (BT_BITOUL32(nbits) * sizeof (uint_t)) 117*0Sstevel@tonic-gate #define BT_TEST32(bitmap, bitindex) \ 118*0Sstevel@tonic-gate ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0) 119*0Sstevel@tonic-gate #define BT_SET32(bitmap, bitindex) \ 120*0Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); } 121*0Sstevel@tonic-gate #define BT_CLEAR32(bitmap, bitindex) \ 122*0Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); } 123*0Sstevel@tonic-gate #endif /* _LP64 */ 124*0Sstevel@tonic-gate 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_ASM) 127*0Sstevel@tonic-gate #include <sys/atomic.h> 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate /* 130*0Sstevel@tonic-gate * return next available bit index from map with specified number of bits 131*0Sstevel@tonic-gate */ 132*0Sstevel@tonic-gate extern index_t bt_availbit(ulong_t *bitmap, size_t nbits); 133*0Sstevel@tonic-gate /* 134*0Sstevel@tonic-gate * find the highest order bit that is on, and is within or below 135*0Sstevel@tonic-gate * the word specified by wx 136*0Sstevel@tonic-gate */ 137*0Sstevel@tonic-gate extern int bt_gethighbit(ulong_t *mapp, int wx); 138*0Sstevel@tonic-gate extern int bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2, 139*0Sstevel@tonic-gate size_t end_pos); 140*0Sstevel@tonic-gate /* 141*0Sstevel@tonic-gate * Find highest and lowest one bit set. 142*0Sstevel@tonic-gate * Returns bit number + 1 of bit that is set, otherwise returns 0. 143*0Sstevel@tonic-gate * Low order bit is 0, high order bit is 31. 144*0Sstevel@tonic-gate */ 145*0Sstevel@tonic-gate extern int highbit(ulong_t); 146*0Sstevel@tonic-gate extern int lowbit(ulong_t); 147*0Sstevel@tonic-gate extern int bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop); 148*0Sstevel@tonic-gate extern void bt_copy(ulong_t *, ulong_t *, ulong_t); 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gate /* 151*0Sstevel@tonic-gate * find the parity 152*0Sstevel@tonic-gate */ 153*0Sstevel@tonic-gate extern int odd_parity(ulong_t); 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gate /* 156*0Sstevel@tonic-gate * Atomically set/clear bits 157*0Sstevel@tonic-gate * Atomic exclusive operations will set "result" to "-1" 158*0Sstevel@tonic-gate * if the bit is already set/cleared. "result" will be set 159*0Sstevel@tonic-gate * to 0 otherwise. 160*0Sstevel@tonic-gate */ 161*0Sstevel@tonic-gate #define BT_ATOMIC_SET(bitmap, bitindex) \ 162*0Sstevel@tonic-gate { atomic_or_long(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); } 163*0Sstevel@tonic-gate #define BT_ATOMIC_CLEAR(bitmap, bitindex) \ 164*0Sstevel@tonic-gate { atomic_and_long(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); } 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate #define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \ 167*0Sstevel@tonic-gate { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \ 168*0Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 169*0Sstevel@tonic-gate #define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \ 170*0Sstevel@tonic-gate { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \ 171*0Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gate /* 174*0Sstevel@tonic-gate * Extracts bits between index h (high, inclusive) and l (low, exclusive) from 175*0Sstevel@tonic-gate * u, which must be an unsigned integer. 176*0Sstevel@tonic-gate */ 177*0Sstevel@tonic-gate #define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU)) 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate #endif /* _KERNEL && !_ASM */ 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gate #ifdef __cplusplus 182*0Sstevel@tonic-gate } 183*0Sstevel@tonic-gate #endif 184*0Sstevel@tonic-gate 185*0Sstevel@tonic-gate #endif /* _SYS_BITMAP_H */ 186