xref: /freebsd/sys/cddl/compat/opensolaris/sys/bitmap.h (revision 069ac184)
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 2005 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 _COMPAT_OPENSOLARIS_SYS_BITMAP_H
32 #define	_COMPAT_OPENSOLARIS_SYS_BITMAP_H
33 
34 #include <sys/atomic.h>
35 
36 /*
37  * Operations on bitmaps of arbitrary size
38  * A bitmap is a vector of 1 or more ulong_t's.
39  * The user of the package is responsible for range checks and keeping
40  * track of sizes.
41  */
42 
43 #ifdef _LP64
44 #define	BT_ULSHIFT	6 /* log base 2 of BT_NBIPUL, to extract word index */
45 #define	BT_ULSHIFT32	5 /* log base 2 of BT_NBIPUL, to extract word index */
46 #else
47 #define	BT_ULSHIFT	5 /* log base 2 of BT_NBIPUL, to extract word index */
48 #endif
49 
50 #define	BT_NBIPUL	(1 << BT_ULSHIFT)	/* n bits per ulong_t */
51 #define	BT_ULMASK	(BT_NBIPUL - 1)		/* to extract bit index */
52 
53 #ifdef _LP64
54 #define	BT_NBIPUL32	(1 << BT_ULSHIFT32)	/* n bits per ulong_t */
55 #define	BT_ULMASK32	(BT_NBIPUL32 - 1)	/* to extract bit index */
56 #define	BT_ULMAXMASK	0xffffffffffffffff	/* used by bt_getlowbit */
57 #else
58 #define	BT_ULMAXMASK	0xffffffff
59 #endif
60 
61 /*
62  * bitmap is a ulong_t *, bitindex an index_t
63  *
64  * The macros BT_WIM and BT_BIW internal; there is no need
65  * for users of this package to use them.
66  */
67 
68 /*
69  * word in map
70  */
71 #define	BT_WIM(bitmap, bitindex) \
72 	((bitmap)[(bitindex) >> BT_ULSHIFT])
73 /*
74  * bit in word
75  */
76 #define	BT_BIW(bitindex) \
77 	(1UL << ((bitindex) & BT_ULMASK))
78 
79 #ifdef _LP64
80 #define	BT_WIM32(bitmap, bitindex) \
81 	((bitmap)[(bitindex) >> BT_ULSHIFT32])
82 
83 #define	BT_BIW32(bitindex) \
84 	(1UL << ((bitindex) & BT_ULMASK32))
85 #endif
86 
87 /*
88  * These are public macros
89  *
90  * BT_BITOUL == n bits to n ulong_t's
91  */
92 #define	BT_BITOUL(nbits) \
93 	(((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
94 #define	BT_SIZEOFMAP(nbits) \
95 	(BT_BITOUL(nbits) * sizeof (ulong_t))
96 #define	BT_TEST(bitmap, bitindex) \
97 	((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
98 #define	BT_SET(bitmap, bitindex) \
99 	{ BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
100 #define	BT_CLEAR(bitmap, bitindex) \
101 	{ BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
102 
103 #ifdef _LP64
104 #define	BT_BITOUL32(nbits) \
105 	(((nbits) + BT_NBIPUL32 - 1l) / BT_NBIPUL32)
106 #define	BT_SIZEOFMAP32(nbits) \
107 	(BT_BITOUL32(nbits) * sizeof (uint_t))
108 #define	BT_TEST32(bitmap, bitindex) \
109 	((BT_WIM32((bitmap), (bitindex)) & BT_BIW32(bitindex)) ? 1 : 0)
110 #define	BT_SET32(bitmap, bitindex) \
111 	{ BT_WIM32((bitmap), (bitindex)) |= BT_BIW32(bitindex); }
112 #define	BT_CLEAR32(bitmap, bitindex) \
113 	{ BT_WIM32((bitmap), (bitindex)) &= ~BT_BIW32(bitindex); }
114 #endif /* _LP64 */
115 
116 #endif	/* _COMPAT_OPENSOLARIS_SYS_BITMAP_H */
117