1*f55f5e63Swiz.\" $NetBSD: bitmap.3,v 1.12 2018/03/08 06:47:30 wiz Exp $ 29df40f33Schristos.\" 39df40f33Schristos.\" Copyright (c) 2012 The NetBSD Foundation, Inc. 49df40f33Schristos.\" All rights reserved. 59df40f33Schristos.\" 69df40f33Schristos.\" This code is derived from software contributed to The NetBSD Foundation 79df40f33Schristos.\" by Christos Zoulas. 89df40f33Schristos.\" 99df40f33Schristos.\" Redistribution and use in source and binary forms, with or without 109df40f33Schristos.\" modification, are permitted provided that the following conditions 119df40f33Schristos.\" are met: 129df40f33Schristos.\" 1. Redistributions of source code must retain the above copyright 139df40f33Schristos.\" notice, this list of conditions and the following disclaimer. 149df40f33Schristos.\" 2. Redistributions in binary form must reproduce the above copyright 159df40f33Schristos.\" notice, this list of conditions and the following disclaimer in the 169df40f33Schristos.\" documentation and/or other materials provided with the distribution. 179df40f33Schristos.\" 189df40f33Schristos.\" THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 199df40f33Schristos.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 209df40f33Schristos.\" TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 219df40f33Schristos.\" PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 229df40f33Schristos.\" BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 239df40f33Schristos.\" CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 249df40f33Schristos.\" SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 259df40f33Schristos.\" INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 269df40f33Schristos.\" CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 279df40f33Schristos.\" ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 289df40f33Schristos.\" POSSIBILITY OF SUCH DAMAGE. 299df40f33Schristos.\" 30*f55f5e63Swiz.Dd March 8, 2018 31349bee0bSwiz.Dt BITMAP 3 329df40f33Schristos.Os 339df40f33Schristos.Sh NAME 349df40f33Schristos.Nm __BITMAP_CLR , 359df40f33Schristos.Nm __BITMAP_ISSET , 369df40f33Schristos.Nm __BITMAP_SET , 379df40f33Schristos.Nm __BITMAP_SIZE , 385d0cc05dSchristos.Nm __BITMAP_TYPE , 399df40f33Schristos.Nm __BITMAP_ZERO 409df40f33Schristos.Nd bitmap manipulation macros 419df40f33Schristos.Sh LIBRARY 429df40f33Schristos.Lb libc 439df40f33Schristos.Sh SYNOPSIS 449df40f33Schristos.In sys/bitops.h 459df40f33Schristos.Fn __BITMAP_CLR "int bit" "type *bitmap" 469df40f33Schristos.Fn __BITMAP_ISSET "int bit" "type *bitmap" 479df40f33Schristos.Fn __BITMAP_SET "int bit" "type *bitmap" 489df40f33Schristos.Fn __BITMAP_SIZE "type" "int nbits" 495d0cc05dSchristos.Fn __BITMAP_TYPE "name" "type" "int nbits" 509df40f33Schristos.Fn __BITMAP_ZERO "type *bitmap" 519df40f33Schristos.Sh DESCRIPTION 529df40f33SchristosThe supplied macros are similar to the 539df40f33Schristos.Xr select 2 549df40f33Schristos.Fn FD_SET 55c04a45abSchristosfamily, to the 56c04a45abSchristos.Xr setbit 9 , 57c04a45abSchristosmacros and the 589df40f33Schristos.Xr bitstring 3 599df40f33Schristoslibrary. 609df40f33SchristosThey are different from 619df40f33Schristos.Fn FD_SET 629df40f33Schristosbecause they are designed to handle multiple sized bitmaps at the same time, 639df40f33Schristosand they can be of any integral type. 649df40f33SchristosThey are different from 65c04a45abSchristos.Xr setbit 9 66c04a45abSchristosbecause they can operate on different integral types, not just on bytes. 67c04a45abSchristosThey are different from 689df40f33Schristos.Xr bitstring 3 699df40f33Schristosbecause they are just macros, they don't allocate memory or use code, 709df40f33Schristosand they can be used in both kernel and userland. 719df40f33Schristos.Pp 729df40f33SchristosThe following macros are provided for manipulating creating and manipulating 739df40f33Schristosbitmaps: 749df40f33Schristos.Pp 75349bee0bSwiz.Fn __BITMAP_CLR bit bitmap 765d0cc05dSchristosremoves the given 775d0cc05dSchristos.Fa bit 785d0cc05dSchristosfrom the 795d0cc05dSchristos.Fa bitmap . 809df40f33Schristos.Pp 819df40f33Schristos.Fn __BITMAP_ISSET bit bitmap 829df40f33Schristosis non-zero if 839df40f33Schristos.Fa bit 849df40f33Schristosis a member of 859df40f33Schristos.Fa bitmap , 869df40f33Schristoszero otherwise. 879df40f33Schristos.Pp 889df40f33Schristos.Fn __BITMAP_SET bit bitmap 899df40f33SchristosSets the given 909df40f33Schristos.Fa bit 919df40f33Schristosin the 929df40f33Schristos.Fa bitmap . 939df40f33Schristos.Pp 945d0cc05dSchristos.Fn __BITMAP_SIZE type nbits 955d0cc05dSchristosReturns the number of elements would be required of the given 965d0cc05dSchristos.Fa type 975d0cc05dSchristosto hold 985d0cc05dSchristos.Fa nbits . 995d0cc05dSchristos.Pp 1005d0cc05dSchristos.Fn __BITMAP_TYPE name type nbits 1015d0cc05dSchristosDeclares the properly sized bitmap structure 1025d0cc05dSchristosof the given 1035d0cc05dSchristos.Fa type 1045d0cc05dSchristosthat holds 1055d0cc05dSchristos.Fa nbits 1065d0cc05dSchristosand is named 1075d0cc05dSchristos.Fa name . 1085d0cc05dSchristos.Pp 109ee301cc0Smartin.Fn __BITMAP_ZERO bitmap 1105d0cc05dSchristosinitializes a descriptor set pointed to by 1115d0cc05dSchristos.Fa bitmap 1125d0cc05dSchristosto the null set. 1139df40f33Schristos.Pp 1149df40f33SchristosThe behavior of these macros is undefined for negative 1159df40f33Schristosbit values or ones greater than the number of bits the bitmap can hold. 1169df40f33Schristos.Sh EXAMPLES 1179df40f33Schristos.Bd -literal 11801869ca4Swiz#include <sys/bitops.h> 1199df40f33Schristos 1209df40f33Schristosint 1219df40f33Schristosmain(int argc, char **argv) 1229df40f33Schristos{ 1235d0cc05dSchristos __BITMAP_TYPE(, uint32_t, 5000) bitmap; 1249df40f33Schristos 1259df40f33Schristos /* Initialize the read set to null */ 12601869ca4Swiz __BITMAP_ZERO(&bitmap); 1279df40f33Schristos 1289df40f33Schristos /* Set bit 1 */ 12901869ca4Swiz __BITMAP_SET(1, &bitmap); 1309df40f33Schristos 13101869ca4Swiz for (size_t i = 0; i < 5000; i++) { 13201869ca4Swiz if (__BITMAP_ISSET(i, &bitmap)) { 1339df40f33Schristos /* Should just print 1 */ 1349df40f33Schristos printf("Bit %zu is set\en", i); 13501869ca4Swiz __BITMAP_CLR(i, &bitmap); 1369df40f33Schristos } 1379df40f33Schristos break; 1389df40f33Schristos } 1399df40f33Schristos return 0; 1409df40f33Schristos} 1419df40f33Schristos.Ed 1429df40f33Schristos.Sh SEE ALSO 1439df40f33Schristos.Xr select 2 , 144f8b5dc2cSjruoho.Xr bitops 3 , 145c04a45abSchristos.Xr bitstring 3 , 146c04a45abSchristos.Xr setbit 9 1479df40f33Schristos.Sh HISTORY 1489df40f33SchristosThe 1499df40f33Schristos.Fn __BITMAP_* 1509df40f33Schristosmacros appeared in 151f2bb7e66Snjoly.Nx 7.0 . 152