diff --git a/share/man/man3/bitstring.3 b/share/man/man3/bitstring.3 index ac87ac6d9124..808cd48f384b 100644 --- a/share/man/man3/bitstring.3 +++ b/share/man/man3/bitstring.3 @@ -1,471 +1,471 @@ .\" Copyright (c) 1989, 1991, 1993 .\" The Regents of the University of California. All rights reserved. .\" .\" This code is derived from software contributed to Berkeley by .\" Paul Vixie. .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions and the following disclaimer. .\" 2. Redistributions in binary form must reproduce the above copyright .\" notice, this list of conditions and the following disclaimer in the .\" documentation and/or other materials provided with the distribution. .\" 3. Neither the name of the University nor the names of its contributors .\" may be used to endorse or promote products derived from this software .\" without specific prior written permission. .\" .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF .\" SUCH DAMAGE. .\" .\" Copyright (c) 2014,2016 Spectra Logic Corporation .\" All rights reserved. .\" .\" Redistribution and use in source and binary forms, with or without .\" modification, are permitted provided that the following conditions .\" are met: .\" 1. Redistributions of source code must retain the above copyright .\" notice, this list of conditions, and the following disclaimer, .\" without modification. .\" 2. Redistributions in binary form must reproduce at minimum a disclaimer .\" substantially similar to the "NO WARRANTY" disclaimer below .\" ("Disclaimer") and any redistribution must be conditioned upon .\" including a substantially similar Disclaimer requirement for further .\" binary redistribution. .\" .\" NO WARRANTY .\" THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS .\" "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT .\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR .\" A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT .\" HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, .\" STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING .\" IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE .\" POSSIBILITY OF SUCH DAMAGES. .\" .\" @(#)bitstring.3 8.1 (Berkeley) 7/19/93 .\" -.Dd August 8, 2021 +.Dd November 21, 2023 .Dt BITSTRING 3 .Os .Sh NAME .Nm bit_alloc , .Nm bit_clear , .Nm bit_count , .Nm bit_decl , .Nm bit_ffc , .Nm bit_ffs , .Nm bit_ff_at , .Nm bit_ffc_at , .Nm bit_ffs_at , .Nm bit_ffc_area , .Nm bit_ffs_area , .Nm bit_ff_area_at , .Nm bit_ffc_area_at , .Nm bit_ffs_area_at , .Nm bit_nclear , .Nm bit_nset , .Nm bit_ntest , .Nm bit_set , .Nm bit_test , .Nm bitstr_size .Nd bit-string manipulation functions and macros .Sh SYNOPSIS .In bitstring.h .Ft bitstr_t * -.Fn bit_alloc "int nbits" +.Fn bit_alloc "size_t nbits" .Ft void -.Fn bit_decl "bitstr_t *name" "int nbits" +.Fn bit_decl "bitstr_t *name" "size_t nbits" .Ft void -.Fn bit_clear "bitstr_t *name" "int bit" +.Fn bit_clear "bitstr_t *name" "size_t bit" .Ft void -.Fn bit_count "bitstr_t *name" "int count" "int nbits" "int *value" +.Fn bit_count "bitstr_t *name" "size_t count" "size_t nbits" "ssize_t *value" .Ft void -.Fn bit_ffc "bitstr_t *name" "int nbits" "int *value" +.Fn bit_ffc "bitstr_t *name" "size_t nbits" "ssize_t *value" .Ft void -.Fn bit_ffs "bitstr_t *name" "int nbits" "int *value" +.Fn bit_ffs "bitstr_t *name" "size_t nbits" "ssize_t *value" .Ft void -.Fn bit_ffc_at "bitstr_t *name" "int start" "int nbits" "int *value" +.Fn bit_ffc_at "bitstr_t *name" "size_t start" "size_t nbits" "ssize_t *value" .Ft void -.Fn bit_ffs_at "bitstr_t *name" "int start" "int nbits" "int *value" +.Fn bit_ffs_at "bitstr_t *name" "size_t start" "size_t nbits" "ssize_t *value" .Ft void -.Fn bit_ff_at "bitstr_t *name" "int start" "int nbits" "int match" "int *value" +.Fn bit_ff_at "bitstr_t *name" "size_t start" "size_t nbits" "int match" "ssize_t *value" .Ft void -.Fn bit_ffc_area "bitstr_t *name" "int nbits" "int size" "int *value" +.Fn bit_ffc_area "bitstr_t *name" "size_t nbits" "size_t size" "ssize_t *value" .Ft void -.Fn bit_ffs_area "bitstr_t *name" "int nbits" "int size" "int *value" +.Fn bit_ffs_area "bitstr_t *name" "size_t nbits" "size_t size" "ssize_t *value" .Ft void -.Fn bit_ffc_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int *value" +.Fn bit_ffc_area_at "bitstr_t *name" "size_t start" "size_t nbits" "size_t size" "ssize_t *value" .Ft void -.Fn bit_ffs_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int *value" +.Fn bit_ffs_area_at "bitstr_t *name" "size_t start" "size_t nbits" "size_t size" "ssize_t *value" .Ft void -.Fn bit_ff_area_at "bitstr_t *name" "int start" "int nbits" "int size" "int match" "int *value" -.Fn bit_foreach "bitstr_t *name" "int nbits" "int var" -.Fn bit_foreach_at "bitstr_t *name" "int start" "int nbits" "int var" -.Fn bit_foreach_unset "bitstr_t *name" "int nbits" "int var" -.Fn bit_foreach_unset_at "bitstr_t *name" "int start" "int nbits" "int var" +.Fn bit_ff_area_at "bitstr_t *name" "size_t start" "size_t nbits" "size_t size" "int match" "ssize_t *value" +.Fn bit_foreach "bitstr_t *name" "size_t nbits" "size_t var" +.Fn bit_foreach_at "bitstr_t *name" "size_t start" "size_t nbits" "size_t var" +.Fn bit_foreach_unset "bitstr_t *name" "size_t nbits" "size_t var" +.Fn bit_foreach_unset_at "bitstr_t *name" "size_t start" "size_t nbits" "size_t var" .Ft void -.Fn bit_nclear "bitstr_t *name" "int start" "int stop" +.Fn bit_nclear "bitstr_t *name" "size_t start" "size_t stop" .Ft void -.Fn bit_nset "bitstr_t *name" "int start" "int stop" +.Fn bit_nset "bitstr_t *name" "size_t start" "size_t stop" .Ft int -.Fn bit_ntest "bitstr_t *name" "int start" "int stop" "int match" +.Fn bit_ntest "bitstr_t *name" "size_t start" "size_t stop" "int match" .Ft void -.Fn bit_set "bitstr_t *name" "int bit" +.Fn bit_set "bitstr_t *name" "size_t bit" .Ft int -.Fn bitstr_size "int nbits" +.Fn bitstr_size "size_t nbits" .Ft int -.Fn bit_test "bitstr_t *name" "int bit" +.Fn bit_test "bitstr_t *name" "size_t bit" .Sh DESCRIPTION These macros operate on strings of bits. .Pp The function .Fn bit_alloc returns a pointer of type .Dq Fa "bitstr_t *" to sufficient space to store .Fa nbits bits, or .Dv NULL if no space is available. If successful, the returned bit string is initialized with all bits cleared. .Pp The macro .Fn bit_decl declares a bit string with sufficient space to store .Fa nbits bits. .Fn bit_decl may be used to include statically sized bit strings in structure definitions or to create bit strings on the stack. Users of this macro are responsible for initialization of the bit string, typically via a global initialization of the containing struct or use of the .Fn bit_nset or .Fn bin_nclear functions. .Pp The macro .Fn bitstr_size returns the number of bytes necessary to store .Fa nbits bits. This is useful for copying bit strings. .Pp The functions .Fn bit_clear and .Fn bit_set clear or set the zero-based numbered bit .Fa bit , in the bit string .Ar name . .Pp The .Fn bit_nset and .Fn bit_nclear functions set or clear the zero-based numbered bits from .Fa start through .Fa stop in the bit string .Ar name . .Pp The .Fn bit_test function evaluates to non-zero if the zero-based numbered bit .Fa bit of bit string .Fa name is set, and zero otherwise. .Pp The .Fn bit_ntest function evaluates to non-zero if the zero-based numbered bits from .Fa start through .Fa stop in the bit string .Ar name all have the value .Ar match . .Pp The function .Fn bit_ffc stores in the location referenced by .Fa value the zero-based number of the first bit not set in the array of .Fa nbits bits referenced by .Fa name . If all bits are set, the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffs function stores in the location referenced by .Fa value the zero-based number of the first bit set in the array of .Fa nbits bits referenced by .Fa name . If no bits are set, the location referenced by .Fa value is set to \-1. .Pp The function .Fn bit_ffc_at stores in the location referenced by .Fa value the zero-based number of the first bit not set in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start . If all bits at or after .Fa start are set, the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffs_at function stores in the location referenced by .Fa value the zero-based number of the first bit set in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start . If no bits are set after .Fa start , the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ff_at function stores in the location referenced by .Fa value the zero-based number of the first bit in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start that has value .Fa match . If no bits after .Fa start match that value, the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffc_area function stores in the location referenced by .Fa value the zero-based number of the first bit beginning a sequence of unset bits of at least .Fa size unset bits in the array of .Fa nbits bits referenced by .Fa name . If no sequence of contiguous unset bits of the specified .Fa size can be found, the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffs_area function stores in the location referenced by .Fa value the zero-based number of the first bit beginning a sequence of set bits of at least .Fa size set bits in the array of .Fa nbits bits referenced by .Fa name . If no sequence of contiguous set bits of the specified .Fa size can be found, the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffc_area_at function stores in the location referenced by .Fa value the zero-based number of the first bit beginning a sequence of unset bits of at least .Fa size unset bits in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start . If no sequence of contiguous unset bits of the specified .Fa size can be found at or after .Fa start , the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ffs_area_at function stores in the location referenced by .Fa value the zero-based number of the first bit beginning a sequence of set bits of at least .Fa size set bits in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start . If no sequence of contiguous set bits of the specified .Fa size can be found at or after .Fa start , the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_ff_area_at function stores in the location referenced by .Fa value the zero-based number of the first bit beginning a sequence of bits of at least .Fa size bits in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start in which all bits have the value .Fa match . If no sequence of contiguous such bits of the specified .Fa size can be found at or after .Fa start , the location referenced by .Fa value is set to \-1. .Pp The .Fn bit_count function stores in the location referenced by .Fa value the number of bits set in the array of .Fa nbits bits referenced by .Fa name , at or after the zero-based bit index .Fa start . .Pp The macro .Fn bit_foreach traverses all set bits in the array of .Fa nbits referenced by .Fa name in the forward direction, assigning each location in turn to .Fa var . .Pp The macro .Fn bit_foreach_at traverses all set bits in the array of .Fa nbits referenced by .Fa name in the forward direction at or after the zero-based bit index .Fa start , assigning each location in turn to .Fa var . .Pp The macro .Fn bit_foreach_unset traverses all unset bits in the array of .Fa nbits referenced by .Fa name in the forward direction, assigning each location in turn to .Fa var . .Pp The macro .Fn bit_foreach_unset_at traverses all unset bits in the array of .Fa nbits referenced by .Fa name in the forward direction at or after the zero-based bit index .Fa start , assigning each location in turn to .Fa var . .Pp The arguments in bit string macros are evaluated only once and may safely have side effects. .Sh EXAMPLES .Bd -literal -offset indent #include #include \&... #define LPR_BUSY_BIT 0 #define LPR_FORMAT_BIT 1 #define LPR_DOWNLOAD_BIT 2 \&... #define LPR_AVAILABLE_BIT 9 #define LPR_MAX_BITS 10 make_lpr_available() { bitstr_t bit_decl(bitlist, LPR_MAX_BITS); ... bit_nclear(bitlist, 0, LPR_MAX_BITS - 1); ... if (!bit_test(bitlist, LPR_BUSY_BIT)) { bit_clear(bitlist, LPR_FORMAT_BIT); bit_clear(bitlist, LPR_DOWNLOAD_BIT); bit_set(bitlist, LPR_AVAILABLE_BIT); } } .Ed .Sh SEE ALSO .Xr malloc 3 , .Xr bitset 9 .Sh HISTORY The .Nm bitstring functions first appeared in .Bx 4.4 . diff --git a/sys/sys/bitstring.h b/sys/sys/bitstring.h index 127de2ebc419..8a976962dd36 100644 --- a/sys/sys/bitstring.h +++ b/sys/sys/bitstring.h @@ -1,418 +1,394 @@ /*- * SPDX-License-Identifier: BSD-3-Clause * * Copyright (c) 1989, 1993 * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Paul Vixie. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * Copyright (c) 2014 Spectra Logic Corporation * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. */ #ifndef _SYS_BITSTRING_H_ #define _SYS_BITSTRING_H_ #ifdef _KERNEL #include #include #endif #include typedef unsigned long bitstr_t; /*---------------------- Private Implementation Details ----------------------*/ #define _BITSTR_MASK (~0UL) #define _BITSTR_BITS (sizeof(bitstr_t) * 8) -#ifdef roundup2 -#define _bit_roundup2 roundup2 -#else -#define _bit_roundup2(x, y) (((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */ -#endif +/* round up x to the next multiple of y if y is a power of two */ +#define _bit_roundup2(x, y) \ + (((size_t)(x) + (y) - 1) & ~((size_t)(y) - 1)) /* bitstr_t in bit string containing the bit. */ -static inline int -_bit_idx(int _bit) +static inline size_t +_bit_idx(size_t _bit) { return (_bit / _BITSTR_BITS); } /* bit number within bitstr_t at _bit_idx(_bit). */ -static inline int -_bit_offset(int _bit) +static inline size_t +_bit_offset(size_t _bit) { return (_bit % _BITSTR_BITS); } /* Mask for the bit within its long. */ static inline bitstr_t -_bit_mask(int _bit) +_bit_mask(size_t _bit) { return (1UL << _bit_offset(_bit)); } static inline bitstr_t -_bit_make_mask(int _start, int _stop) +_bit_make_mask(size_t _start, size_t _stop) { return ((_BITSTR_MASK << _bit_offset(_start)) & (_BITSTR_MASK >> (_BITSTR_BITS - _bit_offset(_stop) - 1))); } /*----------------------------- Public Interface -----------------------------*/ /* Number of bytes allocated for a bit string of nbits bits */ -#define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8) +#define bitstr_size(_nbits) (_bit_roundup2((_nbits), _BITSTR_BITS) / 8) /* Allocate a bit string initialized with no bits set. */ #ifdef _KERNEL static inline bitstr_t * -bit_alloc(int _nbits, struct malloc_type *type, int flags) +bit_alloc(size_t _nbits, struct malloc_type *type, int flags) { return ((bitstr_t *)malloc(bitstr_size(_nbits), type, flags | M_ZERO)); } #else static inline bitstr_t * -bit_alloc(int _nbits) +bit_alloc(size_t _nbits) { return ((bitstr_t *)calloc(bitstr_size(_nbits), 1)); } #endif /* Allocate a bit string on the stack */ #define bit_decl(name, nbits) \ ((name)[bitstr_size(nbits) / sizeof(bitstr_t)]) /* Is bit N of bit string set? */ static inline int -bit_test(const bitstr_t *_bitstr, int _bit) +bit_test(const bitstr_t *_bitstr, size_t _bit) { return ((_bitstr[_bit_idx(_bit)] & _bit_mask(_bit)) != 0); } /* Set bit N of bit string. */ static inline void -bit_set(bitstr_t *_bitstr, int _bit) +bit_set(bitstr_t *_bitstr, size_t _bit) { _bitstr[_bit_idx(_bit)] |= _bit_mask(_bit); } /* clear bit N of bit string name */ static inline void -bit_clear(bitstr_t *_bitstr, int _bit) +bit_clear(bitstr_t *_bitstr, size_t _bit) { _bitstr[_bit_idx(_bit)] &= ~_bit_mask(_bit); } /* Are bits in [start ... stop] in bit string all 0 or all 1? */ static inline int -bit_ntest(const bitstr_t *_bitstr, int _start, int _stop, int _match) +bit_ntest(const bitstr_t *_bitstr, size_t _start, size_t _stop, int _match) { const bitstr_t *_stopbitstr; bitstr_t _mask; _mask = (_match == 0) ? 0 : _BITSTR_MASK; _stopbitstr = _bitstr + _bit_idx(_stop); _bitstr += _bit_idx(_start); if (_bitstr == _stopbitstr) return (0 == ((*_bitstr ^ _mask) & _bit_make_mask(_start, _stop))); if (_bit_offset(_start) != 0 && 0 != ((*_bitstr++ ^ _mask) & _bit_make_mask(_start, _BITSTR_BITS - 1))) return (0); if (_bit_offset(_stop) == _BITSTR_BITS - 1) ++_stopbitstr; while (_bitstr < _stopbitstr) { if (*_bitstr++ != _mask) return (0); } return (_bit_offset(_stop) == _BITSTR_BITS - 1 || 0 == ((*_stopbitstr ^ _mask) & _bit_make_mask(0, _stop))); } /* Set bits start ... stop inclusive in bit string. */ static inline void -bit_nset(bitstr_t *_bitstr, int _start, int _stop) +bit_nset(bitstr_t *_bitstr, size_t _start, size_t _stop) { bitstr_t *_stopbitstr; _stopbitstr = _bitstr + _bit_idx(_stop); _bitstr += _bit_idx(_start); if (_bitstr == _stopbitstr) { *_bitstr |= _bit_make_mask(_start, _stop); } else { if (_bit_offset(_start) != 0) *_bitstr++ |= _bit_make_mask(_start, _BITSTR_BITS - 1); if (_bit_offset(_stop) == _BITSTR_BITS - 1) ++_stopbitstr; while (_bitstr < _stopbitstr) *_bitstr++ = _BITSTR_MASK; if (_bit_offset(_stop) != _BITSTR_BITS - 1) *_stopbitstr |= _bit_make_mask(0, _stop); } } /* Clear bits start ... stop inclusive in bit string. */ static inline void -bit_nclear(bitstr_t *_bitstr, int _start, int _stop) +bit_nclear(bitstr_t *_bitstr, size_t _start, size_t _stop) { bitstr_t *_stopbitstr; _stopbitstr = _bitstr + _bit_idx(_stop); _bitstr += _bit_idx(_start); if (_bitstr == _stopbitstr) { *_bitstr &= ~_bit_make_mask(_start, _stop); } else { if (_bit_offset(_start) != 0) *_bitstr++ &= ~_bit_make_mask(_start, _BITSTR_BITS - 1); if (_bit_offset(_stop) == _BITSTR_BITS - 1) ++_stopbitstr; while (_bitstr < _stopbitstr) *_bitstr++ = 0; if (_bit_offset(_stop) != _BITSTR_BITS - 1) *_stopbitstr &= ~_bit_make_mask(0, _stop); } } /* Find the first '_match'-bit in bit string at or after bit start. */ -static inline void -bit_ff_at(bitstr_t *_bitstr, int _start, int _nbits, int _match, - int *_result) +static inline ssize_t +bit_ff_at_(bitstr_t *_bitstr, size_t _start, size_t _nbits, int _match) { bitstr_t *_curbitstr; bitstr_t *_stopbitstr; bitstr_t _mask; bitstr_t _test; - int _value; + ssize_t _value; - if (_start >= _nbits || _nbits <= 0) { - *_result = -1; - return; - } + if (_start >= _nbits || _nbits <= 0) + return (-1); _curbitstr = _bitstr + _bit_idx(_start); _stopbitstr = _bitstr + _bit_idx(_nbits - 1); _mask = _match ? 0 : _BITSTR_MASK; _test = _mask ^ *_curbitstr; if (_bit_offset(_start) != 0) _test &= _bit_make_mask(_start, _BITSTR_BITS - 1); while (_test == 0 && _curbitstr < _stopbitstr) _test = _mask ^ *(++_curbitstr); _value = ((_curbitstr - _bitstr) * _BITSTR_BITS) + ffsl(_test) - 1; if (_test == 0 || - (_bit_offset(_nbits) != 0 && _value >= _nbits)) + (_bit_offset(_nbits) != 0 && (size_t)_value >= _nbits)) _value = -1; - *_result = _value; + return (_value); } +#define bit_ff_at(_bitstr, _start, _nbits, _match, _resultp) \ + *(_resultp) = bit_ff_at_((_bitstr), (_start), (_nbits), (_match)) /* Find the first bit set in bit string at or after bit start. */ -static inline void -bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) -{ - bit_ff_at(_bitstr, _start, _nbits, 1, _result); -} +#define bit_ffs_at(_bitstr, _start, _nbits, _resultp) \ + *(_resultp) = bit_ff_at_((_bitstr), (_start), (_nbits), 1) /* Find the first bit clear in bit string at or after bit start. */ -static inline void -bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) -{ - bit_ff_at(_bitstr, _start, _nbits, 0, _result); -} +#define bit_ffc_at(_bitstr, _start, _nbits, _resultp) \ + *(_resultp) = bit_ff_at_((_bitstr), (_start), (_nbits), 0) /* Find the first bit set in bit string. */ -static inline void -bit_ffs(bitstr_t *_bitstr, int _nbits, int *_result) -{ - bit_ffs_at(_bitstr, /*start*/0, _nbits, _result); -} +#define bit_ffs(_bitstr, _nbits, _resultp) \ + *(_resultp) = bit_ff_at_((_bitstr), 0, (_nbits), 1) /* Find the first bit clear in bit string. */ -static inline void -bit_ffc(bitstr_t *_bitstr, int _nbits, int *_result) -{ - bit_ffc_at(_bitstr, /*start*/0, _nbits, _result); -} +#define bit_ffc(_bitstr, _nbits, _resultp) \ + *(_resultp) = bit_ff_at_((_bitstr), 0, (_nbits), 0) /* Find contiguous sequence of at least size '_match'-bits at or after start */ -static inline void -bit_ff_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size, - int _match, int *_result) +static inline ssize_t +bit_ff_area_at_(bitstr_t *_bitstr, size_t _start, size_t _nbits, size_t _size, + int _match) { bitstr_t *_curbitstr, _mask, _test; - int _value, _last, _shft, _maxshft; + size_t _last, _shft, _maxshft; + ssize_t _value; - if (_start + _size > _nbits || _nbits <= 0) { - *_result = -1; - return; - } + if (_start + _size > _nbits || _nbits <= 0) + return (-1); _mask = _match ? _BITSTR_MASK : 0; _maxshft = _bit_idx(_size - 1) == 0 ? _size : (int)_BITSTR_BITS; _value = _start; _curbitstr = _bitstr + _bit_idx(_start); _test = ~(_BITSTR_MASK << _bit_offset(_start)); for (_last = _size - 1, _test |= _mask ^ *_curbitstr; !(_bit_idx(_last) == 0 && (_test & _bit_make_mask(0, _last)) == 0); _last -= _BITSTR_BITS, _test = _mask ^ *++_curbitstr) { if (_test == 0) continue; /* Shrink-left every 0-area in _test by maxshft-1 bits. */ for (_shft = _maxshft; _shft > 1 && (_test & (_test + 1)) != 0; _shft = (_shft + 1) / 2) _test |= _test >> _shft / 2; /* Find the start of the first 0-area in _test. */ _last = ffsl(~(_test >> 1)); _value = (_curbitstr - _bitstr) * _BITSTR_BITS + _last; /* If there's insufficient space left, give up. */ if (_value + _size > _nbits) { _value = -1; break; } _last += _size - 1; /* If a solution is contained in _test, success! */ if (_bit_idx(_last) == 0) break; /* A solution here needs bits from the next word. */ } - *_result = _value; + return (_value); } +#define bit_ff_area_at(_bitstr, _start, _nbits, _size, _match, _resultp) \ + *(_resultp) = bit_ff_area_at_(_bitstr, _start, _nbits, _size, _match); /* Find contiguous sequence of at least size set bits at or after start */ -static inline void -bit_ffs_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size, - int *_result) -{ - bit_ff_area_at(_bitstr, _start, _nbits, _size, 1, _result); -} +#define bit_ffs_area_at(_bitstr, _start, _nbits, _size, _resultp) \ + *(_resultp) = bit_ff_area_at_((_bitstr), (_start), (_nbits), (_size), 1) /* Find contiguous sequence of at least size cleared bits at or after start */ -static inline void -bit_ffc_area_at(bitstr_t *_bitstr, int _start, int _nbits, int _size, - int *_result) -{ - bit_ff_area_at(_bitstr, _start, _nbits, _size, 0, _result); -} +#define bit_ffc_area_at(_bitstr, _start, _nbits, _size, _resultp) \ + *(_resultp) = bit_ff_area_at_((_bitstr), (_start), (_nbits), (_size), 0) /* Find contiguous sequence of at least size set bits in bit string */ -static inline void -bit_ffs_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result) -{ - bit_ffs_area_at(_bitstr, /*start*/0, _nbits, _size, _result); -} +#define bit_ffs_area(_bitstr, _nbits, _size, _resultp) \ + *(_resultp) = bit_ff_area_at_((_bitstr), 0, (_nbits), (_size), 1) /* Find contiguous sequence of at least size cleared bits in bit string */ -static inline void -bit_ffc_area(bitstr_t *_bitstr, int _nbits, int _size, int *_result) -{ - bit_ffc_area_at(_bitstr, /*start*/0, _nbits, _size, _result); -} +#define bit_ffc_area(_bitstr, _nbits, _size, _resultp) \ + *(_resultp) = bit_ff_area_at_((_bitstr), 0, (_nbits), (_size), 0) /* Count the number of bits set in a bitstr of size _nbits at or after _start */ -static inline void -bit_count(bitstr_t *_bitstr, int _start, int _nbits, int *_result) +static inline ssize_t +bit_count_(bitstr_t *_bitstr, size_t _start, size_t _nbits) { bitstr_t *_curbitstr, mask; - int _value = 0, curbitstr_len; + size_t curbitstr_len; + ssize_t _value = 0; if (_start >= _nbits) - goto out; + return (0); _curbitstr = _bitstr + _bit_idx(_start); _nbits -= _BITSTR_BITS * _bit_idx(_start); _start -= _BITSTR_BITS * _bit_idx(_start); if (_start > 0) { curbitstr_len = (int)_BITSTR_BITS < _nbits ? (int)_BITSTR_BITS : _nbits; mask = _bit_make_mask(_start, _bit_offset(curbitstr_len - 1)); _value += __bitcountl(*_curbitstr & mask); _curbitstr++; + if (_nbits < _BITSTR_BITS) + return (_value); _nbits -= _BITSTR_BITS; } while (_nbits >= (int)_BITSTR_BITS) { _value += __bitcountl(*_curbitstr); _curbitstr++; _nbits -= _BITSTR_BITS; } if (_nbits > 0) { mask = _bit_make_mask(0, _bit_offset(_nbits - 1)); _value += __bitcountl(*_curbitstr & mask); } -out: - *_result = _value; + return (_value); } +#define bit_count(_bitstr, _start, _nbits, _resultp) \ + *(_resultp) = bit_count_((_bitstr), (_start), (_nbits)) /* Traverse all set bits, assigning each location in turn to iter */ #define bit_foreach_at(_bitstr, _start, _nbits, _iter) \ - for (bit_ffs_at((_bitstr), (_start), (_nbits), &(_iter)); \ + for ((_iter) = bit_ff_at_((_bitstr), (_start), (_nbits), 1); \ (_iter) != -1; \ - bit_ffs_at((_bitstr), (_iter) + 1, (_nbits), &(_iter))) + (_iter) = bit_ff_at_((_bitstr), (_iter) + 1, (_nbits), 1)) #define bit_foreach(_bitstr, _nbits, _iter) \ bit_foreach_at(_bitstr, /*start*/0, _nbits, _iter) /* Traverse all unset bits, assigning each location in turn to iter */ #define bit_foreach_unset_at(_bitstr, _start, _nbits, _iter) \ - for (bit_ffc_at((_bitstr), (_start), (_nbits), &(_iter)); \ + for ((_iter) = bit_ff_at_((_bitstr), (_start), (_nbits), 0); \ (_iter) != -1; \ - bit_ffc_at((_bitstr), (_iter) + 1, (_nbits), &(_iter))) + (_iter) = bit_ff_at_((_bitstr), (_iter) + 1, (_nbits), 0)) #define bit_foreach_unset(_bitstr, _nbits, _iter) \ bit_foreach_unset_at(_bitstr, /*start*/0, _nbits, _iter) #endif /* _SYS_BITSTRING_H_ */ diff --git a/tests/sys/sys/bitstring_test.c b/tests/sys/sys/bitstring_test.c index 0c214b9e67a7..a48042a4a063 100644 --- a/tests/sys/sys/bitstring_test.c +++ b/tests/sys/sys/bitstring_test.c @@ -1,889 +1,977 @@ /*- * Copyright (c) 2014 Spectra Logic Corporation + * Copyright (c) 2023 Klara, Inc. * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions, and the following disclaimer, * without modification. * 2. Redistributions in binary form must reproduce at minimum a disclaimer * substantially similar to the "NO WARRANTY" disclaimer below * ("Disclaimer") and any redistribution must be conditioned upon * including a substantially similar Disclaimer requirement for further * binary redistribution. * * NO WARRANTY * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGES. */ #include #include -#include +#include #include typedef void (testfunc_t)(bitstr_t *bstr, int nbits, const char *memloc); static void bitstring_run_stack_test(testfunc_t *test, int nbits) { bitstr_t bit_decl(bitstr, nbits); test(bitstr, nbits, "stack"); } static void bitstring_run_heap_test(testfunc_t *test, int nbits) { bitstr_t *bitstr = bit_alloc(nbits); test(bitstr, nbits, "heap"); + free(bitstr); } static void bitstring_test_runner(testfunc_t *test) { const int bitstr_sizes[] = { 0, 1, _BITSTR_BITS - 1, _BITSTR_BITS, _BITSTR_BITS + 1, 2 * _BITSTR_BITS - 1, 2 * _BITSTR_BITS, 1023, 1024 }; for (unsigned long i = 0; i < nitems(bitstr_sizes); i++) { bitstring_run_stack_test(test, bitstr_sizes[i]); bitstring_run_heap_test(test, bitstr_sizes[i]); } } #define BITSTRING_TC_DEFINE(name) \ ATF_TC_WITHOUT_HEAD(name); \ static testfunc_t name ## _test; \ \ ATF_TC_BODY(name, tc) \ { \ bitstring_test_runner(name ## _test); \ } \ \ static void \ name ## _test(bitstr_t *bitstr, int nbits, const char *memloc) #define BITSTRING_TC_ADD(tp, name) \ do { \ ATF_TP_ADD_TC(tp, name); \ } while (0) ATF_TC_WITHOUT_HEAD(bitstr_in_struct); ATF_TC_BODY(bitstr_in_struct, tc) { struct bitstr_containing_struct { bitstr_t bit_decl(bitstr, 8); } test_struct; bit_nclear(test_struct.bitstr, 0, 8); } ATF_TC_WITHOUT_HEAD(bitstr_size); ATF_TC_BODY(bitstr_size, tc) { size_t sob = sizeof(bitstr_t); ATF_CHECK_EQ(0, bitstr_size(0)); ATF_CHECK_EQ(sob, bitstr_size(1)); ATF_CHECK_EQ(sob, bitstr_size(sob * 8)); ATF_CHECK_EQ(2 * sob, bitstr_size(sob * 8 + 1)); } BITSTRING_TC_DEFINE(bit_set) /* bitstr_t *bitstr, int nbits, const char *memloc */ { memset(bitstr, 0, bitstr_size(nbits)); for (int i = 0; i < nbits; i++) { bit_set(bitstr, i); for (int j = 0; j < nbits; j++) { ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 1 : 0, "bit_set_%d_%s: Failed on bit %d", nbits, memloc, i); } bit_clear(bitstr, i); } } BITSTRING_TC_DEFINE(bit_clear) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, j; memset(bitstr, 0xFF, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_clear(bitstr, i); for (j = 0; j < nbits; j++) { ATF_REQUIRE_MSG(bit_test(bitstr, j) == (j == i) ? 0 : 1, "bit_clear_%d_%s: Failed on bit %d", nbits, memloc, i); } bit_set(bitstr, i); } } BITSTRING_TC_DEFINE(bit_ffs) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i; int found_set_bit; memset(bitstr, 0, bitstr_size(nbits)); bit_ffs(bitstr, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_%d_%s: Failed all clear bits.", nbits, memloc); for (i = 0; i < nbits; i++) { memset(bitstr, 0xFF, bitstr_size(nbits)); if (i > 0) bit_nclear(bitstr, 0, i - 1); bit_ffs(bitstr, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == i, "bit_ffs_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } } BITSTRING_TC_DEFINE(bit_ffc) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i; int found_clear_bit; memset(bitstr, 0xFF, bitstr_size(nbits)); bit_ffc(bitstr, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_%d_%s: Failed all set bits.", nbits, memloc); for (i = 0; i < nbits; i++) { memset(bitstr, 0, bitstr_size(nbits)); if (i > 0) bit_nset(bitstr, 0, i - 1); bit_ffc(bitstr, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == i, "bit_ffc_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } } BITSTRING_TC_DEFINE(bit_ffs_at) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i; int found_set_bit; memset(bitstr, 0xFF, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffs_at(bitstr, i, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == i, "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } memset(bitstr, 0, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffs_at(bitstr, i, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } memset(bitstr, 0x55, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffs_at(bitstr, i, nbits, &found_set_bit); if (i == nbits - 1 && (nbits & 1) == 0) { ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } else { ATF_REQUIRE_MSG(found_set_bit == i + (i & 1), "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } } memset(bitstr, 0xAA, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffs_at(bitstr, i, nbits, &found_set_bit); if (i == nbits - 1 && (nbits & 1) != 0) { ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } else { ATF_REQUIRE_MSG( found_set_bit == i + ((i & 1) ? 0 : 1), "bit_ffs_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_set_bit); } } /* Pass a start value beyond the size of the bit string */ bit_ffs_at(bitstr, nbits, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d", nbits, memloc, nbits, found_set_bit); bit_ffs_at(bitstr, nbits + 3, nbits, &found_set_bit); ATF_REQUIRE_MSG(found_set_bit == -1, "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d", nbits, memloc, nbits + 3, found_set_bit); } BITSTRING_TC_DEFINE(bit_ffc_at) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, found_clear_bit; memset(bitstr, 0, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffc_at(bitstr, i, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == i, "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } memset(bitstr, 0xFF, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffc_at(bitstr, i, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } memset(bitstr, 0x55, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffc_at(bitstr, i, nbits, &found_clear_bit); if (i == nbits - 1 && (nbits & 1) != 0) { ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } else { ATF_REQUIRE_MSG( found_clear_bit == i + ((i & 1) ? 0 : 1), "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } } memset(bitstr, 0xAA, bitstr_size(nbits)); for (i = 0; i < nbits; i++) { bit_ffc_at(bitstr, i, nbits, &found_clear_bit); if (i == nbits - 1 && (nbits & 1) == 0) { ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } else { ATF_REQUIRE_MSG(found_clear_bit == i + (i & 1), "bit_ffc_at_%d_%s: Failed on bit %d, Result %d", nbits, memloc, i, found_clear_bit); } } /* Pass a start value beyond the size of the bit string */ bit_ffc_at(bitstr, nbits, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_at_%d_%s: Failed with high start value, Result %d", nbits, memloc, found_clear_bit); bit_ffc_at(bitstr, nbits + 3, nbits, &found_clear_bit); ATF_REQUIRE_MSG(found_clear_bit == -1, "bit_ffc_at_%d_%s: Failed with high start value of %d, Result %d", nbits, memloc, nbits + 3, found_clear_bit); } BITSTRING_TC_DEFINE(bit_ffc_area_at_all_or_nothing) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int found; memset(bitstr, 0, bitstr_size(nbits)); if (nbits % _BITSTR_BITS != 0) bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1); for (int start = 0; start < nbits; start++) { for (int size = 1; size < nbits - start; size++) { bit_ffc_area_at(bitstr, start, nbits, size, &found); ATF_REQUIRE_EQ_MSG(start, found, "bit_ffc_area_at_%d_%s: " "Did not find %d clear bits at %d", nbits, memloc, size, start); } } memset(bitstr, 0xff, bitstr_size(nbits)); if (nbits % _BITSTR_BITS != 0) bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1); for (int start = 0; start < nbits; start++) { for (int size = 1; size < nbits - start; size++) { bit_ffc_area_at(bitstr, start, nbits, size, &found); ATF_REQUIRE_EQ_MSG(-1, found, "bit_ffc_area_at_%d_%s: " "Found %d clear bits at %d", nbits, memloc, size, start); } } } BITSTRING_TC_DEFINE(bit_ffs_area_at_all_or_nothing) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int found; memset(bitstr, 0, bitstr_size(nbits)); if (nbits % _BITSTR_BITS != 0) bit_nset(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1); for (int start = 0; start < nbits; start++) { for (int size = 1; size < nbits - start; size++) { bit_ffs_area_at(bitstr, start, nbits, size, &found); ATF_REQUIRE_EQ_MSG(-1, found, "bit_ffs_area_at_%d_%s: " "Found %d set bits at %d", nbits, memloc, size, start); } } memset(bitstr, 0xff, bitstr_size(nbits)); if (nbits % _BITSTR_BITS != 0) bit_nclear(bitstr, nbits, roundup2(nbits, _BITSTR_BITS) - 1); for (int start = 0; start < nbits; start++) { for (int size = 1; size < nbits - start; size++) { bit_ffs_area_at(bitstr, start, nbits, size, &found); ATF_REQUIRE_EQ_MSG(start, found, "bit_ffs_area_at_%d_%s: " "Did not find %d set bits at %d", nbits, memloc, size, start); } } } ATF_TC_WITHOUT_HEAD(bit_ffs_area); ATF_TC_BODY(bit_ffs_area, tc) { const int nbits = 72; bitstr_t bit_decl(bitstr, nbits); int location; memset(bitstr, 0, bitstr_size(nbits)); bit_nset(bitstr, 5, 6); location = 0; bit_ffs_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffs_area: found location of size 3 when only 2 bits are set"); ATF_REQUIRE_EQ_MSG(0, bit_ntest(bitstr, 5, 7, 1), "bit_ntest: found location of size 3 when only 2 bits are set"); bit_set(bitstr, 7); location = 0; bit_ffs_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(5, location, "bit_ffs_area: failed to find location of size 3 %d", location); ATF_REQUIRE_EQ_MSG(1, bit_ntest(bitstr, 5, 7, 1), "bit_ntest: failed to find all 3 bits set"); bit_set(bitstr, 8); location = 0; bit_ffs_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(5, location, "bit_ffs_area: failed to find location of size 3"); location = 0; bit_ffs_area_at(bitstr, 2, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(5, location, "bit_ffs_area_at: failed to find location of size 3"); location = 0; bit_ffs_area_at(bitstr, 6, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(6, location, "bit_ffs_area_at: failed to find location of size 3"); location = 0; bit_ffs_area_at(bitstr, 8, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffs_area_at: found invalid location"); bit_nset(bitstr, 69, 71); location = 0; bit_ffs_area_at(bitstr, 8, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(69, location, "bit_ffs_area_at: failed to find location of size 3"); location = 0; bit_ffs_area_at(bitstr, 69, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(69, location, "bit_ffs_area_at: failed to find location of size 3"); location = 0; bit_ffs_area_at(bitstr, 70, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffs_area_at: found invalid location"); location = 0; bit_ffs_area_at(bitstr, 72, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffs_area_at: found invalid location"); bit_nset(bitstr, 59, 67); location = 0; bit_ffs_area(bitstr, nbits, 9, &location); ATF_REQUIRE_EQ_MSG(59, location, "bit_ffs_area: failed to find location of size 9"); location = 0; bit_ffs_area(bitstr, nbits, 10, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffs_area: found invalid location"); } ATF_TC_WITHOUT_HEAD(bit_ffc_area); ATF_TC_BODY(bit_ffc_area, tc) { const int nbits = 80; bitstr_t bit_decl(bitstr, nbits); int location; /* set all bits */ memset(bitstr, 0xFF, bitstr_size(nbits)); bit_clear(bitstr, 7); bit_clear(bitstr, 8); location = 0; bit_ffc_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffc_area: found location of size 3 when only 2 bits are set"); bit_clear(bitstr, 9); location = 0; bit_ffc_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(7, location, "bit_ffc_area: failed to find location of size 3"); bit_clear(bitstr, 10); location = 0; bit_ffc_area(bitstr, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(7, location, "bit_ffc_area: failed to find location of size 3"); location = 0; bit_ffc_area_at(bitstr, 2, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(7, location, "bit_ffc_area_at: failed to find location of size 3"); location = 0; bit_ffc_area_at(bitstr, 8, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(8, location, "bit_ffc_area_at: failed to find location of size 3"); location = 0; bit_ffc_area_at(bitstr, 9, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffc_area_at: found invalid bit location"); bit_clear(bitstr, 77); bit_clear(bitstr, 78); bit_clear(bitstr, 79); location = 0; bit_ffc_area_at(bitstr, 12, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(77, location, "bit_ffc_area_at: failed to find location of size 3"); location = 0; bit_ffc_area_at(bitstr, 77, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(77, location, "bit_ffc_area_at: failed to find location of size 3"); location = 0; bit_ffc_area_at(bitstr, 78, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffc_area_at: found invalid location"); location = 0; bit_ffc_area_at(bitstr, 85, nbits, 3, &location); ATF_REQUIRE_EQ_MSG(-1, location, "bit_ffc_area_at: found invalid location"); } BITSTRING_TC_DEFINE(bit_nclear) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, j; int found_set_bit; int found_clear_bit; for (i = 0; i < nbits; i++) { for (j = i; j < nbits; j++) { memset(bitstr, 0xFF, bitstr_size(nbits)); bit_nclear(bitstr, i, j); bit_ffc(bitstr, nbits, &found_clear_bit); ATF_REQUIRE_MSG( found_clear_bit == i, "bit_nclear_%d_%d_%d%s: Failed with result %d", nbits, i, j, memloc, found_clear_bit); bit_ffs_at(bitstr, i, nbits, &found_set_bit); ATF_REQUIRE_MSG( (j + 1 < nbits) ? found_set_bit == j + 1 : -1, "bit_nset_%d_%d_%d%s: Failed with result %d", nbits, i, j, memloc, found_set_bit); } } } BITSTRING_TC_DEFINE(bit_nset) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, j; int found_set_bit; int found_clear_bit; for (i = 0; i < nbits; i++) { for (j = i; j < nbits; j++) { memset(bitstr, 0, bitstr_size(nbits)); bit_nset(bitstr, i, j); bit_ffs(bitstr, nbits, &found_set_bit); ATF_REQUIRE_MSG( found_set_bit == i, "bit_nset_%d_%d_%d%s: Failed with result %d", nbits, i, j, memloc, found_set_bit); bit_ffc_at(bitstr, i, nbits, &found_clear_bit); ATF_REQUIRE_MSG( (j + 1 < nbits) ? found_clear_bit == j + 1 : -1, "bit_nset_%d_%d_%d%s: Failed with result %d", nbits, i, j, memloc, found_clear_bit); } } } BITSTRING_TC_DEFINE(bit_count) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int result, s, e, expected; /* Empty bitstr */ memset(bitstr, 0, bitstr_size(nbits)); bit_count(bitstr, 0, nbits, &result); ATF_CHECK_MSG(0 == result, "bit_count_%d_%s_%s: Failed with result %d", nbits, "clear", memloc, result); /* Full bitstr */ memset(bitstr, 0xFF, bitstr_size(nbits)); bit_count(bitstr, 0, nbits, &result); ATF_CHECK_MSG(nbits == result, "bit_count_%d_%s_%s: Failed with result %d", nbits, "set", memloc, result); /* Invalid _start value */ memset(bitstr, 0xFF, bitstr_size(nbits)); bit_count(bitstr, nbits, nbits, &result); ATF_CHECK_MSG(0 == result, "bit_count_%d_%s_%s: Failed with result %d", nbits, "invalid_start", memloc, result); /* Alternating bitstr, starts with 0 */ memset(bitstr, 0xAA, bitstr_size(nbits)); bit_count(bitstr, 0, nbits, &result); ATF_CHECK_MSG(nbits / 2 == result, "bit_count_%d_%s_%d_%s: Failed with result %d", nbits, "alternating", 0, memloc, result); /* Alternating bitstr, starts with 1 */ memset(bitstr, 0x55, bitstr_size(nbits)); bit_count(bitstr, 0, nbits, &result); ATF_CHECK_MSG((nbits + 1) / 2 == result, "bit_count_%d_%s_%d_%s: Failed with result %d", nbits, "alternating", 1, memloc, result); /* Varying start location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (s = 0; s < nbits; s++) { expected = s % 2 == 0 ? (nbits - s) / 2 : (nbits - s + 1) / 2; bit_count(bitstr, s, nbits, &result); ATF_CHECK_MSG(expected == result, "bit_count_%d_%s_%d_%s: Failed with result %d", nbits, "vary_start", s, memloc, result); } /* Varying end location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (e = 0; e < nbits; e++) { bit_count(bitstr, 0, e, &result); ATF_CHECK_MSG(e / 2 == result, "bit_count_%d_%s_%d_%s: Failed with result %d", nbits, "vary_end", e, memloc, result); } } BITSTRING_TC_DEFINE(bit_foreach) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, set_bit; /* Empty bitstr */ memset(bitstr, 0x00, bitstr_size(nbits)); bit_foreach (bitstr, nbits, set_bit) { atf_tc_fail("bit_foreach_%d_%s_%s: Failed at location %d", nbits, "clear", memloc, set_bit); } /* Full bitstr */ i = 0; memset(bitstr, 0xFF, bitstr_size(nbits)); bit_foreach(bitstr, nbits, set_bit) { ATF_REQUIRE_MSG(set_bit == i, "bit_foreach_%d_%s_%s: Failed on turn %d at location %d", nbits, "set", memloc, i, set_bit); i++; } ATF_REQUIRE_MSG(i == nbits, "bit_foreach_%d_%s_%s: Invalid number of turns %d", nbits, "set", memloc, i); /* Alternating bitstr, starts with 0 */ i = 0; memset(bitstr, 0xAA, bitstr_size(nbits)); bit_foreach(bitstr, nbits, set_bit) { ATF_REQUIRE_MSG(set_bit == i * 2 + 1, "bit_foreach_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "alternating", 0, memloc, i, set_bit); i++; } ATF_REQUIRE_MSG(i == nbits / 2, "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d", nbits, "alternating", 0, memloc, i); /* Alternating bitstr, starts with 1 */ i = 0; memset(bitstr, 0x55, bitstr_size(nbits)); bit_foreach(bitstr, nbits, set_bit) { ATF_REQUIRE_MSG(set_bit == i * 2, "bit_foreach_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "alternating", 1, memloc, i, set_bit); i++; } ATF_REQUIRE_MSG(i == (nbits + 1) / 2, "bit_foreach_%d_%s_%d_%s: Invalid number of turns %d", nbits, "alternating", 1, memloc, i); } BITSTRING_TC_DEFINE(bit_foreach_at) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, s, e, set_bit; /* Invalid _start value */ memset(bitstr, 0xFF, bitstr_size(nbits)); bit_foreach_at(bitstr, nbits, nbits, set_bit) { atf_tc_fail("bit_foreach_at_%d_%s_%s: Failed at location %d", nbits, "invalid_start", memloc, set_bit); } /* Varying start location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (s = 0; s < nbits; s++) { i = 0; bit_foreach_at(bitstr, s, nbits, set_bit) { ATF_REQUIRE_MSG(set_bit == (i + s / 2) * 2 + 1, "bit_foreach_at_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "vary_start", s, memloc, i, set_bit); i++; } ATF_REQUIRE_MSG(i == nbits / 2 - s / 2, "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d", nbits, "vary_start", s, memloc, i); } /* Varying end location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (e = 0; e < nbits; e++) { i = 0; bit_foreach_at(bitstr, 0, e, set_bit) { ATF_REQUIRE_MSG(set_bit == i * 2 + 1, "bit_foreach_at_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "vary_end", e, memloc, i, set_bit); i++; } ATF_REQUIRE_MSG(i == e / 2, "bit_foreach_at_%d_%s_%d_%s: Invalid number of turns %d", nbits, "vary_end", e, memloc, i); } } BITSTRING_TC_DEFINE(bit_foreach_unset) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, unset_bit; /* Empty bitstr */ i = 0; memset(bitstr, 0, bitstr_size(nbits)); bit_foreach_unset(bitstr, nbits, unset_bit) { ATF_REQUIRE_MSG(unset_bit == i, "bit_foreach_unset_%d_%s_%s: " "Failed on turn %d at location %d", nbits, "clear", memloc, i, unset_bit); i++; } ATF_REQUIRE_MSG(i == nbits, "bit_foreach_unset_%d_%s_%s: Invalid number of turns %d", nbits, "set", memloc, i); /* Full bitstr */ memset(bitstr, 0xFF, bitstr_size(nbits)); bit_foreach_unset(bitstr, nbits, unset_bit) { atf_tc_fail("bit_foreach_unset_%d_%s_%s: " "Failed at location %d", nbits, "set", memloc, unset_bit); } /* Alternating bitstr, starts with 0 */ i = 0; memset(bitstr, 0xAA, bitstr_size(nbits)); bit_foreach_unset(bitstr, nbits, unset_bit) { ATF_REQUIRE_MSG(unset_bit == i * 2, "bit_foreach_unset_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "alternating", 0, memloc, i, unset_bit); i++; } ATF_REQUIRE_MSG(i == (nbits + 1) / 2, "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d", nbits, "alternating", 0, memloc, i); /* Alternating bitstr, starts with 1 */ i = 0; memset(bitstr, 0x55, bitstr_size(nbits)); bit_foreach_unset(bitstr, nbits, unset_bit) { ATF_REQUIRE_MSG(unset_bit == i * 2 + 1, "bit_foreach_unset_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "alternating", 1, memloc, i, unset_bit); i++; } ATF_REQUIRE_MSG(i == nbits / 2, "bit_foreach_unset_%d_%s_%d_%s: Invalid number of turns %d", nbits, "alternating", 1, memloc, i); } BITSTRING_TC_DEFINE(bit_foreach_unset_at) /* bitstr_t *bitstr, int nbits, const char *memloc */ { int i, s, e, unset_bit; /* Invalid _start value */ memset(bitstr, 0, bitstr_size(nbits)); bit_foreach_unset_at(bitstr, nbits, nbits, unset_bit) { atf_tc_fail("bit_foreach_unset_at_%d_%s_%s: " "Failed at location %d", nbits, "invalid_start", memloc, unset_bit); } /* Varying start location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (s = 0; s < nbits; s++) { i = 0; bit_foreach_unset_at(bitstr, s, nbits, unset_bit) { ATF_REQUIRE_MSG(unset_bit == (i + (s + 1) / 2) * 2, "bit_foreach_unset_at_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "vary_start", s, memloc, i, unset_bit); i++; } ATF_REQUIRE_MSG(i == (nbits + 1) / 2 - (s + 1) / 2, "bit_foreach_unset_at_%d_%s_%d_%s: " "Invalid number of turns %d", nbits, "vary_start", s, memloc, i); } /* Varying end location */ memset(bitstr, 0xAA, bitstr_size(nbits)); for (e = 0; e < nbits; e++) { i = 0; bit_foreach_unset_at(bitstr, 0, e, unset_bit) { ATF_REQUIRE_MSG(unset_bit == i * 2, "bit_foreach_unset_at_%d_%s_%d_%s: " "Failed on turn %d at location %d", nbits, "vary_end", e, memloc, i, unset_bit); i++; } ATF_REQUIRE_MSG(i == (e + 1) / 2, "bit_foreach_unset_at_%d_%s_%d_%s: " "Invalid number of turns %d", nbits, "vary_end", e, memloc, i); } } +/* + * Perform various tests on large bit strings. We can't simply add larger + * sizes to bitstring_runner as most of the existing tests are exhaustive + * and would take forever to run for large values of nbits. + * + * On 32-bit platforms, we use nbits = SSIZE_MAX (2147483647) bits, which + * is the largest we can hope to support; on 64-bit platforms, we use + * nbits = INT_MAX + 30 (2147483677), which is small enough to be + * practicable yet large enough to reveal arithmetic overflow bugs. + */ +ATF_TC_WITHOUT_HEAD(bitstr_large); +ATF_TC_BODY(bitstr_large, tc) +{ + size_t nbits = INT_MAX < SSIZE_MAX ? (size_t)INT_MAX + 30 : SSIZE_MAX; + size_t early = 5, late = nbits - 5; + ssize_t fc, fs; + bitstr_t *b; + + /* Check for overflow in size calculation */ + ATF_REQUIRE(nbits >= (size_t)INT_MAX); + ATF_REQUIRE(bitstr_size(nbits) >= nbits / 8); + + /* Allocate the bit string */ + ATF_REQUIRE(b = bit_alloc(nbits)); + + /* Check that we allocated enough */ + ATF_REQUIRE(malloc_usable_size(b) >= bitstr_size(nbits)); + + /* Check ffc, ffs on all-zeroes string */ + bit_ffc(b, nbits, &fc); + ATF_CHECK_EQ(0L, fc); + bit_ffs(b, nbits, &fs); + ATF_CHECK_EQ(-1L, fs); + + /* Set, test, and clear an early bit */ + bit_set(b, early); + bit_ffs(b, nbits, &fs); + ATF_CHECK_EQ((ssize_t)early, fs); + ATF_CHECK_EQ(0, bit_test(b, early - 1)); + ATF_CHECK(bit_test(b, early) != 0); + ATF_CHECK_EQ(0, bit_test(b, early + 1)); + bit_clear(b, early); + ATF_CHECK_EQ(0, bit_test(b, early)); + + /* Set, test, and clear an early bit range */ + bit_nset(b, early - 1, early + 1); + bit_ffs(b, nbits, &fs); + ATF_CHECK_EQ((ssize_t)early - 1, fs); + ATF_CHECK_EQ(0, bit_test(b, early - 2)); + ATF_CHECK(bit_test(b, early - 1)); + ATF_CHECK(bit_test(b, early)); + ATF_CHECK(bit_test(b, early + 1)); + ATF_CHECK_EQ(0, bit_test(b, early + 2)); + bit_nclear(b, early - 1, early + 1); + ATF_CHECK_EQ(0, bit_test(b, early - 1)); + ATF_CHECK_EQ(0, bit_test(b, early)); + ATF_CHECK_EQ(0, bit_test(b, early + 1)); + + /* Set, test, and clear a late bit */ + bit_set(b, late); + bit_ffs(b, nbits, &fs); + ATF_CHECK_EQ((ssize_t)late, fs); + ATF_CHECK_EQ(0, bit_test(b, late - 1)); + ATF_CHECK(bit_test(b, late) != 0); + ATF_CHECK_EQ(0, bit_test(b, late + 1)); + bit_clear(b, late); + ATF_CHECK_EQ(0, bit_test(b, late)); + + /* Set, test, and clear a late bit range */ + bit_nset(b, late - 1, late + 1); + bit_ffs(b, nbits, &fs); + ATF_CHECK_EQ((ssize_t)late - 1, fs); + ATF_CHECK_EQ(0, bit_test(b, late - 2)); + ATF_CHECK(bit_test(b, late - 1)); + ATF_CHECK(bit_test(b, late)); + ATF_CHECK(bit_test(b, late + 1)); + ATF_CHECK_EQ(0, bit_test(b, late + 2)); + bit_nclear(b, late - 1, late + 1); + ATF_CHECK_EQ(0, bit_test(b, late - 1)); + ATF_CHECK_EQ(0, bit_test(b, late)); + ATF_CHECK_EQ(0, bit_test(b, late + 1)); + + free(b); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, bitstr_in_struct); ATF_TP_ADD_TC(tp, bitstr_size); ATF_TP_ADD_TC(tp, bit_ffc_area); ATF_TP_ADD_TC(tp, bit_ffs_area); BITSTRING_TC_ADD(tp, bit_set); BITSTRING_TC_ADD(tp, bit_clear); BITSTRING_TC_ADD(tp, bit_ffs); BITSTRING_TC_ADD(tp, bit_ffc); BITSTRING_TC_ADD(tp, bit_ffs_at); BITSTRING_TC_ADD(tp, bit_ffc_at); BITSTRING_TC_ADD(tp, bit_nclear); BITSTRING_TC_ADD(tp, bit_nset); BITSTRING_TC_ADD(tp, bit_count); BITSTRING_TC_ADD(tp, bit_ffs_area_at_all_or_nothing); BITSTRING_TC_ADD(tp, bit_ffc_area_at_all_or_nothing); BITSTRING_TC_ADD(tp, bit_foreach); BITSTRING_TC_ADD(tp, bit_foreach_at); BITSTRING_TC_ADD(tp, bit_foreach_unset); BITSTRING_TC_ADD(tp, bit_foreach_unset_at); + ATF_TP_ADD_TC(tp, bitstr_large); return (atf_no_error()); }