summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--sys/sys/bitstring.h13
-rw-r--r--tests/sys/sys/bitstring_test.c12
2 files changed, 21 insertions, 4 deletions
diff --git a/sys/sys/bitstring.h b/sys/sys/bitstring.h
index fa0ee19..32465d1 100644
--- a/sys/sys/bitstring.h
+++ b/sys/sys/bitstring.h
@@ -75,6 +75,12 @@ typedef unsigned long bitstr_t;
#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
+
/* bitstr_t in bit string containing the bit. */
static inline int
_bit_idx(int _bit)
@@ -104,9 +110,8 @@ _bit_make_mask(int _start, int _stop)
}
/*----------------------------- Public Interface -----------------------------*/
-/* Number of bytes consumed by a bit string of nbits bits */
-#define bitstr_size(_nbits) \
- (((_nbits) + _BITSTR_BITS - 1) / 8)
+/* Number of bytes allocated for a bit string of nbits bits */
+#define bitstr_size(_nbits) (_bit_roundup2(_nbits, _BITSTR_BITS) / 8)
/* Allocate a bit string initialized with no bits set. */
#ifdef _KERNEL
@@ -123,7 +128,7 @@ bit_alloc(int _nbits)
}
#endif
-/* Allocate a bit string on the stack with no bits set. */
+/* Allocate a bit string on the stack */
#define bit_decl(name, nbits) \
((name)[bitstr_size(nbits) / sizeof(bitstr_t)])
diff --git a/tests/sys/sys/bitstring_test.c b/tests/sys/sys/bitstring_test.c
index f80115a..fbfe782 100644
--- a/tests/sys/sys/bitstring_test.c
+++ b/tests/sys/sys/bitstring_test.c
@@ -102,6 +102,17 @@ ATF_TC_BODY(bitstr_in_struct, tc)
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 */
{
@@ -407,6 +418,7 @@ ATF_TP_ADD_TCS(tp)
{
ATF_TP_ADD_TC(tp, bitstr_in_struct);
+ ATF_TP_ADD_TC(tp, bitstr_size);
BITSTRING_TC_ADD(tp, bit_set);
BITSTRING_TC_ADD(tp, bit_clear);
BITSTRING_TC_ADD(tp, bit_ffs);
OpenPOWER on IntegriCloud