Clean up code. Add comments. Init module with callback to register all
[bertos.git] / bertos / struct / bitarray.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Bitarray module
33  *
34  * \author Daniele Basile <asterix@develer.com>
35  *
36  * $WIZ$ module_name = "bitarray"
37  */
38
39 #ifndef STRUCT_BITARRAY_H
40 #define STRUCT_BITARRAY_H
41
42 #include <cfg/compiler.h>
43 #include <cfg/macros.h>
44 #include <cfg/debug.h>
45
46 #include <cpu/types.h>
47
48 typedef struct BitArray
49 {
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
53 } BitArray;
54
55 /**
56  * Convenience macro to create a memory area for the BitArray.
57  * \param name Name of the variable.
58  * \param size Number of bits requested. It will be rounded to the nearest
59  *             byte
60  */
61 #define BITARRAY_ALLOC(name, size)   uint8_t name[DIV_ROUNDUP((size), 8)]
62
63 /**
64  * Set one bit into the bit array.
65  * \param bitx BitArray context
66  * \param idx The bit to set
67  */
68 INLINE void bitarray_set(BitArray *bitx, int idx)
69 {
70         ASSERT((size_t)idx <= bitx->bitarray_len);
71
72         int page = idx / 8;
73         uint8_t bit = idx % 8;
74
75         bitx->array[page] |= BV(bit);
76 }
77
78 /**
79  * Clear one bit in the bit array.
80  * \param bitx BitArray context
81  * \param idx The bit to clear
82  */
83 INLINE void bitarray_clear(BitArray *bitx, int idx)
84 {
85         ASSERT((size_t)idx <= bitx->bitarray_len);
86
87         int page = idx / 8;
88         uint8_t bit = idx % 8;
89
90         bitx->array[page] &= ~BV(bit);
91 }
92
93 /**
94  * Set a range of bits.
95  *
96  * The range starts from \a idx (inclusive) and spans \a offset bits.
97  *
98  * \param bitx BitArray context
99  * \param idx Starting bit
100  * \param offset Number of bit to set
101  */
102 INLINE void bitarray_setRange(BitArray *bitx, int idx, int offset)
103 {
104         ASSERT((size_t)idx <= bitx->bitarray_len);
105
106         for (int i = idx; i < offset + idx; i++)
107                 bitarray_set(bitx, i);
108 }
109
110 /**
111  * Clear a range of bits.
112  *
113  * The range starts from \a idx (inclusive) and spans \a offset bits.
114  *
115  * \param bitx BitArray context
116  * \param idx Starting bit
117  * \param offset Number of bits to clear
118  */
119 INLINE void bitarray_clearRange(BitArray *bitx, int idx, int offset)
120 {
121         ASSERT((size_t)idx <= bitx->bitarray_len);
122
123         for (int i = idx; i < offset + idx; i++)
124                 bitarray_clear(bitx, i);
125 }
126
127 /**
128  * Test a bit.
129  *
130  * \param bitx BitArray context
131  * \param idx Bit to test
132  * \return True if bit is set, false otherwise.
133  */
134 INLINE bool bitarray_test(BitArray *bitx, int idx)
135 {
136         ASSERT((size_t)idx <= bitx->bitarray_len);
137         int page = idx / 8;
138         uint8_t bit = idx % 8;
139
140         return (bitx->array[page] & BV(bit));
141 }
142
143 /**
144  * Check if the bitarray is full
145  *
146  * Only \a bitarray_len bits are tested.
147  *
148  * \param bitx BitArray to test
149  * \return True if \a bitx is full, false otherwise
150  */
151 INLINE bool bitarray_isFull(BitArray *bitx)
152 {
153         // test full bytes except the last one
154         for (size_t page = 0; page <= bitx->size - 2; page++)
155         {
156                 if (!(bitx->array[page] == 0xff))
157                         return 0;
158         }
159         // test the last byte using the correct bitmask
160         uint8_t mask = BV(bitx->bitarray_len >> 3) - 1;
161         if (!(bitx->array[bitx->size - 1] & mask))
162                 return 0;
163
164         return 1;
165 }
166
167 /*
168  * Ugly!.. reformat it.
169  */
170 /**
171  * Test if a range of bit is full.
172  *
173  * \param bitx BitArray context
174  * \param idx Starting bit
175  * \param offset Number of bits to test
176  * \return True if range is full, false otherwise
177  */
178 INLINE bool bitarray_isRangeFull(BitArray *bitx, int idx, int offset)
179 {
180         ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
181
182         for (int i = idx; i <= idx + offset; i++)
183                 if (!bitarray_test(bitx, i))
184                         return 0;
185
186         return 1;
187 }
188
189 /*
190  * Ugly!.. reformat it.
191  */
192 /**
193  * Test if a range of bit is empty.
194  *
195  * \param bitx BitArray context
196  * \param idx Starting bit
197  * \param offset Number of bits to test
198  * \return True if range is empty, false otherwise
199  */
200 INLINE bool bitarray_isRangeEmpty(BitArray *bitx, int idx, int offset)
201 {
202         ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
203
204         for (int i = idx; i <= idx + offset; i++)
205                 if (bitarray_test(bitx, i))
206                         return 0;
207
208         return 1;
209 }
210
211 /**
212  * Print on debug serial a BitArray.
213  * \note This module does not use the logging module, so you
214  *       can't decide the logging level.
215  * \param bitx BitArray to be printed.
216  */
217 INLINE void bitarray_dump(BitArray *bitx)
218 {
219         kprintf("bitarray size[%zu]bits on [%zu]bytes\n", bitx->bitarray_len, bitx->size);
220
221         int i = 0;
222         int j = 0;
223         int count = bitx->bitarray_len;
224
225         while (count--)
226         {
227                 kprintf("%d", bitarray_test(bitx, i++));
228                 if (j == 7)
229                 {
230                         kprintf("..%02x [%d]\n", bitx->array[(i / 8) - 1], i);
231                         j = 0;
232                         continue;
233                 }
234                 j++;
235         }
236
237         if (j != 0)
238                 kprintf("..%02x [%d]\n", bitx->array[i / 8], i);
239 }
240
241 int bitarray_firstSetBit(BitArray *bitx);
242
243 /**
244  * Init a BitArray.
245  *
246  * The BitArray uses an external array for storage. You can use the macro
247  * BITARRAY_ALLOC to declare an appropriate memory size. Example usage:
248  * \code
249  * BITARRAY_ALLOC(bits_mem, 17);
250  * BitArray bits;
251  * bitarray_init(&bits, 17, bits_mem, sizeof(bits_mem))
252  * \endcode
253  *
254  * \param bitx BitArray context
255  * \param bitarray_len Number of bits in the BitArray
256  * \param array Memory area for the BitArray
257  * \param size Size (in bytes) of the memory area \a array
258  */
259 INLINE void bitarray_init(BitArray *bitx, size_t bitarray_len, uint8_t *array, size_t size)
260 {
261         bitx->size = size;
262         bitx->array = array;
263         bitx->bitarray_len = bitarray_len;
264 }
265
266
267 int bitarray_testSetup(void);
268 int bitarray_testRun(void);
269 int bitarray_testTearDown(void);
270
271 #endif /* STRUCT_BITARRAY_H */