4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
32 * \brief Bitarray module
34 * \author Daniele Basile <asterix@develer.com>
36 * $WIZ$ module_name = "bitarray"
39 #ifndef STRUCT_BITARRAY_H
40 #define STRUCT_BITARRAY_H
42 #include <cfg/compiler.h>
43 #include <cfg/macros.h>
44 #include <cfg/debug.h>
46 #include <cpu/types.h>
48 typedef struct BitArray
50 size_t size; /// Size in bytes of the bitarray
51 size_t bitarray_len; /// Number of bits used
52 uint8_t *array; /// Pointer to memory occupied by the bitarray
55 #define BITARRAY_ALLOC(name, size) uint8_t name[DIV_ROUNDUP((size), 8)]
56 #define BITARRAY_SIZE(name) (sizeof((name)) * 8)
58 INLINE void bitarray_set(BitArray *bitx, int idx)
60 ASSERT((size_t)idx <= bitx->bitarray_len);
63 uint8_t bit = idx % 8;
65 bitx->array[page] |= BV(bit);
68 INLINE void bitarray_clear(BitArray *bitx, int idx)
70 ASSERT((size_t)idx <= bitx->bitarray_len);
73 uint8_t bit = idx % 8;
75 bitx->array[page] &= ~BV(bit);
78 INLINE void bitarray_setRange(BitArray *bitx, int idx, int offset)
80 ASSERT((size_t)idx <= bitx->bitarray_len);
82 for (int i = idx; i < offset + idx; i++)
83 bitarray_set(bitx, i);
87 INLINE void bitarray_clearRange(BitArray *bitx, int idx, int offset)
89 ASSERT((size_t)idx <= bitx->bitarray_len);
91 for (int i = idx; i < offset + idx; i++)
92 bitarray_clear(bitx, i);
95 INLINE bool bitarray_test(BitArray *bitx, int idx)
97 ASSERT((size_t)idx <= bitx->bitarray_len);
99 uint8_t bit = idx % 8;
101 return (bitx->array[page] & BV(bit));
105 * Check if the bitarray is full
107 * Only \a bitarray_len bits are tested.
109 INLINE bool bitarray_isFull(BitArray *bitx)
111 // test full bytes except the last one
112 for (size_t page = 0; page <= bitx->size - 2; page++)
114 if (!(bitx->array[page] == 0xff))
117 // test the last byte using the correct bitmask
118 uint8_t mask = BV(bitx->bitarray_len >> 3) - 1;
119 if (!(bitx->array[bitx->size - 1] & mask))
126 * Ugly!.. reformat it.
128 INLINE bool bitarray_isRangeFull(BitArray *bitx, int idx, int offset)
130 ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
132 for (int i = idx; i <= idx + offset; i++)
133 if (!bitarray_test(bitx, i))
140 * Ugly!.. reformat it.
142 INLINE bool bitarray_isRangeEmpty(BitArray *bitx, int idx, int offset)
144 ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
146 for (int i = idx; i <= idx + offset; i++)
147 if (bitarray_test(bitx, i))
154 INLINE void bitarray_dump(BitArray *bitx)
156 kprintf("bitarray size[%zu]bits on [%zu]bytes\n", bitx->bitarray_len, bitx->size);
160 int count = bitx->bitarray_len;
164 kprintf("%d", bitarray_test(bitx, i++));
167 kprintf("..%02x [%d]\n", bitx->array[(i / 8) - 1], i);
175 kprintf("..%02x [%d]\n", bitx->array[i / 8], i);
178 INLINE void bitarray_init(BitArray *bitx, size_t bitarray_len, uint8_t *array, size_t size)
182 bitx->bitarray_len = bitarray_len;
186 int bitarray_testSetup(void);
187 int bitarray_testRun(void);
188 int bitarray_testTearDown(void);
190 #endif /* STRUCT_BITARRAY_H */