From 4ac7a89ec44f465a12154d2ff695684eccf3bb77 Mon Sep 17 00:00:00 2001 From: asterix Date: Fri, 13 Aug 2010 14:00:03 +0000 Subject: [PATCH] Add first implementation of bit array module. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@4188 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/struct/bitarray.h | 92 +++++++++++++++++++++++++++++++++++ bertos/struct/bitarray_test.c | 64 ++++++++++++++++++++++++ 2 files changed, 156 insertions(+) create mode 100644 bertos/struct/bitarray.h create mode 100644 bertos/struct/bitarray_test.c 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); -- 2.25.1