From: asterix Date: Fri, 13 Aug 2010 14:00:03 +0000 (+0000) Subject: Add first implementation of bit array module. X-Git-Tag: 2.6.0~202 X-Git-Url: https://codewiz.org/gitweb?a=commitdiff_plain;h=4ac7a89ec44f465a12154d2ff695684eccf3bb77;p=bertos.git Add first implementation of bit array module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4188 38d2e660-2303-0410-9eaa-f027e97ec537 --- diff --git a/bertos/struct/bitarray.h b/bertos/struct/bitarray.h new file mode 100644 index 00000000..3602d8a6 --- /dev/null +++ b/bertos/struct/bitarray.h @@ -0,0 +1,92 @@ +/** + * \file + * + * + * \brief Bitarray module + * + * \author Daniele Basile + * + * $WIZ$ module_name = "bitarray" + */ + +#ifndef STRUCT_BITARRAY_H +#define STRUCT_BITARRAY_H + +#include +#include +#include + +#include + +typedef struct BitArray +{ + size_t size; + uint8_t *array; + +} BitArray; + + +#define ALLOC_BITARRAY(name, size) uint8_t name[DIV_ROUNDUP((size),8)] + + +INLINE void bitarray_set(BitArray *ctx, int idx) +{ + ASSERT((size_t)idx <= ctx->size); + int page = idx / 8; + int bit = idx % 8; + + ctx->array[page] |= BV(bit); +} + +INLINE void bitarray_clear(BitArray *ctx, int idx) +{ + ASSERT((size_t)idx <= ctx->size); + int page = idx / 8; + int bit = idx % 8; + + ctx->array[page] &= ~BV(bit); +} + +INLINE bool bitarray_check(BitArray *ctx, int idx) +{ + ASSERT((size_t)idx <= ctx->size); + int page = idx / 8; + int bit = idx % 8; + + return (ctx->array[page] & BV(bit)); +} + +INLINE void init_bitarray(BitArray *ctx, uint8_t *array, size_t size) +{ + ctx->size = size; + ctx->array = array; +} + +#endif /* STRUCT_BITARRAY_H */ diff --git a/bertos/struct/bitarray_test.c b/bertos/struct/bitarray_test.c new file mode 100644 index 00000000..fa65c44a --- /dev/null +++ b/bertos/struct/bitarray_test.c @@ -0,0 +1,64 @@ +/** + * \file + * + * + * \brief Bitarray test + * + * \author Daniele Basile + */ + +#include + +#include +#include +#include + +ALLOC_BITARRAY(test1, 128); +BitArray ctx; + +int bitarray_testSetup(void) +{ + kdbg_init(); + init_bitarray(&ctx, test1, sizeof(test1)); + return 0; +} + +int bitarray_testRun(void) +{ + return 0; +} + +int bitarray_testTearDown(void) +{ + return 0; +} + +TEST_MAIN(bitarray);