From 8ad4f1f6d610ce74466f049b25950cf0fd981856 Mon Sep 17 00:00:00 2001 From: lottaviano Date: Fri, 10 Sep 2010 07:55:47 +0000 Subject: [PATCH] Improve performance of bitarray_isFull(). Add documentation. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4225 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/struct/bitarray.h | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/bertos/struct/bitarray.h b/bertos/struct/bitarray.h index 36d1aa2a..3e96b7c5 100644 --- a/bertos/struct/bitarray.h +++ b/bertos/struct/bitarray.h @@ -47,9 +47,9 @@ typedef struct BitArray { - size_t size; - size_t bitarray_len; - uint8_t *array; + size_t size; /// Size in bytes of the bitarray + size_t bitarray_len; /// Number of bits used + uint8_t *array; /// Pointer to memory occupied by the bitarray } BitArray; #define BITARRAY_ALLOC(name, size) uint8_t name[DIV_ROUNDUP((size), 8)] @@ -101,28 +101,23 @@ INLINE bool bitarray_test(BitArray *bitx, int idx) return (bitx->array[page] & BV(bit)); } -/* - * Ugly!.. reformat it. +/** + * Check if the bitarray is full + * + * Only \a bitarray_len bits are tested. */ INLINE bool bitarray_isFull(BitArray *bitx) { - int count = bitx->size; - for (size_t page = 0; page <= bitx->size / 8; page++) + // test full bytes except the last one + for (size_t page = 0; page <= bitx->size - 2; page++) { - if (count < 8) - { - for (size_t i = page * 8; i <= bitx->bitarray_len; i++) - if (!bitarray_test(bitx, i)) - return 0; - count--; - } - else - { - if (!(bitx->array[page] == 0xff)) - return 0; - } - count -= 8; + if (!(bitx->array[page] == 0xff)) + return 0; } + // test the last byte using the correct bitmask + uint8_t mask = BV(bitx->bitarray_len >> 3) - 1; + if (!(bitx->array[bitx->size - 1] & mask)) + return 0; return 1; } -- 2.25.1