1 | /* |
---|
2 | * bits.h - bitmap API definition |
---|
3 | * |
---|
4 | * Author Ghassan Almaless (2008,2009,2010,2011,2012) |
---|
5 | * Alain Greiner (2016,2017,2018,2019,2020) |
---|
6 | * |
---|
7 | * Copyright (c) UPMC Sorbonne Universites |
---|
8 | * |
---|
9 | * This file is part of ALMOS-MKH. |
---|
10 | * |
---|
11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
12 | * under the terms of the GNU General Public License as published by |
---|
13 | * the Free Software Foundation; version 2.0 of the License. |
---|
14 | * |
---|
15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU General Public License |
---|
21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
23 | */ |
---|
24 | |
---|
25 | #ifndef _BITS_H_ |
---|
26 | #define _BITS_H_ |
---|
27 | |
---|
28 | #include <kernel_config.h> |
---|
29 | #include <hal_kernel_types.h> |
---|
30 | #include <hal_remote.h> |
---|
31 | |
---|
32 | /********************************************************************************************** |
---|
33 | * This file defines the API to access a generic bitmap, that can be local or remote. |
---|
34 | * It is implemented as an array of uint32_t words. |
---|
35 | * The number of entries in this array is statically defined at compile time |
---|
36 | * and defines the max number of items that can be registered in the bitmap. |
---|
37 | * The remote accesses are used in the VFS by the inum allocator. |
---|
38 | *********************************************************************************************/ |
---|
39 | |
---|
40 | typedef uint32_t bitmap_t; |
---|
41 | |
---|
42 | /********************************************************************************************** |
---|
43 | * This macro returns the number of 32 bits words required to register <size> entries. |
---|
44 | *********************************************************************************************/ |
---|
45 | |
---|
46 | #define BITMAP_SIZE(size) ( ((size) & 31) ? (((size)>>5) + 1) : ((size)>>5) ) |
---|
47 | |
---|
48 | /********************************************************************************************** |
---|
49 | * This macro returns the smallest power of 2 value, that is larger or equal to data value. |
---|
50 | * It returns 0xFFFFFFFF if data is larger than 0x80000000. |
---|
51 | *********************************************************************************************/ |
---|
52 | |
---|
53 | #define POW2_ROUNDUP(data) ( (data <= 0x00000001) ? 0x00000001 : \ |
---|
54 | (data <= 0x00000002) ? 0x00000002 : \ |
---|
55 | (data <= 0x00000004) ? 0x00000004 : \ |
---|
56 | (data <= 0x00000008) ? 0x00000008 : \ |
---|
57 | (data <= 0x00000010) ? 0x00000010 : \ |
---|
58 | (data <= 0x00000020) ? 0x00000020 : \ |
---|
59 | (data <= 0x00000040) ? 0x00000040 : \ |
---|
60 | (data <= 0x00000080) ? 0x00000080 : \ |
---|
61 | (data <= 0x00000100) ? 0x00000100 : \ |
---|
62 | (data <= 0x00000200) ? 0x00000200 : \ |
---|
63 | (data <= 0x00000400) ? 0x00000400 : \ |
---|
64 | (data <= 0x00000800) ? 0x00000800 : \ |
---|
65 | (data <= 0x00001000) ? 0x00001000 : \ |
---|
66 | (data <= 0x00002000) ? 0x00002000 : \ |
---|
67 | (data <= 0x00004000) ? 0x00004000 : \ |
---|
68 | (data <= 0x00008000) ? 0x00008000 : \ |
---|
69 | (data <= 0x00010000) ? 0x00010000 : \ |
---|
70 | (data <= 0x00020000) ? 0x00020000 : \ |
---|
71 | (data <= 0x00040000) ? 0x00040000 : \ |
---|
72 | (data <= 0x00080000) ? 0x00080000 : \ |
---|
73 | (data <= 0x00100000) ? 0x00100000 : \ |
---|
74 | (data <= 0x00200000) ? 0x00200000 : \ |
---|
75 | (data <= 0x00400000) ? 0x00400000 : \ |
---|
76 | (data <= 0x00800000) ? 0x00800000 : \ |
---|
77 | (data <= 0x01000000) ? 0x01000000 : \ |
---|
78 | (data <= 0x02000000) ? 0x02000000 : \ |
---|
79 | (data <= 0x04000000) ? 0x04000000 : \ |
---|
80 | (data <= 0x08000000) ? 0x08000000 : \ |
---|
81 | (data <= 0x10000000) ? 0x10000000 : \ |
---|
82 | (data <= 0x20000000) ? 0x20000000 : \ |
---|
83 | (data <= 0x40000000) ? 0x40000000 : \ |
---|
84 | (data <= 0x80000000) ? 0x80000000 : 0xFFFFFFFF ) |
---|
85 | |
---|
86 | /********************************************************************************************* |
---|
87 | * These macros are NOT used by the bitmap, but are useful in other contexts... [AG] |
---|
88 | *********************************************************************************************/ |
---|
89 | |
---|
90 | #define ARROUND_UP(val, size) (((val) & ((size) -1)) ? ((val) & ~((size)-1)) + (size) : (val)) |
---|
91 | #define ARROUND_DOWN(val, size) ((val) & ~((size) - 1)) |
---|
92 | |
---|
93 | #define ABS(x) (((x) < 0) ? -(x) : (x)) |
---|
94 | #define MIN(x,y) (((x) < (y)) ? (x) : (y)) |
---|
95 | #define MAX(x,y) (((x) < (y)) ? (y) : (x)) |
---|
96 | |
---|
97 | /********************************************************************************************* |
---|
98 | * This function reset all bits in a local or remote bitmap. |
---|
99 | ********************************************************************************************* |
---|
100 | * @ bitmap : pointer on first word in the bitmap. |
---|
101 | * @ size : number of bits in bitmap. |
---|
102 | ********************************************************************************************/ |
---|
103 | extern void bitmap_init( bitmap_t * bitmap, |
---|
104 | uint32_t size ); |
---|
105 | |
---|
106 | extern void bitmap_remote_init( xptr_t bitmap_xp, |
---|
107 | uint32_t size ); |
---|
108 | |
---|
109 | /********************************************************************************************* |
---|
110 | * These functions set a specific bit in a local or remote bitmap. |
---|
111 | ********************************************************************************************* |
---|
112 | * @ bitmap : pointer on the bitmap |
---|
113 | * @ index : bit index in the bitmap |
---|
114 | ********************************************************************************************/ |
---|
115 | extern inline void bitmap_set( bitmap_t * bitmap, |
---|
116 | uint32_t index ); |
---|
117 | |
---|
118 | extern inline void bitmap_remote_set( xptr_t bitmap_xp, |
---|
119 | uint32_t index ); |
---|
120 | |
---|
121 | /********************************************************************************************* |
---|
122 | * These functions clear a specific bit in a local or remote bitmap. |
---|
123 | ********************************************************************************************* |
---|
124 | * @ bitmap : pointer on the bitmap |
---|
125 | * @ index : bit index in the bitmap |
---|
126 | ********************************************************************************************/ |
---|
127 | extern inline void bitmap_clear( bitmap_t * bitmap, |
---|
128 | uint32_t index ); |
---|
129 | |
---|
130 | extern inline void bitmap_remote_clear( xptr_t bitmap_xp, |
---|
131 | uint32_t index ); |
---|
132 | |
---|
133 | /********************************************************************************************* |
---|
134 | * These functions search the first bit non-set in a local or remote bitmap, in the |
---|
135 | * range [0 , size-1], set this bit, and return the index of the found bit. |
---|
136 | * The lock protecting the bitmap must be taken by the caller. |
---|
137 | ********************************************************************************************* |
---|
138 | * @ bitmap : pointer on the bitmap. |
---|
139 | * @ size : number of bits to scan. |
---|
140 | * @ returns index of found bit / returns 0xFFFFFFFF if not found. |
---|
141 | ********************************************************************************************/ |
---|
142 | extern uint32_t bitmap_alloc( bitmap_t * bitmap, |
---|
143 | uint32_t size ); |
---|
144 | |
---|
145 | extern uint32_t bitmap_remote_alloc( xptr_t bitmap_xp, |
---|
146 | uint32_t size ); |
---|
147 | |
---|
148 | /********************************************************************************************* |
---|
149 | * This function returns the index of aa specific bit in a bitmap. |
---|
150 | ********************************************************************************************* |
---|
151 | * @ bitmap : pointer on the bitmap |
---|
152 | * @ index : bit index in the bitmap |
---|
153 | * @ returns true if bitmap[index] is set |
---|
154 | ********************************************************************************************/ |
---|
155 | extern inline bool_t bitmap_state( bitmap_t * bitmap, |
---|
156 | uint32_t index ); |
---|
157 | |
---|
158 | /********************************************************************************************* |
---|
159 | * This function set a range of bits in a bitmap : [index ... (index + len)[ |
---|
160 | ********************************************************************************************* |
---|
161 | * @ bitmap : pointer on the bitmap |
---|
162 | * @ index : first bit index in the bitmap |
---|
163 | * @ len : number of bits to set |
---|
164 | ********************************************************************************************/ |
---|
165 | extern void bitmap_set_range( bitmap_t * bitmap, |
---|
166 | uint32_t index, |
---|
167 | uint32_t len ); |
---|
168 | |
---|
169 | /********************************************************************************************* |
---|
170 | * This function reset a range of bits in a bitmap : [index ... (index + len)[ |
---|
171 | ********************************************************************************************* |
---|
172 | * @ bitmap : pointer on the bitmap |
---|
173 | * @ index : first bit index in the bitmap |
---|
174 | * @ len : number of bits to clear |
---|
175 | ********************************************************************************************/ |
---|
176 | extern void bitmap_clear_range( bitmap_t * bitmap, |
---|
177 | uint32_t index, |
---|
178 | uint32_t len ); |
---|
179 | |
---|
180 | /********************************************************************************************* |
---|
181 | * This function returns the index of first bit set in a bitmap, starting from index. |
---|
182 | ********************************************************************************************* |
---|
183 | * @ bitmap : pointer on the bitmap |
---|
184 | * @ index : first bit to analyse in the bitmap |
---|
185 | * @ size : number of bits to analyse in bitmap |
---|
186 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
187 | ********************************************************************************************/ |
---|
188 | extern uint32_t bitmap_ffs2( bitmap_t * bitmap, |
---|
189 | uint32_t index, |
---|
190 | uint32_t size ); |
---|
191 | |
---|
192 | /********************************************************************************************* |
---|
193 | * This function returns the index of first bit cleared in a bitmap, starting from index. |
---|
194 | ********************************************************************************************* |
---|
195 | * @ bitmap : pointer on the bitmap |
---|
196 | * @ index : first bit to analyse in the bitmap |
---|
197 | * @ size : number of bits to analyse in bitmap |
---|
198 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
199 | ********************************************************************************************/ |
---|
200 | extern uint32_t bitmap_ffc2( bitmap_t * bitmap, |
---|
201 | uint32_t index, |
---|
202 | uint32_t size ); |
---|
203 | |
---|
204 | /********************************************************************************************* |
---|
205 | * This function returns the index of first bit set in a bitmap, starting from bit 0. |
---|
206 | ********************************************************************************************* |
---|
207 | * @ bitmap : pointer on the bitmap |
---|
208 | * @ size : number of bits to analyse in bitmap |
---|
209 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
210 | ********************************************************************************************/ |
---|
211 | extern uint32_t bitmap_ffs( bitmap_t * bitmap, |
---|
212 | uint32_t size ); |
---|
213 | |
---|
214 | /********************************************************************************************* |
---|
215 | * These functions return the index of first bit cleared in a local or remote bitmap, |
---|
216 | * starting from bit 0. |
---|
217 | ********************************************************************************************* |
---|
218 | * @ bitmap : pointer on the bitmap |
---|
219 | * @ size : number of bits to analyse in bitmap |
---|
220 | * @ returns index if found / returns 0xFFFFFFFF if bit not found |
---|
221 | ********************************************************************************************/ |
---|
222 | extern uint32_t bitmap_ffc( bitmap_t * bitmap, |
---|
223 | uint32_t size ); |
---|
224 | |
---|
225 | extern uint32_t bitmap_remote_ffc( xptr_t bitmap_xp, |
---|
226 | uint32_t size ); |
---|
227 | |
---|
228 | /********************************************************************************************* |
---|
229 | * This function takes a positive integer <val> as input argument, and returns the smallest |
---|
230 | * integer <order> such as : 1<<order >= val. |
---|
231 | * In other words, <order> is the min number of bits to encode the <val> values. |
---|
232 | ********************************************************************************************* |
---|
233 | * @ val : value to analyse |
---|
234 | * @ returns logarithm value |
---|
235 | ********************************************************************************************/ |
---|
236 | static inline uint32_t bits_log2( uint32_t val ) |
---|
237 | { |
---|
238 | uint32_t i; |
---|
239 | |
---|
240 | if( val > 0 ) |
---|
241 | { |
---|
242 | val--; |
---|
243 | for( i=0 ; val > 0 ; i++ ) val = val >> 1; |
---|
244 | return i; |
---|
245 | } |
---|
246 | return 0; |
---|
247 | } |
---|
248 | |
---|
249 | #endif /* _BITS_H_ */ |
---|