1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 28 /* All Rights Reserved */ 29 30 31 #ifndef _SYS_BITMAP_H 32 #define _SYS_BITMAP_H 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 39 (defined(__i386) || defined(__amd64)) 40 #include <asm/bitmap.h> 41 #endif 42 43 /* 44 * Operations on bitmaps of arbitrary size 45 * A bitmap is a vector of 1 or more ulong_t's. 46 * The user of the package is responsible for range checks and keeping 47 * track of sizes. 48 */ 49 50 #ifdef _LP64 51 #define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */ 52 #define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */ 53 #else 54 #define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */ 55 #endif 56 57 #define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */ 58 #define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */ 59 60 #ifdef _LP64 61 #define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */ 62 #define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */ 63 #define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */ 64 #else 65 #define BT_ULMAXMASK 0xffffffff 66 #endif 67 68 /* 69 * bitmap is a ulong_t *, bitindex an index_t 70 * 71 * The macros BT_WIM and BT_BIW internal; there is no need 72 * for users of this package to use them. 73 */ 74 75 /* 76 * word in map 77 */ 78 #define BT_WIM(bitmap, bitindex) \ 79 ((bitmap)[(bitindex) >> BT_ULSHIFT]) 80 /* 81 * bit in word 82 */ 83 #define BT_BIW(bitindex) \ 84 (1UL << ((bitindex) & BT_ULMASK)) 85 86 #ifdef _LP64 87 #define BT_WIM32(bitmap, bitindex) \ 88 ((bitmap)[(bitindex) >> BT_ULSHIFT32]) 89 90 #define BT_BIW32(bitindex) \ 91 (1UL << ((bitindex) & BT_ULMASK32)) 92 #endif 93 94 /* 95 * These are public macros 96 * 97 * BT_BITOUL == n bits to n ulong_t's 98 */ 99 #define BT_BITOUL(nbits) \ 100 (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL) 101 #define BT_SIZEOFMAP(nbits) \ 102 (BT_BITOUL(nbits) * sizeof (ulong_t)) 103 #define BT_TEST(bitmap, bitindex) \ 104 ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0) 105 #define BT_SET(bitmap, bitindex) \ 106 { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); } 107 #define BT_CLEAR(bitmap, bitindex) \ 108 { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); } 109 110 #ifdef _LP64 111 #define BT_BITOUL32(nbits) \ 112 (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32) 113 #define BT_SIZEOFMAP32(nbits) \ 114 (BT_BITOUL32(nbits) * sizeof (uint_t)) 115 #define BT_TEST32(bitmap, bitindex) \ 116 ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0) 117 #define BT_SET32(bitmap, bitindex) \ 118 { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); } 119 #define BT_CLEAR32(bitmap, bitindex) \ 120 { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); } 121 #endif /* _LP64 */ 122 123 124 /* 125 * BIT_ONLYONESET is a private macro not designed for bitmaps of 126 * arbitrary size. u must be an unsigned integer/long. It returns 127 * true if one and only one bit is set in u. 128 */ 129 #define BIT_ONLYONESET(u) \ 130 ((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0)) 131 132 #ifndef _ASM 133 134 /* 135 * return next available bit index from map with specified number of bits 136 */ 137 extern index_t bt_availbit(ulong_t *bitmap, size_t nbits); 138 /* 139 * find the highest order bit that is on, and is within or below 140 * the word specified by wx 141 */ 142 extern int bt_gethighbit(ulong_t *mapp, int wx); 143 extern int bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2, 144 size_t end_pos); 145 extern int bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop); 146 extern void bt_copy(ulong_t *, ulong_t *, ulong_t); 147 148 /* 149 * find the parity 150 */ 151 extern int odd_parity(ulong_t); 152 153 /* 154 * Atomically set/clear bits 155 * Atomic exclusive operations will set "result" to "-1" 156 * if the bit is already set/cleared. "result" will be set 157 * to 0 otherwise. 158 */ 159 #define BT_ATOMIC_SET(bitmap, bitindex) \ 160 { atomic_or_ulong(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); } 161 #define BT_ATOMIC_CLEAR(bitmap, bitindex) \ 162 { atomic_and_ulong(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); } 163 164 #define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \ 165 { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \ 166 (bitindex) % BT_NBIPUL); } 167 #define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \ 168 { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \ 169 (bitindex) % BT_NBIPUL); } 170 171 /* 172 * Extracts bits between index h (high, inclusive) and l (low, exclusive) from 173 * u, which must be an unsigned integer. 174 */ 175 #define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU)) 176 177 #endif /* _ASM */ 178 179 #ifdef __cplusplus 180 } 181 #endif 182 183 #endif /* _SYS_BITMAP_H */ 184