Rename macros.
[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;
51         size_t bitarray_len;
52         uint8_t *array;
53 } BitArray;
54
55 #define BITARRAY_ALLOC(name, size)   uint8_t name[DIV_ROUNDUP((size), 8)]
56 #define BITARRAY_SIZE(name)         (sizeof((name)) * 8)
57
58 INLINE void bitarray_set(BitArray *bitx, int idx)
59 {
60         ASSERT((size_t)idx <= bitx->bitarray_len);
61
62         int page = idx / 8;
63         uint8_t bit = idx % 8;
64
65         bitx->array[page] |= BV(bit);
66 }
67
68 INLINE void bitarray_clear(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 INLINE void bitarray_setRange(BitArray *bitx, int idx, int offset)
79 {
80         ASSERT((size_t)idx <= bitx->bitarray_len);
81
82         for (int i = idx; i < offset + idx; i++)
83                 bitarray_set(bitx, i);
84 }
85
86
87 INLINE void bitarray_clearRange(BitArray *bitx, int idx, int offset)
88 {
89         ASSERT((size_t)idx <= bitx->bitarray_len);
90
91         for (int i = idx; i < offset + idx; i++)
92                 bitarray_clear(bitx, i);
93 }
94
95 INLINE bool bitarray_test(BitArray *bitx, int idx)
96 {
97         ASSERT((size_t)idx <= bitx->bitarray_len);
98         int page = idx / 8;
99         uint8_t bit = idx % 8;
100
101         return (bitx->array[page] & BV(bit));
102 }
103
104 /*
105  * Ugly!.. reformat it.
106  */
107 INLINE bool bitarray_isFull(BitArray *bitx)
108 {
109         int count = bitx->size;
110         for (size_t page = 0; page <= bitx->size / 8; page++)
111         {
112                 if (count < 8)
113                 {
114                         for (size_t i = page * 8; i <= bitx->bitarray_len; i++)
115                                 if (!bitarray_test(bitx, i))
116                                         return 0;
117                                 count--;
118                 }
119                 else
120                 {
121                         if (!(bitx->array[page] == 0xff))
122                                 return 0;
123                 }
124                 count -= 8;
125         }
126
127         return 1;
128 }
129
130 /*
131  * Ugly!.. reformat it.
132  */
133 INLINE bool bitarray_isRangeFull(BitArray *bitx, int idx, int offset)
134 {
135         ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
136
137         for (int i = idx; i <= idx + offset; i++)
138                 if (!bitarray_test(bitx, i))
139                         return 0;
140
141         return 1;
142 }
143
144 /*
145  * Ugly!.. reformat it.
146  */
147 INLINE bool bitarray_isRangeEmpty(BitArray *bitx, int idx, int offset)
148 {
149         ASSERT((size_t)(idx + offset) <= bitx->bitarray_len);
150
151         for (int i = idx; i <= idx + offset; i++)
152                 if (bitarray_test(bitx, i))
153                         return 0;
154
155         return 1;
156 }
157
158
159 INLINE void bitarray_dump(BitArray *bitx)
160 {
161         kprintf("bitarray size[%zu]bits on [%zu]bytes\n", bitx->bitarray_len, bitx->size);
162
163         int i = 0;
164         int j = 0;
165         int count = bitx->bitarray_len;
166
167         while (count--)
168         {
169                 kprintf("%d", bitarray_test(bitx, i++));
170                 if (j == 7)
171                 {
172                         kprintf("..%02x [%d]\n", bitx->array[(i / 8) - 1], i);
173                         j = 0;
174                         continue;
175                 }
176                 j++;
177         }
178
179         if (j != 0)
180                 kprintf("..%02x [%d]\n", bitx->array[i / 8], i);
181 }
182
183 INLINE void init_bitarray(BitArray *bitx, size_t bitarray_len, uint8_t *array, size_t size)
184 {
185         bitx->size = size;
186         bitx->array = array;
187         bitx->bitarray_len = bitarray_len;
188 }
189
190
191 int bitarray_testSetup(void);
192 int bitarray_testRun(void);
193 int bitarray_testTearDown(void);
194
195 #endif /* STRUCT_BITARRAY_H */