source: rtems/cpukit/libfs/src/rfs/rtems-rfs-bitmaps.h @ 3c96bee

4.115
Last change on this file since 3c96bee was a15eaaf, checked in by Joel Sherrill <joel.sherrill@…>, on 01/10/13 at 19:20:34

cpukit: Doxygen group fixes and many warnings addressed

The output of the modules.html is much improved. Most
filesystem and POSIX API related groups are properly nested.
Some formatting issues were addressed as were multiple
inconsistencies.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/**
2 * @file
3 *
4 * @brief RTEMS File Systems Bitmap Routines
5 *
6 * @ingroup rtems_rfs
7 *
8 * RTEMS File Systems Bitmap Routines.
9 *
10 * These functions manage bit maps. A bit map consists of the map of bit
11 * allocated in a block and a search map where a bit represents 32 actual
12 * bits. The search map allows for a faster search for an available bit as 32
13 * search bits can checked in a test.
14 */
15
16/*
17 *  COPYRIGHT (c) 2010 Chris Johns <chrisj@rtems.org>
18 *
19 *  The license and distribution terms for this file may be
20 *  found in the file LICENSE in this distribution or at
21 *  http://www.rtems.com/license/LICENSE.
22 */
23
24
25#if !defined (_RTEMS_RFS_BITMAPS_H_)
26#define _RTEMS_RFS_BITMAPS_H_
27
28#include <rtems/rfs/rtems-rfs-buffer.h>
29#include <rtems/rfs/rtems-rfs-file-system-fwd.h>
30#include <rtems/rfs/rtems-rfs-trace.h>
31
32/**
33 * Define the way the bits are configured. We can have them configured as clear
34 * being 0 or clear being 1. This does not effect how masks are defined. A mask
35 * always has a 1 for set and 0 for clear.
36 */
37#define RTEMS_RFS_BITMAP_CLEAR_ZERO 0
38
39#if RTEMS_RFS_BITMAP_CLEAR_ZERO
40/*
41 * Bit set is a 1 and clear is 0.
42 */
43#define RTEMS_RFS_BITMAP_BIT_CLEAR          0
44#define RTEMS_RFS_BITMAP_BIT_SET            1
45#define RTEMS_RFS_BITMAP_ELEMENT_SET        (RTEMS_RFS_BITMAP_ELEMENT_FULL_MASK)
46#define RTEMS_RFS_BITMAP_ELEMENT_CLEAR      (0)
47#define RTEMS_RFS_BITMAP_SET_BITS(_t, _b)   ((_t) | (_b))
48#define RTEMS_RFS_BITMAP_CLEAR_BITS(_t, _b) ((_t) & ~(_b))
49#define RTEMS_RFS_BITMAP_TEST_BIT(_t, _b)   (((_t) & (1 << (_b))) != 0 ? true : false)
50#else
51/*
52 * Bit set is a 0 and clear is 1.
53 */
54#define RTEMS_RFS_BITMAP_BIT_CLEAR          1
55#define RTEMS_RFS_BITMAP_BIT_SET            0
56#define RTEMS_RFS_BITMAP_ELEMENT_SET        (0)
57#define RTEMS_RFS_BITMAP_ELEMENT_CLEAR      (RTEMS_RFS_BITMAP_ELEMENT_FULL_MASK)
58#define RTEMS_RFS_BITMAP_SET_BITS(_t, _b)   ((_t) & ~(_b))
59#define RTEMS_RFS_BITMAP_CLEAR_BITS(_t, _b) ((_t) | (_b))
60#define RTEMS_RFS_BITMAP_TEST_BIT(_t, _b)   (((_t) & (1 << (_b))) == 0 ? true : false)
61#endif
62
63/**
64 * Invert a mask. Masks are always 1 for set and 0 for clear.
65 */
66#define RTEMS_RFS_BITMAP_INVERT_MASK(_mask) (~(_mask))
67
68/**
69 * This is the full mask of the length of the element. A mask is always a 1 for
70 * set and 0 for clear. It is not effected by the state of
71 * RTEMS_RFS_BITMAP_CLEAR_ZERO.
72 */
73#define RTEMS_RFS_BITMAP_ELEMENT_FULL_MASK (0xffffffffUL)
74
75/**
76 * The bitmap search window. Searches occur around a seed in either direction
77 * for half the window.
78 */
79#define RTEMS_RFS_BITMAP_SEARCH_WINDOW (rtems_rfs_bitmap_element_bits () * 64)
80
81/**
82 * A bit in a map.
83 */
84typedef int32_t rtems_rfs_bitmap_bit;
85
86/**
87 * The basic element of a bitmap. A bitmap is manipulated by elements.
88 */
89typedef uint32_t rtems_rfs_bitmap_element;
90
91/**
92 * The power of 2 number of bits in the element.
93 */
94#define RTEMS_RFS_ELEMENT_BITS_POWER_2 (5)
95
96/**
97 * A bitmap or map is an array of bitmap elements.
98 */
99typedef rtems_rfs_bitmap_element* rtems_rfs_bitmap_map;
100
101/**
102 * The bitmap control is a simple way to manage the various parts of a bitmap.
103 */
104typedef struct rtems_rfs_bitmap_control_s
105{
106  rtems_rfs_buffer_handle* buffer;      //< Handle the to buffer with the bit
107                                        //map.
108  rtems_rfs_file_system*   fs;          //< The map's file system.
109  rtems_rfs_buffer_block   block;       //< The map's block number on disk.
110  size_t                   size;        //< Number of bits in the map. Passed
111                                        //to create.
112  size_t                   free;        //< Number of bits in the map that are
113                                        //free (clear).
114  rtems_rfs_bitmap_map     search_bits; //< The search bit map memory.
115} rtems_rfs_bitmap_control;
116
117/**
118 * Return the number of bits for the number of bytes provided.
119 */
120#define rtems_rfs_bitmap_numof_bits(_bytes) (8 * (_bytes))
121
122/**
123 * Return the number of bits for the number of bytes provided.  The search
124 * element and the element must have the same number of bits.
125 */
126#define rtems_rfs_bitmap_element_bits() \
127  rtems_rfs_bitmap_numof_bits (sizeof (rtems_rfs_bitmap_element))
128
129/**
130 * Return the number of bits a search element covers.
131 */
132#define rtems_rfs_bitmap_search_element_bits() \
133  (rtems_rfs_bitmap_element_bits() * rtems_rfs_bitmap_element_bits())
134
135/**
136 * Return the number of elements for a given number of bits.
137 */
138#define rtems_rfs_bitmap_elements(_bits) \
139  ((((_bits) - 1) / rtems_rfs_bitmap_element_bits()) + 1)
140
141/**
142 * Release the bitmap buffer back to the buffer pool or cache.
143 */
144#define rtems_rfs_bitmap_release_buffer(_fs, _bm)    \
145  rtems_rfs_buffer_handle_release (_fs, (_bm)->buffer)
146
147/**
148 * Return the element index for a given bit. We use a macro to hide any
149 * implementation assuptions. Typically this would be calculated by dividing
150 * the bit index by the number of bits in an element. Given we have a power of
151 * 2 as the number of bits we can avoid the division by using a shift. A good
152 * compiler should figure this out but I would rather enforce this than rely on
153 * the specific backend of a compiler to do the right thing.
154 */
155#define rtems_rfs_bitmap_map_index(_b) \
156  ((_b) >> RTEMS_RFS_ELEMENT_BITS_POWER_2)
157
158/**
159 * Return the bit offset for a given bit in an element in a map. See @ref
160 * rtems_rfs_bitmap_map_index for a detailed reason why.
161 */
162#define rtems_rfs_bitmap_map_offset(_b) \
163  ((_b) & ((1 << RTEMS_RFS_ELEMENT_BITS_POWER_2) - 1))
164
165/**
166 * Return the size of the bitmap.
167 */
168#define rtems_rfs_bitmap_map_size(_c) ((_c)->size)
169
170/**
171 * Return the number of free bits in the bitmap.
172 */
173#define rtems_rfs_bitmap_map_free(_c) ((_c)->free)
174
175/**
176 * Return the buffer handle.
177 */
178#define rtems_rfs_bitmap_map_handle(_c) ((_c)->buffer)
179
180/**
181 * Return the bitmap map block.
182 */
183#define rtems_rfs_bitmap_map_block(_c) ((_c)->block)
184
185/**
186 * Create a bit mask with the specified number of bits up to an element's
187 * size. The mask is aligned to bit 0 of the element.
188 *
189 * @param[in] size is the number of bits in the mask.
190 *
191 * @return The mask of the argument size number of bits.
192 */
193rtems_rfs_bitmap_element rtems_rfs_bitmap_mask (unsigned int size);
194
195/**
196 * Create a bit mask section. A mask section is a mask that is not aligned to
197 * an end of the element.
198 *
199 * @param[in] start is the first bit of the mask numbered from 0.
200 * @param[in] end is the end bit of the mask numbered from 0.
201 *
202 * @return Mask section as defined by the start and end arguments.
203 */
204rtems_rfs_bitmap_element rtems_rfs_bitmap_mask_section (unsigned int start,
205                                                        unsigned int end);
206
207/**
208 * Set a bit in a map and if all the bits are set, set the search map bit as
209 * well.
210 *
211 * @param[in] control is the control for the map.
212 * @param[in] bit is the bit in the map to set.
213 *
214 * @retval 0 Successful operation.
215 * @retval error_code An error occurred.
216 */
217int rtems_rfs_bitmap_map_set (rtems_rfs_bitmap_control* control,
218                              rtems_rfs_bitmap_bit      bit);
219
220/**
221 * Clear a bit in a map and make sure the search map bit is clear so a search
222 * will find this bit available.
223 *
224 * @param[in] control is the control for the map.
225 * @param[in] bit is the bit in the map to clear.
226 *
227 * @retval 0 Successful operation.
228 * @retval error_code An error occurred.
229 */
230int rtems_rfs_bitmap_map_clear (rtems_rfs_bitmap_control* control,
231                                rtems_rfs_bitmap_bit      bit);
232
233/**
234 * Test a bit in the map.
235 *
236 * @param[in] control is the bitmap control.
237 * @param[in] bit is the bit to test.
238 * @param[in] state is the state of the bit if no error is returned.
239 *
240 * @retval 0 Successful operation.
241 * @retval error_code An error occurred.
242 */
243int
244rtems_rfs_bitmap_map_test (rtems_rfs_bitmap_control* control,
245                           rtems_rfs_bitmap_bit      bit,
246                           bool*                     state);
247
248/**
249 * Set all bits in the bitmap and set the dirty bit.
250 *
251 * @param[in] control is the bitmap control.
252 *
253 * @retval 0 Successful operation.
254 * @retval error_code An error occurred.
255 */
256int rtems_rfs_bitmap_map_set_all (rtems_rfs_bitmap_control* control);
257
258/**
259 * Clear all bits in the bitmap and set the dirty bit.
260 *
261 * @param[in] control is the bitmap control.
262 *
263 * @retval 0 Successful operation.
264 * @retval error_code An error occurred.
265 */
266int rtems_rfs_bitmap_map_clear_all (rtems_rfs_bitmap_control* control);
267
268/**
269 * Find a free bit searching from the seed up and down until found. The search
270 * is performing by moving up from the seed for the window distance then to
271 * search down from the seed for the window distance. This is repeated out
272 * from the seed for each window until a free bit is found. The search is
273 * performed by checking the search map to see if the map has a free bit.
274 *
275 * @param[in] control is the map control.
276 * @param[in] seed is the bit to search out from.
277 * @param[out] allocate A bit was allocated.
278 * @param[out] bit will contain the bit found free if true is returned.
279 *
280 * @retval 0 Successful operation.
281 * @retval error_code An error occurred.
282 */
283int rtems_rfs_bitmap_map_alloc (rtems_rfs_bitmap_control* control,
284                                rtems_rfs_bitmap_bit      seed,
285                                bool*                     allocate,
286                                rtems_rfs_bitmap_bit*     bit);
287
288/**
289 * Create a search bit map from the actual bit map.
290 *
291 * @param[in] control is the map control.
292 *
293 * @retval 0 Successful operation.
294 * @retval error_code An error occurred.
295 */
296int rtems_rfs_bitmap_create_search (rtems_rfs_bitmap_control* control);
297
298/**
299 * Open a bitmap control with a map and search map.
300 *
301 * @param[in] control is the map control.
302 * @param[in] fs is the file system data.
303 * @param[in]  buffer is a pointer to the buffer handle the map is
304 *           stored in.
305 * @param[in] size is the number of bits in the map.
306 *
307 * @retval 0 Successful operation.
308 * @retval error_code An error occurred.
309 */
310int rtems_rfs_bitmap_open (rtems_rfs_bitmap_control* control,
311                           rtems_rfs_file_system*    fs,
312                           rtems_rfs_buffer_handle*  buffer,
313                           size_t                    size,
314                           rtems_rfs_buffer_block    block);
315
316/**
317 * Close a bitmap.
318 *
319 * @param[in] control is the bit map control.
320 *
321 * @retval 0 Successful operation.
322 * @retval error_code An error occurred.
323 */
324int rtems_rfs_bitmap_close (rtems_rfs_bitmap_control* control);
325
326#endif
Note: See TracBrowser for help on using the repository browser.