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 52b616c6cSwesolows * Common Development and Distribution License (the "License"). 62b616c6cSwesolows * 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 */ 212b616c6cSwesolows 227c478bd9Sstevel@tonic-gate /* 239acbbeafSnn35248 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 27bf16b11eSMatthew Ahrens /* 28bf16b11eSMatthew Ahrens * Copyright (c) 2014 by Delphix. All rights reserved. 29bf16b11eSMatthew Ahrens */ 30bf16b11eSMatthew Ahrens 317c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 327c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate #ifndef _SYS_BITMAP_H 367c478bd9Sstevel@tonic-gate #define _SYS_BITMAP_H 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate #ifdef __cplusplus 397c478bd9Sstevel@tonic-gate extern "C" { 407c478bd9Sstevel@tonic-gate #endif 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include <sys/feature_tests.h> 432b616c6cSwesolows #if defined(__GNUC__) && defined(_ASM_INLINES) && \ 442b616c6cSwesolows (defined(__i386) || defined(__amd64)) 457c478bd9Sstevel@tonic-gate #include <asm/bitmap.h> 467c478bd9Sstevel@tonic-gate #endif 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate /* 497c478bd9Sstevel@tonic-gate * Operations on bitmaps of arbitrary size 507c478bd9Sstevel@tonic-gate * A bitmap is a vector of 1 or more ulong_t's. 517c478bd9Sstevel@tonic-gate * The user of the package is responsible for range checks and keeping 527c478bd9Sstevel@tonic-gate * track of sizes. 537c478bd9Sstevel@tonic-gate */ 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate #ifdef _LP64 567c478bd9Sstevel@tonic-gate #define BT_ULSHIFT 6 /* log base 2 of BT_NBIPUL, to extract word index */ 577c478bd9Sstevel@tonic-gate #define BT_ULSHIFT32 5 /* log base 2 of BT_NBIPUL, to extract word index */ 587c478bd9Sstevel@tonic-gate #else 597c478bd9Sstevel@tonic-gate #define BT_ULSHIFT 5 /* log base 2 of BT_NBIPUL, to extract word index */ 607c478bd9Sstevel@tonic-gate #endif 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #define BT_NBIPUL (1 << BT_ULSHIFT) /* n bits per ulong_t */ 637c478bd9Sstevel@tonic-gate #define BT_ULMASK (BT_NBIPUL - 1) /* to extract bit index */ 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate #ifdef _LP64 667c478bd9Sstevel@tonic-gate #define BT_NBIPUL32 (1 << BT_ULSHIFT32) /* n bits per ulong_t */ 677c478bd9Sstevel@tonic-gate #define BT_ULMASK32 (BT_NBIPUL32 - 1) /* to extract bit index */ 687c478bd9Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffffffffffff /* used by bt_getlowbit */ 697c478bd9Sstevel@tonic-gate #else 707c478bd9Sstevel@tonic-gate #define BT_ULMAXMASK 0xffffffff 717c478bd9Sstevel@tonic-gate #endif 727c478bd9Sstevel@tonic-gate 737c478bd9Sstevel@tonic-gate /* 747c478bd9Sstevel@tonic-gate * bitmap is a ulong_t *, bitindex an index_t 757c478bd9Sstevel@tonic-gate * 767c478bd9Sstevel@tonic-gate * The macros BT_WIM and BT_BIW internal; there is no need 777c478bd9Sstevel@tonic-gate * for users of this package to use them. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate /* 817c478bd9Sstevel@tonic-gate * word in map 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate #define BT_WIM(bitmap, bitindex) \ 847c478bd9Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT]) 857c478bd9Sstevel@tonic-gate /* 867c478bd9Sstevel@tonic-gate * bit in word 877c478bd9Sstevel@tonic-gate */ 887c478bd9Sstevel@tonic-gate #define BT_BIW(bitindex) \ 897c478bd9Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK)) 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate #ifdef _LP64 927c478bd9Sstevel@tonic-gate #define BT_WIM32(bitmap, bitindex) \ 937c478bd9Sstevel@tonic-gate ((bitmap)[(bitindex) >> BT_ULSHIFT32]) 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate #define BT_BIW32(bitindex) \ 967c478bd9Sstevel@tonic-gate (1UL << ((bitindex) & BT_ULMASK32)) 977c478bd9Sstevel@tonic-gate #endif 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate /* 1007c478bd9Sstevel@tonic-gate * These are public macros 1017c478bd9Sstevel@tonic-gate * 1027c478bd9Sstevel@tonic-gate * BT_BITOUL == n bits to n ulong_t's 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate #define BT_BITOUL(nbits) \ 1057c478bd9Sstevel@tonic-gate (((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL) 1067c478bd9Sstevel@tonic-gate #define BT_SIZEOFMAP(nbits) \ 1077c478bd9Sstevel@tonic-gate (BT_BITOUL(nbits) * sizeof (ulong_t)) 1087c478bd9Sstevel@tonic-gate #define BT_TEST(bitmap, bitindex) \ 1097c478bd9Sstevel@tonic-gate ((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0) 1107c478bd9Sstevel@tonic-gate #define BT_SET(bitmap, bitindex) \ 1117c478bd9Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); } 1127c478bd9Sstevel@tonic-gate #define BT_CLEAR(bitmap, bitindex) \ 1137c478bd9Sstevel@tonic-gate { BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); } 1147c478bd9Sstevel@tonic-gate 1157c478bd9Sstevel@tonic-gate #ifdef _LP64 1167c478bd9Sstevel@tonic-gate #define BT_BITOUL32(nbits) \ 1177c478bd9Sstevel@tonic-gate (((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32) 1187c478bd9Sstevel@tonic-gate #define BT_SIZEOFMAP32(nbits) \ 1197c478bd9Sstevel@tonic-gate (BT_BITOUL32(nbits) * sizeof (uint_t)) 1207c478bd9Sstevel@tonic-gate #define BT_TEST32(bitmap, bitindex) \ 1217c478bd9Sstevel@tonic-gate ((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0) 1227c478bd9Sstevel@tonic-gate #define BT_SET32(bitmap, bitindex) \ 1237c478bd9Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); } 1247c478bd9Sstevel@tonic-gate #define BT_CLEAR32(bitmap, bitindex) \ 1257c478bd9Sstevel@tonic-gate { BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); } 1267c478bd9Sstevel@tonic-gate #endif /* _LP64 */ 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate 1299acbbeafSnn35248 /* 1309acbbeafSnn35248 * BIT_ONLYONESET is a private macro not designed for bitmaps of 1319acbbeafSnn35248 * arbitrary size. u must be an unsigned integer/long. It returns 1329acbbeafSnn35248 * true if one and only one bit is set in u. 1339acbbeafSnn35248 */ 1349acbbeafSnn35248 #define BIT_ONLYONESET(u) \ 1359acbbeafSnn35248 ((((u) == 0) ? 0 : ((u) & ((u) - 1)) == 0)) 1369acbbeafSnn35248 1377c478bd9Sstevel@tonic-gate #if defined(_KERNEL) && !defined(_ASM) 1387c478bd9Sstevel@tonic-gate #include <sys/atomic.h> 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate /* 1417c478bd9Sstevel@tonic-gate * return next available bit index from map with specified number of bits 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate extern index_t bt_availbit(ulong_t *bitmap, size_t nbits); 1447c478bd9Sstevel@tonic-gate /* 1457c478bd9Sstevel@tonic-gate * find the highest order bit that is on, and is within or below 1467c478bd9Sstevel@tonic-gate * the word specified by wx 1477c478bd9Sstevel@tonic-gate */ 1487c478bd9Sstevel@tonic-gate extern int bt_gethighbit(ulong_t *mapp, int wx); 1497c478bd9Sstevel@tonic-gate extern int bt_range(ulong_t *bitmap, size_t *pos1, size_t *pos2, 1507c478bd9Sstevel@tonic-gate size_t end_pos); 1517c478bd9Sstevel@tonic-gate /* 1527c478bd9Sstevel@tonic-gate * Find highest and lowest one bit set. 1537c478bd9Sstevel@tonic-gate * Returns bit number + 1 of bit that is set, otherwise returns 0. 1547c478bd9Sstevel@tonic-gate * Low order bit is 0, high order bit is 31. 1557c478bd9Sstevel@tonic-gate */ 1567c478bd9Sstevel@tonic-gate extern int highbit(ulong_t); 157bf16b11eSMatthew Ahrens extern int highbit64(uint64_t); 1587c478bd9Sstevel@tonic-gate extern int lowbit(ulong_t); 1597c478bd9Sstevel@tonic-gate extern int bt_getlowbit(ulong_t *bitmap, size_t start, size_t stop); 1607c478bd9Sstevel@tonic-gate extern void bt_copy(ulong_t *, ulong_t *, ulong_t); 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate /* 1637c478bd9Sstevel@tonic-gate * find the parity 1647c478bd9Sstevel@tonic-gate */ 1657c478bd9Sstevel@tonic-gate extern int odd_parity(ulong_t); 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate /* 1687c478bd9Sstevel@tonic-gate * Atomically set/clear bits 1697c478bd9Sstevel@tonic-gate * Atomic exclusive operations will set "result" to "-1" 1707c478bd9Sstevel@tonic-gate * if the bit is already set/cleared. "result" will be set 1717c478bd9Sstevel@tonic-gate * to 0 otherwise. 1727c478bd9Sstevel@tonic-gate */ 1737c478bd9Sstevel@tonic-gate #define BT_ATOMIC_SET(bitmap, bitindex) \ 174*75d94465SJosef 'Jeff' Sipek { atomic_or_ulong(&(BT_WIM(bitmap, bitindex)), BT_BIW(bitindex)); } 1757c478bd9Sstevel@tonic-gate #define BT_ATOMIC_CLEAR(bitmap, bitindex) \ 176*75d94465SJosef 'Jeff' Sipek { atomic_and_ulong(&(BT_WIM(bitmap, bitindex)), ~BT_BIW(bitindex)); } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate #define BT_ATOMIC_SET_EXCL(bitmap, bitindex, result) \ 1797c478bd9Sstevel@tonic-gate { result = atomic_set_long_excl(&(BT_WIM(bitmap, bitindex)), \ 1807c478bd9Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 1817c478bd9Sstevel@tonic-gate #define BT_ATOMIC_CLEAR_EXCL(bitmap, bitindex, result) \ 1827c478bd9Sstevel@tonic-gate { result = atomic_clear_long_excl(&(BT_WIM(bitmap, bitindex)), \ 1837c478bd9Sstevel@tonic-gate (bitindex) % BT_NBIPUL); } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate /* 1867c478bd9Sstevel@tonic-gate * Extracts bits between index h (high, inclusive) and l (low, exclusive) from 1877c478bd9Sstevel@tonic-gate * u, which must be an unsigned integer. 1887c478bd9Sstevel@tonic-gate */ 1897c478bd9Sstevel@tonic-gate #define BITX(u, h, l) (((u) >> (l)) & ((1LU << ((h) - (l) + 1LU)) - 1LU)) 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate #endif /* _KERNEL && !_ASM */ 1927c478bd9Sstevel@tonic-gate 1937c478bd9Sstevel@tonic-gate #ifdef __cplusplus 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate #endif 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate #endif /* _SYS_BITMAP_H */ 198