xref: /openbsd-src/share/man/man3/bit_alloc.3 (revision d32639f6ddc06f615d8e07a47d97091b3531a48f)
1*d32639f6Sjmc.\"	$OpenBSD: bit_alloc.3,v 1.3 2022/09/11 06:38:11 jmc Exp $
24d9461faSderaadt.\"	$NetBSD: bitstring.3,v 1.4 1994/11/30 15:24:31 jtc Exp $
34d9461faSderaadt.\"
44d9461faSderaadt.\" Copyright (c) 1989, 1991, 1993
54d9461faSderaadt.\"	The Regents of the University of California.  All rights reserved.
64d9461faSderaadt.\"
74d9461faSderaadt.\" This code is derived from software contributed to Berkeley by
84d9461faSderaadt.\" Paul Vixie.
94d9461faSderaadt.\" Redistribution and use in source and binary forms, with or without
104d9461faSderaadt.\" modification, are permitted provided that the following conditions
114d9461faSderaadt.\" are met:
124d9461faSderaadt.\" 1. Redistributions of source code must retain the above copyright
134d9461faSderaadt.\"    notice, this list of conditions and the following disclaimer.
144d9461faSderaadt.\" 2. Redistributions in binary form must reproduce the above copyright
154d9461faSderaadt.\"    notice, this list of conditions and the following disclaimer in the
164d9461faSderaadt.\"    documentation and/or other materials provided with the distribution.
174d9461faSderaadt.\" 3. Neither the name of the University nor the names of its contributors
184d9461faSderaadt.\"    may be used to endorse or promote products derived from this software
194d9461faSderaadt.\"    without specific prior written permission.
204d9461faSderaadt.\"
214d9461faSderaadt.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
224d9461faSderaadt.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
234d9461faSderaadt.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
244d9461faSderaadt.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
254d9461faSderaadt.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
264d9461faSderaadt.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
274d9461faSderaadt.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
284d9461faSderaadt.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
294d9461faSderaadt.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
304d9461faSderaadt.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
314d9461faSderaadt.\" SUCH DAMAGE.
324d9461faSderaadt.\"
334d9461faSderaadt.\"     @(#)bitstring.3	8.1 (Berkeley) 7/19/93
344d9461faSderaadt.\"
35*d32639f6Sjmc.Dd $Mdocdate: September 11 2022 $
364d9461faSderaadt.Dt BIT_ALLOC 3
374d9461faSderaadt.Os
384d9461faSderaadt.Sh NAME
394d9461faSderaadt.Nm bit_alloc ,
404d9461faSderaadt.Nm bit_clear ,
414d9461faSderaadt.Nm bit_decl ,
424d9461faSderaadt.Nm bit_ffc ,
434d9461faSderaadt.Nm bit_ffs ,
444d9461faSderaadt.Nm bit_nclear ,
454d9461faSderaadt.Nm bit_nset ,
464d9461faSderaadt.Nm bit_set ,
474d9461faSderaadt.Nm bitstr_size ,
484d9461faSderaadt.Nm bit_test
494d9461faSderaadt.Nd bit-string manipulation macros
504d9461faSderaadt.Sh SYNOPSIS
514d9461faSderaadt.In bitstring.h
524d9461faSderaadt.Ft bitstr_t *
534d9461faSderaadt.Fn bit_alloc "int nbits"
544d9461faSderaadt.Fn bit_clear "bit_str name" "int bit"
554d9461faSderaadt.Fn bit_decl "bit_str name" "int nbits"
564d9461faSderaadt.Fn bit_ffc "bit_str name" "int nbits" "int *value"
574d9461faSderaadt.Fn bit_ffs "bit_str name" "int nbits" "int *value"
584d9461faSderaadt.Fn bit_nclear "bit_str name" "int start" "int stop"
594d9461faSderaadt.Fn bit_nset "bit_str name" "int start" "int stop"
604d9461faSderaadt.Fn bit_set "bit_str name" "int bit"
614d9461faSderaadt.Fn bitstr_size "int nbits"
624d9461faSderaadt.Fn bit_test "bit_str name" "int bit"
634d9461faSderaadt.Sh DESCRIPTION
644d9461faSderaadtThese macros operate on strings of bits.
654d9461faSderaadt.Pp
664d9461faSderaadtThe
674d9461faSderaadt.Fn bit_alloc
684d9461faSderaadtmacro returns a pointer of type
69*d32639f6Sjmc.Vt bitstr_t *
704d9461faSderaadtto sufficient space to store
714d9461faSderaadt.Fa nbits
724d9461faSderaadtbits, or
734d9461faSderaadt.Dv NULL
744d9461faSderaadtif no space is available.
754d9461faSderaadt.Pp
764d9461faSderaadtThe
774d9461faSderaadt.Fn bit_decl
784d9461faSderaadtmacro allocates sufficient space to store
794d9461faSderaadt.Fa nbits
804d9461faSderaadtbits on the stack.
814d9461faSderaadt.Pp
824d9461faSderaadtThe
834d9461faSderaadt.Fn bitstr_size
844d9461faSderaadtmacro returns the number of elements of type
85*d32639f6Sjmc.Vt bitstr_t
864d9461faSderaadtnecessary to store
874d9461faSderaadt.Fa nbits
884d9461faSderaadtbits.
894d9461faSderaadtThis is useful for copying bit strings.
904d9461faSderaadt.Pp
914d9461faSderaadtThe
924d9461faSderaadt.Fn bit_clear
934d9461faSderaadtand
944d9461faSderaadt.Fn bit_set
954d9461faSderaadtmacros clear or set the zero-based numbered bit
964d9461faSderaadt.Fa bit ,
974d9461faSderaadtin the bit string
984d9461faSderaadt.Ar name .
994d9461faSderaadt.Pp
1004d9461faSderaadtThe
1014d9461faSderaadt.Fn bit_nclear
1024d9461faSderaadtand
1034d9461faSderaadt.Fn bit_nset
1044d9461faSderaadtmacros clear or set the zero-based numbered bits from
1054d9461faSderaadt.Fa start
1064d9461faSderaadtto
1074d9461faSderaadt.Fa stop
1084d9461faSderaadtin the bit string
1094d9461faSderaadt.Ar name .
1104d9461faSderaadt.Pp
1114d9461faSderaadtThe
1124d9461faSderaadt.Fn bit_test
1134d9461faSderaadtmacro evaluates to non-zero if the zero-based numbered bit
1144d9461faSderaadt.Fa bit
1154d9461faSderaadtof bit string
1164d9461faSderaadt.Fa name
1174d9461faSderaadtis set, and zero otherwise.
1184d9461faSderaadt.Pp
1194d9461faSderaadtThe
1204d9461faSderaadt.Fn bit_ffs
1214d9461faSderaadtmacro stores in the location referenced by
1224d9461faSderaadt.Fa value
1234d9461faSderaadtthe zero-based number of the first bit set in the array of
1244d9461faSderaadt.Fa nbits
1254d9461faSderaadtbits referenced by
1264d9461faSderaadt.Fa name .
1274d9461faSderaadtIf no bits are set, the location referenced by
1284d9461faSderaadt.Fa value
1294d9461faSderaadtis set to \-1.
1304d9461faSderaadt.Pp
1314d9461faSderaadtThe
1324d9461faSderaadt.Fn bit_ffc
1334d9461faSderaadtmacro stores in the location referenced by
1344d9461faSderaadt.Fa value
1354d9461faSderaadtthe zero-based number of the first bit not set in the array of
1364d9461faSderaadt.Fa nbits
1374d9461faSderaadtbits referenced by
1384d9461faSderaadt.Fa name .
1394d9461faSderaadtIf all bits are set, the location referenced by
1404d9461faSderaadt.Fa value
1414d9461faSderaadtis set to \-1.
1424d9461faSderaadt.Pp
1434d9461faSderaadtThe arguments to these macros are evaluated only once and may safely
1444d9461faSderaadthave side effects.
1454d9461faSderaadt.Sh EXAMPLES
1464d9461faSderaadt.Bd -literal -offset indent
1474d9461faSderaadt#include <limits.h>
1484d9461faSderaadt#include <bitstring.h>
1494d9461faSderaadt
1504d9461faSderaadt\&...
1514d9461faSderaadt#define	LPR_BUSY_BIT		0
1524d9461faSderaadt#define	LPR_FORMAT_BIT		1
1534d9461faSderaadt#define	LPR_DOWNLOAD_BIT	2
1544d9461faSderaadt\&...
1554d9461faSderaadt#define	LPR_AVAILABLE_BIT	9
1564d9461faSderaadt#define	LPR_MAX_BITS		10
1574d9461faSderaadt
1584d9461faSderaadtmake_lpr_available()
1594d9461faSderaadt{
1604d9461faSderaadt	bitstr_t bit_decl(bitlist, LPR_MAX_BITS);
1614d9461faSderaadt	...
1624d9461faSderaadt	bit_nclear(bitlist, 0, LPR_MAX_BITS - 1);
1634d9461faSderaadt	...
1644d9461faSderaadt	if (!bit_test(bitlist, LPR_BUSY_BIT)) {
1654d9461faSderaadt		bit_clear(bitlist, LPR_FORMAT_BIT);
1664d9461faSderaadt		bit_clear(bitlist, LPR_DOWNLOAD_BIT);
1674d9461faSderaadt		bit_set(bitlist, LPR_AVAILABLE_BIT);
1684d9461faSderaadt	}
1694d9461faSderaadt}
1704d9461faSderaadt.Ed
1714d9461faSderaadt.Sh SEE ALSO
1724d9461faSderaadt.Xr malloc 3
1734d9461faSderaadt.Sh HISTORY
1744d9461faSderaadtThese functions first appeared in
1756e23b9dcSjsg.Bx 4.3 Reno .
176