xref: /freebsd/sys/contrib/openzfs/include/sys/bitmap.h (revision 535af610)
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 https://opensource.org/licenses/CDDL-1.0.
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 #ifdef	__cplusplus
35 extern "C" {
36 #endif
37 
38 /*
39  * Operations on bitmaps of arbitrary size
40  * A bitmap is a vector of 1 or more ulong_t's.
41  * The user of the package is responsible for range checks and keeping
42  * track of sizes.
43  */
44 
45 #ifdef _LP64
46 #define	BT_ULSHIFT	6 /* log base 2 of BT_NBIPUL, to extract word index */
47 #define	BT_ULSHIFT32	5 /* log base 2 of BT_NBIPUL, to extract word index */
48 #else
49 #define	BT_ULSHIFT	5 /* log base 2 of BT_NBIPUL, to extract word index */
50 #endif
51 
52 #define	BT_NBIPUL	(1 << BT_ULSHIFT)	/* n bits per ulong_t */
53 #define	BT_ULMASK	(BT_NBIPUL - 1)		/* to extract bit index */
54 
55 /*
56  * bitmap is a ulong_t *, bitindex an index_t
57  *
58  * The macros BT_WIM and BT_BIW internal; there is no need
59  * for users of this package to use them.
60  */
61 
62 /*
63  * word in map
64  */
65 #define	BT_WIM(bitmap, bitindex) \
66 	((bitmap)[(bitindex) >> BT_ULSHIFT])
67 /*
68  * bit in word
69  */
70 #define	BT_BIW(bitindex) \
71 	(1UL << ((bitindex) & BT_ULMASK))
72 
73 /*
74  * These are public macros
75  *
76  * BT_BITOUL == n bits to n ulong_t's
77  */
78 #define	BT_BITOUL(nbits) \
79 	(((nbits) + BT_NBIPUL - 1l) / BT_NBIPUL)
80 #define	BT_SIZEOFMAP(nbits) \
81 	(BT_BITOUL(nbits) * sizeof (ulong_t))
82 #define	BT_TEST(bitmap, bitindex) \
83 	((BT_WIM((bitmap), (bitindex)) & BT_BIW(bitindex)) ? 1 : 0)
84 #define	BT_SET(bitmap, bitindex) \
85 	{ BT_WIM((bitmap), (bitindex)) |= BT_BIW(bitindex); }
86 #define	BT_CLEAR(bitmap, bitindex) \
87 	{ BT_WIM((bitmap), (bitindex)) &= ~BT_BIW(bitindex); }
88 
89 #ifdef	__cplusplus
90 }
91 #endif
92 
93 #endif	/* _SYS_BITMAP_H */
94