17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*2b616c6cSwesolows * Common Development and Distribution License (the "License"). 6*2b616c6cSwesolows * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 21*2b616c6cSwesolows 227c478bd9Sstevel@tonic-gate /* 23*2b616c6cSwesolows * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #ifndef _SYS_BITMAP_H 327c478bd9Sstevel@tonic-gate #define _SYS_BITMAP_H 337c478bd9Sstevel@tonic-gate 34*2b616c6cSwesolows #pragma ident "%Z%%M% %I% %E% SMI" 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #ifdef __cplusplus 377c478bd9Sstevel@tonic-gate extern "C" { 387c478bd9Sstevel@tonic-gate #endif 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h> 41*2b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 42*2b616c6cSwesolows (defined(__i386) || defined(__amd64)) 437c478bd9Sstevel@tonic-gate #include <asm/bitmap.h> 447c478bd9Sstevel@tonic-gate #endif 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * Operations on bitmaps of arbitrary size 487c478bd9Sstevel@tonic-gate * A bitmap is a vector of 1 or more ulong_t's. 497c478bd9Sstevel@tonic-gate * The user of the package is responsible for range checks and keeping 507c478bd9Sstevel@tonic-gate * track of sizes. 517c478bd9Sstevel@tonic-gate */ 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate #ifdef _LP64 547c478bd9Sstevel@tonic-gate #define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */ 557c478bd9Sstevel@tonic-gate #define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */ 567c478bd9Sstevel@tonic-gate #else 577c478bd9Sstevel@tonic-gate #define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */ 587c478bd9Sstevel@tonic-gate #endif 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */ 617c478bd9Sstevel@tonic-gate #define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate #ifdef _LP64 647c478bd9Sstevel@tonic-gate #define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */ 657c478bd9Sstevel@tonic-gate #define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */ 667c478bd9Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */ 677c478bd9Sstevel@tonic-gate #else 687c478bd9Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffff 697c478bd9Sstevel@tonic-gate #endif 707c478bd9Sstevel@tonic-gate 717c478bd9Sstevel@tonic-gate /* 727c478bd9Sstevel@tonic-gate * bitmap is a ulong_t *, bitindex an index_t 737c478bd9Sstevel@tonic-gate * 747c478bd9Sstevel@tonic-gate * The macros BT_WIM and BT_BIW internal; there is no need 757c478bd9Sstevel@tonic-gate * for users of this package to use them. 767c478bd9Sstevel@tonic-gate */ 777c478bd9Sstevel@tonic-gate 787c478bd9Sstevel@tonic-gate /* 797c478bd9Sstevel@tonic-gate * word in map 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate #define BT_WIM(bitmap, bitindex) \ 827c478bd9Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT]) 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * bit in word 857c478bd9Sstevel@tonic-gate */ 867c478bd9Sstevel@tonic-gate #define BT_BIW(bitindex) \ 877c478bd9Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK)) 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #ifdef _LP64 907c478bd9Sstevel@tonic-gate #define BT_WIM32(bitmap, bitindex) \ 917c478bd9Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT32]) 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate #define BT_BIW32(bitindex) \ 947c478bd9Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK32)) 957c478bd9Sstevel@tonic-gate #endif 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * These are public macros 997c478bd9Sstevel@tonic-gate * 1007c478bd9Sstevel@tonic-gate * BT_BITOUL == n bits to n ulong_t's 1017c478bd9Sstevel@tonic-gate */ 1027c478bd9Sstevel@tonic-gate #define BT_BITOUL(nbits) \ 1037c478bd9Sstevel@tonic-gate (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL) 1047c478bd9Sstevel@tonic-gate #define BT_SIZEOFMAP(nbits) \ 1057c478bd9Sstevel@tonic-gate (BT_BITOUL(nbits) * sizeof (ulong_t)) 1067c478bd9Sstevel@tonic-gate #define BT_TEST(bitmap, bitindex) \ 1077c478bd9Sstevel@tonic-gate ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0) 1087c478bd9Sstevel@tonic-gate #define BT_SET(bitmap, bitindex) \ 1097c478bd9Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); } 1107c478bd9Sstevel@tonic-gate #define BT_CLEAR(bitmap, bitindex) \ 1117c478bd9Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate #ifdef _LP64 1147c478bd9Sstevel@tonic-gate #define BT_BITOUL32(nbits) \ 1157c478bd9Sstevel@tonic-gate (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32) 1167c478bd9Sstevel@tonic-gate #define BT_SIZEOFMAP32(nbits) \ 1177c478bd9Sstevel@tonic-gate (BT_BITOUL32(nbits) * sizeof (uint_t)) 1187c478bd9Sstevel@tonic-gate #define BT_TEST32(bitmap, bitindex) \ 1197c478bd9Sstevel@tonic-gate ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0) 1207c478bd9Sstevel@tonic-gate #define BT_SET32(bitmap, bitindex) \ 1217c478bd9Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); } 1227c478bd9Sstevel@tonic-gate #define BT_CLEAR32(bitmap, bitindex) \ 1237c478bd9Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); } 1247c478bd9Sstevel@tonic-gate #endif /* _LP64 */ 1257c478bd9Sstevel@tonic-gate 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_ASM) 1287c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * return next available bit index from map with specified number of bits 1327c478bd9Sstevel@tonic-gate */ 1337c478bd9Sstevel@tonic-gate extern index_t bt_availbit(ulong_t *bitmap, size_t nbits); 1347c478bd9Sstevel@tonic-gate /* 1357c478bd9Sstevel@tonic-gate * find the highest order bit that is on, and is within or below 1367c478bd9Sstevel@tonic-gate * the word specified by wx 1377c478bd9Sstevel@tonic-gate */ 1387c478bd9Sstevel@tonic-gate extern int bt_gethighbit(ulong_t *mapp, int wx); 1397c478bd9Sstevel@tonic-gate extern int bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2, 1407c478bd9Sstevel@tonic-gate size_t end_pos); 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Find highest and lowest one bit set. 1437c478bd9Sstevel@tonic-gate * Returns bit number + 1 of bit that is set, otherwise returns 0. 1447c478bd9Sstevel@tonic-gate * Low order bit is 0, high order bit is 31. 1457c478bd9Sstevel@tonic-gate */ 1467c478bd9Sstevel@tonic-gate extern int highbit(ulong_t); 1477c478bd9Sstevel@tonic-gate extern int lowbit(ulong_t); 1487c478bd9Sstevel@tonic-gate extern int bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop); 1497c478bd9Sstevel@tonic-gate extern void bt_copy(ulong_t *, ulong_t *, ulong_t); 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * find the parity 1537c478bd9Sstevel@tonic-gate */ 1547c478bd9Sstevel@tonic-gate extern int odd_parity(ulong_t); 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Atomically set/clear bits 1587c478bd9Sstevel@tonic-gate * Atomic exclusive operations will set "result" to "-1" 1597c478bd9Sstevel@tonic-gate * if the bit is already set/cleared. "result" will be set 1607c478bd9Sstevel@tonic-gate * to 0 otherwise. 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate #define BT_ATOMIC_SET(bitmap, bitindex) \ 1637c478bd9Sstevel@tonic-gate { atomic_or_long(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); } 1647c478bd9Sstevel@tonic-gate #define BT_ATOMIC_CLEAR(bitmap, bitindex) \ 1657c478bd9Sstevel@tonic-gate { atomic_and_long(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); } 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate #define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \ 1687c478bd9Sstevel@tonic-gate { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \ 1697c478bd9Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 1707c478bd9Sstevel@tonic-gate #define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \ 1717c478bd9Sstevel@tonic-gate { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \ 1727c478bd9Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 1737c478bd9Sstevel@tonic-gate 1747c478bd9Sstevel@tonic-gate /* 1757c478bd9Sstevel@tonic-gate * Extracts bits between index h (high, inclusive) and l (low, exclusive) from 1767c478bd9Sstevel@tonic-gate * u, which must be an unsigned integer. 1777c478bd9Sstevel@tonic-gate */ 1787c478bd9Sstevel@tonic-gate #define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU)) 1797c478bd9Sstevel@tonic-gate 1807c478bd9Sstevel@tonic-gate #endif /* _KERNEL && !_ASM */ 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate #endif 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate #endif /* _SYS_BITMAP_H */ 187