summaryrefslogtreecommitdiffstats
path: root/include
diff options
context:
space:
mode:
authordwmalone <dwmalone@FreeBSD.org>2000-10-09 12:34:51 +0000
committerdwmalone <dwmalone@FreeBSD.org>2000-10-09 12:34:51 +0000
commit7fc1fa165474e47dca5d7dfb2584d97d169d686d (patch)
tree05af28efec26f6986f3d2bab1c2503a7d3f5faf2 /include
parent9dfb8b6e13f05d19533fa203776a55d7a0629005 (diff)
downloadFreeBSD-src-7fc1fa165474e47dca5d7dfb2584d97d169d686d.zip
FreeBSD-src-7fc1fa165474e47dca5d7dfb2584d97d169d686d.tar.gz
Cleanup of bitstring.h:
1) Add FreeBSD: tag. 2) Add parenthesis around macro args. 3) Add parenthesis around macros which are expressions. 4) Add do { ... } while (0) around macros which are compound statements. 5) Sync bitstr_size and bit_alloc with neater versions from NetBSD. 6) Fix bit_ffs and bit_ffc so that they don't search off the end of the bitstirng. 7) Try to avoid rightshifting signed ints. I didn't take NetBSD's version directly as the macros are significantly slower for long bitstrings. Bruce reviewed a previous version of this patch. PR: 21204 Submitted by: bob@immure.com Reviewed by: bde
Diffstat (limited to 'include')
-rw-r--r--include/bitstring.h85
1 files changed, 46 insertions, 39 deletions
diff --git a/include/bitstring.h b/include/bitstring.h
index 88437e7..eafdba1 100644
--- a/include/bitstring.h
+++ b/include/bitstring.h
@@ -33,7 +33,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * @(#)bitstring.h 8.1 (Berkeley) 7/19/93
+ * $FreeBSD$
*/
#ifndef _BITSTRING_H_
@@ -53,16 +53,15 @@ typedef unsigned char bitstr_t;
/* external macros */
/* bytes in a bitstring of nbits bits */
#define bitstr_size(nbits) \
- ((((nbits) - 1) >> 3) + 1)
+ (((nbits) + 7) >> 3)
/* allocate a bitstring */
#define bit_alloc(nbits) \
- (bitstr_t *)calloc(1, \
- (unsigned int)bitstr_size(nbits) * sizeof(bitstr_t))
+ (bitstr_t *)calloc((size_t)bitstr_size(nbits), sizeof(bitstr_t))
/* allocate a bitstring on the stack */
#define bit_decl(name, nbits) \
- (name)[bitstr_size(nbits)]
+ ((name)[bitstr_size(nbits)])
/* is bit N of bitstring name set? */
#define bit_test(name, bit) \
@@ -70,16 +69,16 @@ typedef unsigned char bitstr_t;
/* set bit N of bitstring name */
#define bit_set(name, bit) \
- (name)[_bit_byte(bit)] |= _bit_mask(bit)
+ ((name)[_bit_byte(bit)] |= _bit_mask(bit))
/* clear bit N of bitstring name */
#define bit_clear(name, bit) \
- (name)[_bit_byte(bit)] &= ~_bit_mask(bit)
+ ((name)[_bit_byte(bit)] &= ~_bit_mask(bit))
/* clear bits start ... stop in bitstring */
-#define bit_nclear(name, start, stop) { \
- register bitstr_t *_name = name; \
- register int _start = start, _stop = stop; \
+#define bit_nclear(name, start, stop) do { \
+ register bitstr_t *_name = (name); \
+ register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@@ -91,12 +90,12 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0; \
_name[_stopbyte] &= 0xff << ((_stop&0x7) + 1); \
} \
-}
+} while (0)
/* set bits start ... stop in bitstring */
-#define bit_nset(name, start, stop) { \
- register bitstr_t *_name = name; \
- register int _start = start, _stop = stop; \
+#define bit_nset(name, start, stop) do { \
+ register bitstr_t *_name = (name); \
+ register int _start = (start), _stop = (stop); \
register int _startbyte = _bit_byte(_start); \
register int _stopbyte = _bit_byte(_stop); \
if (_startbyte == _stopbyte) { \
@@ -108,36 +107,44 @@ typedef unsigned char bitstr_t;
_name[_startbyte] = 0xff; \
_name[_stopbyte] |= 0xff >> (7 - (_stop&0x7)); \
} \
-}
+} while (0)
/* find first bit clear in name */
-#define bit_ffc(name, nbits, value) { \
- register bitstr_t *_name = name; \
- register int _byte, _nbits = nbits; \
- register int _stopbyte = _bit_byte(_nbits), _value = -1; \
- for (_byte = 0; _byte <= _stopbyte; ++_byte) \
- if (_name[_byte] != 0xff) { \
- _value = _byte << 3; \
- for (_stopbyte = _name[_byte]; (_stopbyte&0x1); \
- ++_value, _stopbyte >>= 1); \
- break; \
- } \
+#define bit_ffc(name, nbits, value) do { \
+ register bitstr_t *_name = (name); \
+ register int _byte, _nbits = (nbits); \
+ register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
+ if (_nbits > 0) \
+ for (_byte = 0; _byte <= _stopbyte; ++_byte) \
+ if (_name[_byte] != 0xff) { \
+ bitstr_t _lb; \
+ _value = _byte << 3; \
+ for (_lb = _name[_byte]; (_lb&0x1); \
+ ++_value, _lb >>= 1); \
+ break; \
+ } \
+ if (_value >= nbits) \
+ _value = -1; \
*(value) = _value; \
-}
+} while (0)
/* find first bit set in name */
-#define bit_ffs(name, nbits, value) { \
- register bitstr_t *_name = name; \
- register int _byte, _nbits = nbits; \
- register int _stopbyte = _bit_byte(_nbits), _value = -1; \
- for (_byte = 0; _byte <= _stopbyte; ++_byte) \
- if (_name[_byte]) { \
- _value = _byte << 3; \
- for (_stopbyte = _name[_byte]; !(_stopbyte&0x1); \
- ++_value, _stopbyte >>= 1); \
- break; \
- } \
+#define bit_ffs(name, nbits, value) do { \
+ register bitstr_t *_name = (name); \
+ register int _byte, _nbits = (nbits); \
+ register int _stopbyte = _bit_byte(_nbits - 1), _value = -1; \
+ if (_nbits > 0) \
+ for (_byte = 0; _byte <= _stopbyte; ++_byte) \
+ if (_name[_byte]) { \
+ bitstr_t _lb; \
+ _value = _byte << 3; \
+ for (_lb = _name[_byte]; !(_lb&0x1); \
+ ++_value, _lb >>= 1); \
+ break; \
+ } \
+ if (_value >= nbits) \
+ _value = -1; \
*(value) = _value; \
-}
+} while (0)
#endif /* !_BITSTRING_H_ */
OpenPOWER on IntegriCloud