xref: /netbsd-src/share/man/man3/bitmap.3 (revision f55f5e634e99d4f328f3725a3bb4a7bfe0206f95)
1.\"	$NetBSD: bitmap.3,v 1.12 2018/03/08 06:47:30 wiz Exp $
2.\"
3.\" Copyright (c) 2012 The NetBSD Foundation, Inc.
4.\" All rights reserved.
5.\"
6.\" This code is derived from software contributed to The NetBSD Foundation
7.\" by Christos Zoulas.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\"    notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\"    notice, this list of conditions and the following disclaimer in the
16.\"    documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21.\" PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28.\" POSSIBILITY OF SUCH DAMAGE.
29.\"
30.Dd March 8, 2018
31.Dt BITMAP 3
32.Os
33.Sh NAME
34.Nm __BITMAP_CLR ,
35.Nm __BITMAP_ISSET ,
36.Nm __BITMAP_SET ,
37.Nm __BITMAP_SIZE ,
38.Nm __BITMAP_TYPE ,
39.Nm __BITMAP_ZERO
40.Nd bitmap manipulation macros
41.Sh LIBRARY
42.Lb libc
43.Sh SYNOPSIS
44.In sys/bitops.h
45.Fn __BITMAP_CLR "int bit" "type *bitmap"
46.Fn __BITMAP_ISSET "int bit" "type *bitmap"
47.Fn __BITMAP_SET "int bit" "type *bitmap"
48.Fn __BITMAP_SIZE "type" "int nbits"
49.Fn __BITMAP_TYPE "name" "type" "int nbits"
50.Fn __BITMAP_ZERO "type *bitmap"
51.Sh DESCRIPTION
52The supplied macros are similar to the
53.Xr select 2
54.Fn FD_SET
55family, to the
56.Xr setbit 9 ,
57macros and the
58.Xr bitstring 3
59library.
60They are different from
61.Fn FD_SET
62because they are designed to handle multiple sized bitmaps at the same time,
63and they can be of any integral type.
64They are different from
65.Xr setbit 9
66because they can operate on different integral types, not just on bytes.
67They are different from
68.Xr bitstring 3
69because they are just macros, they don't allocate memory or use code,
70and they can be used in both kernel and userland.
71.Pp
72The following macros are provided for manipulating creating and manipulating
73bitmaps:
74.Pp
75.Fn __BITMAP_CLR bit bitmap
76removes the given
77.Fa bit
78from the
79.Fa bitmap .
80.Pp
81.Fn __BITMAP_ISSET bit bitmap
82is non-zero if
83.Fa bit
84is a member of
85.Fa bitmap ,
86zero otherwise.
87.Pp
88.Fn __BITMAP_SET bit bitmap
89Sets the given
90.Fa bit
91in the
92.Fa bitmap .
93.Pp
94.Fn __BITMAP_SIZE type nbits
95Returns the number of elements would be required of the given
96.Fa type
97to hold
98.Fa nbits .
99.Pp
100.Fn __BITMAP_TYPE name type nbits
101Declares the properly sized bitmap structure
102of the given
103.Fa type
104that holds
105.Fa nbits
106and is named
107.Fa name .
108.Pp
109.Fn __BITMAP_ZERO bitmap
110initializes a descriptor set pointed to by
111.Fa bitmap
112to the null set.
113.Pp
114The behavior of these macros is undefined for negative
115bit values or ones greater than the number of bits the bitmap can hold.
116.Sh EXAMPLES
117.Bd -literal
118#include <sys/bitops.h>
119
120int
121main(int argc, char **argv)
122{
123	__BITMAP_TYPE(, uint32_t, 5000) bitmap;
124
125	/* Initialize the read set to null */
126	__BITMAP_ZERO(&bitmap);
127
128	/* Set bit 1 */
129	__BITMAP_SET(1, &bitmap);
130
131	for (size_t i = 0; i < 5000; i++) {
132		if (__BITMAP_ISSET(i, &bitmap)) {
133			/* Should just print 1 */
134			printf("Bit %zu is set\en", i);
135			__BITMAP_CLR(i, &bitmap);
136		}
137		break;
138	}
139	return 0;
140}
141.Ed
142.Sh SEE ALSO
143.Xr select 2 ,
144.Xr bitops 3 ,
145.Xr bitstring 3 ,
146.Xr setbit 9
147.Sh HISTORY
148The
149.Fn __BITMAP_*
150macros appeared in
151.Nx 7.0 .
152