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