bitmap.h revision 270998
1139825Simp/* 250565Sphk * CDDL HEADER START 350565Sphk * 450565Sphk * The contents of this file are subject to the terms of the 550565Sphk * Common Development and Distribution License (the "License"). 650565Sphk * You may not use this file except in compliance with the License. 750565Sphk * 850565Sphk * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 950565Sphk * or http://www.opensolaris.org/os/licensing. 1050565Sphk * See the License for the specific language governing permissions 1150565Sphk * and limitations under the License. 1250565Sphk * 1350565Sphk * When distributing Covered Code, include this CDDL HEADER in each 1450565Sphk * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 1550565Sphk * If applicable, add the following below this CDDL HEADER, with the 1694182Sphk * fields enclosed by brackets "[]" replaced with your own identifying 17274732Smav * information: Portions Copyright [yyyy] [name of copyright owner] 1894182Sphk * 1994182Sphk * CDDL HEADER END 2050565Sphk */ 21110119Sphk 22110119Sphk/* 23110119Sphk * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 24110119Sphk * Use is subject to license terms. 25103675Sphk */ 2650565Sphk 2794182Sphk/* 2894182Sphk * Copyright (c) 2014 by Delphix. All rights reserved. 29210365Strasz */ 30210365Strasz 31210365Strasz/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 32210365Strasz/* All Rights Reserved */ 33210365Strasz 3494282Sphk 3594282Sphk#ifndef _SYS_BITMAP_H 36210365Strasz#define _SYS_BITMAP_H 37210365Strasz 3894282Sphk#ifdef __cplusplus 39210365Straszextern "C" { 4094282Sphk#endif 4194282Sphk 42210365Strasz#include <sys/feature_tests.h> 43210365Strasz#if defined(__GNUC__) && defined(_ASM_INLINES) && \ 44210365Strasz (defined(__i386) || defined(__amd64)) 4594282Sphk#include <asm/bitmap.h> 4694282Sphk#endif 4794282Sphk 4894282Sphk/* 49210365Strasz * Operations on bitmaps of arbitrary size 50210365Strasz * A bitmap is a vector of 1 or more ulong_t's. 5194282Sphk * The user of the package is responsible for range checks and keeping 5294282Sphk * track of sizes. 5394282Sphk */ 5494282Sphk 5594282Sphk#ifdef _LP64 56210365Strasz#define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */ 57210365Strasz#define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */ 5894282Sphk#else 5994282Sphk#define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */ 6094282Sphk#endif 6194282Sphk 62210365Strasz#define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */ 63210365Strasz#define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */ 6494287Sphk 6594287Sphk#ifdef _LP64 6694287Sphk#define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */ 6794287Sphk#define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */ 6894287Sphk#define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */ 6994287Sphk#else 70169284Spjd#define BT_ULMAXMASK 0xffffffff 71210365Strasz#endif 72169284Spjd 73169284Spjd/* 74169284Spjd * bitmap is a ulong_t *, bitindex an index_t 75169284Spjd * 76210365Strasz * The macros BT_WIM and BT_BIW internal; there is no need 77169284Spjd * for users of this package to use them. 78169284Spjd */ 79169284Spjd 80169284Spjd/* 81169284Spjd * word in map 82210226Strasz */ 83169284Spjd#define BT_WIM(bitmap, bitindex) \ 84169284Spjd ((bitmap)[(bitindex) >> BT_ULSHIFT]) 85169284Spjd/* 86169284Spjd * bit in word 87169284Spjd */ 88169284Spjd#define BT_BIW(bitindex) \ 89169284Spjd (1UL << ((bitindex) & BT_ULMASK)) 90169284Spjd 91169284Spjd#ifdef _LP64 92169284Spjd#define BT_WIM32(bitmap, bitindex) \ 93169284Spjd ((bitmap)[(bitindex) >> BT_ULSHIFT32]) 94169284Spjd 95169284Spjd#define BT_BIW32(bitindex) \ 96169284Spjd (1UL << ((bitindex) & BT_ULMASK32)) 97169284Spjd#endif 98169284Spjd 99169284Spjd/* 100169284Spjd * These are public macros 101169284Spjd * 102210365Strasz * BT_BITOUL == n bits to n ulong_t's 103210365Strasz */ 104182843Slulf#define BT_BITOUL(nbits) \ 105182843Slulf (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL) 106182843Slulf#define BT_SIZEOFMAP(nbits) \ 107182843Slulf (BT_BITOUL(nbits) * sizeof (ulong_t)) 108210365Strasz#define BT_TEST(bitmap, bitindex) \ 109210365Strasz ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0) 110200934Smav#define BT_SET(bitmap, bitindex) \ 111210365Strasz { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); } 112200934Smav#define BT_CLEAR(bitmap, bitindex) \ 113200934Smav { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); } 114210365Strasz 115210365Strasz#ifdef _LP64 116200934Smav#define BT_BITOUL32(nbits) \ 117210365Strasz (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32) 118200934Smav#define BT_SIZEOFMAP32(nbits) \ 119200934Smav (BT_BITOUL32(nbits) * sizeof (uint_t)) 120223089Sgibbs#define BT_TEST32(bitmap, bitindex) \ 121223089Sgibbs ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0) 122223089Sgibbs#define BT_SET32(bitmap, bitindex) \ 123223089Sgibbs { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); } 124223089Sgibbs#define BT_CLEAR32(bitmap, bitindex) \ 125223089Sgibbs { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); } 126223089Sgibbs#endif /* _LP64 */ 127223089Sgibbs 128274732Smav 129274732Smav/* 130274732Smav * BIT_ONLYONESET is a private macro not designed for bitmaps of 131274732Smav * arbitrary size. u must be an unsigned integer/long. It returns 132274732Smav * true if one and only one bit is set in u. 133274732Smav */ 134279005Smav#define BIT_ONLYONESET(u) \ 135274732Smav ((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0)) 136274732Smav 137274732Smav#if defined(_KERNEL) && !defined(_ASM) 138274732Smav#include <sys/atomic.h> 13950565Sphk 140/* 141 * return next available bit index from map with specified number of bits 142 */ 143extern index_t bt_availbit(ulong_t *bitmap, size_t nbits); 144/* 145 * find the highest order bit that is on, and is within or below 146 * the word specified by wx 147 */ 148extern int bt_gethighbit(ulong_t *mapp, int wx); 149extern int bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2, 150 size_t end_pos); 151/* 152 * Find highest and lowest one bit set. 153 * Returns bit number + 1 of bit that is set, otherwise returns 0. 154 * Low order bit is 0, high order bit is 31. 155 */ 156extern int highbit(ulong_t); 157extern int highbit64(uint64_t); 158extern int lowbit(ulong_t); 159extern int bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop); 160extern void bt_copy(ulong_t *, ulong_t *, ulong_t); 161 162/* 163 * find the parity 164 */ 165extern int odd_parity(ulong_t); 166 167/* 168 * Atomically set/clear bits 169 * Atomic exclusive operations will set "result" to "-1" 170 * if the bit is already set/cleared. "result" will be set 171 * to 0 otherwise. 172 */ 173#define BT_ATOMIC_SET(bitmap, bitindex) \ 174 { atomic_or_ulong(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); } 175#define BT_ATOMIC_CLEAR(bitmap, bitindex) \ 176 { atomic_and_ulong(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); } 177 178#define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \ 179 { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \ 180 (bitindex) % BT_NBIPUL); } 181#define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \ 182 { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \ 183 (bitindex) % BT_NBIPUL); } 184 185/* 186 * Extracts bits between index h (high, inclusive) and l (low, exclusive) from 187 * u, which must be an unsigned integer. 188 */ 189#define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU)) 190 191#endif /* _KERNEL && !_ASM */ 192 193#ifdef __cplusplus 194} 195#endif 196 197#endif /* _SYS_BITMAP_H */ 198