source: mainline/uspace/lib/ext4/libext4_bitmap.c@ 2add9ec

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 2add9ec was f22d5ef0, checked in by Frantisek Princ <frantisek.princ@…>, 13 years ago

Copyright update

  • Property mode set to 100644
File size: 5.9 KB
RevLine 
[052e82d]1/*
[f22d5ef0]2 * Copyright (c) 2012 Frantisek Princ
[052e82d]3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup libext4
30 * @{
31 */
32
33/**
34 * @file libext4_bitmap.c
[2674db6]35 * @brief Ext4 bitmap operations.
[052e82d]36 */
37
38#include <errno.h>
39#include <libblock.h>
40#include <sys/types.h>
41#include "libext4.h"
42
[7b16549]43/** Set bit in bitmap to 0 (free).
44 *
45 * Index must be checked by caller, if it's not out of bounds.
46 *
47 * @param bitmap pointer to bitmap
48 * @param index index of bit in bitmap
49 */
[2674db6]50void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
[052e82d]51{
52 uint32_t byte_index = index / 8;
53 uint32_t bit_index = index % 8;
54
[2674db6]55 uint8_t *target = bitmap + byte_index;
56
57 *target &= ~ (1 << bit_index);
58}
59
[7b16549]60/** Free continous set of bits (set to 0).
61 *
62 * Index and count must be checked by caller, if they aren't out of bounds.
63 *
64 * @param bitmap pointer to bitmap
65 * @param index index of first bit to zeroed
66 * @param count number of bits to be zeroed
67 */
[662bd71]68void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
69{
70 uint8_t *target;
71 uint32_t idx = index;
72 uint32_t remaining = count;
73 uint32_t byte_index;
74
[7b16549]75 // Align index to multiple of 8
[662bd71]76 while (((idx % 8) != 0) && (remaining > 0)) {
77
78 byte_index = idx / 8;
79 uint32_t bit_index = idx % 8;
80
81 target = bitmap + byte_index;
82
83 *target &= ~ (1 << bit_index);
84
85 idx++;
86 remaining--;
87 }
88
[7b16549]89 // For < 8 bits this check necessary
[e7ed26be]90 if (remaining == 0) {
91 return;
92 }
93
[662bd71]94 assert((idx % 8) == 0);
95
96 byte_index = idx / 8;
97 target = bitmap + byte_index;
98
[7b16549]99 // Zero the whole bytes
[662bd71]100 while (remaining >= 8) {
101 *target = 0;
102
103 idx += 8;
104 remaining -= 8;
105 target++;
106 }
107
108 assert(remaining < 8);
109
[7b16549]110 // Zero remaining bytes
[662bd71]111 while (remaining != 0) {
112
113 byte_index = idx / 8;
114 uint32_t bit_index = idx % 8;
115
116 target = bitmap + byte_index;
117
118 *target &= ~ (1 << bit_index);
119
120 idx++;
121 remaining--;
122 }
123}
124
[7b16549]125/** Set bit in bitmap to 1 (used).
126 *
127 * @param bitmap pointer to bitmap
128 * @param index index of bit to set
129 */
[2674db6]130void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
131{
132 uint32_t byte_index = index / 8;
133 uint32_t bit_index = index % 8;
134
135 uint8_t *target = bitmap + byte_index;
136
137 *target |= 1 << bit_index;
138}
139
[7b16549]140/** Check if requested bit is free.
141 *
142 * @param bitmap pointer to bitmap
143 * @param index index of bit to be checked
144 * @return true if bit is free, else false
145 */
[2674db6]146bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
147{
148 uint32_t byte_index = index / 8;
149 uint32_t bit_index = index % 8;
[1e65444]150
[052e82d]151 uint8_t *target = bitmap + byte_index;
152
[2674db6]153 if (*target & (1 << bit_index)) {
154 return false;
155 } else {
156 return true;
157 }
[052e82d]158
159}
160
[7b16549]161/** Try to find free byte and set the first bit as used.
162 *
163 * Walk through bitmap and try to find free byte ( == 0).
164 * If byte found, set the first bit as used.
165 *
166 * @param bitmap pointer to bitmap
167 * @param start index of bit, where the algorithm will begin
168 * @param index output value - index of bit (if found free byte)
169 * @param max maximum index of bit in bitmap
170 * @return error code
171 */
[b12ca16]172int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max)
[1c1c736]173{
[b12ca16]174 uint32_t idx;
[7b16549]175
176 // Align idx
[b12ca16]177 if (start % 8) {
178 idx = start + (8 - (start % 8));
179 } else {
180 idx = start;
181 }
182
183 uint8_t *pos = bitmap + (idx / 8);
184
[7b16549]185 // Try to find free byte
[b12ca16]186 while (idx < max) {
[2674db6]187
188 if (*pos == 0) {
189 *pos |= 1;
190
[b12ca16]191 *index = idx;
[2674db6]192 return EOK;
193 }
194
[b12ca16]195 idx += 8;
[2674db6]196 ++pos;
197 }
198
[7b16549]199 // Free byte not found
[2674db6]200 return ENOSPC;
201}
202
[7b16549]203/** Try to find free bit and set it as used (1).
204 *
205 * Walk through bitmap and try to find any free bit.
206 *
207 * @param bitmap pointer to bitmap
208 * @param start_idx index of bit, where algorithm will begin
209 * @param index output value - index of set bit (if found)
210 * @param max maximum index of bit in bitmap
211 * @return error code
212 */
[b12ca16]213int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
214 uint32_t *index, uint32_t max)
[2674db6]215{
[b12ca16]216 uint8_t *pos = bitmap + (start_idx / 8);
217 uint32_t idx = start_idx;
218 bool byte_part = false;
[1c1c736]219
[7b16549]220 // Check the rest of first byte
[b12ca16]221 while ((idx % 8) != 0) {
222 byte_part = true;
223
[528e5b3]224 if ((*pos & (1 << (idx % 8))) == 0) {
[b12ca16]225 *pos |= (1 << (idx % 8));
226 *index = idx;
227 return EOK;
[1c1c736]228 }
229
[b12ca16]230 ++idx;
231 }
232
233 if (byte_part) {
[1c1c736]234 ++pos;
235 }
236
[7b16549]237 // Check the whole bytes (255 = 11111111 binary)
[b12ca16]238 while (idx < max) {
239
240 if ((*pos & 255) != 255) {
241 // free bit found
242 break;
[2674db6]243 }
[1c1c736]244
[b12ca16]245 idx += 8;
246 ++pos;
247 }
[1e65444]248
[7b16549]249 // If idx < max, some free bit found
[b12ca16]250 if (idx < max) {
[7b16549]251
252 // Check which bit from byte is free
[b12ca16]253 for (uint8_t i = 0; i < 8; ++i) {
254 if ((*pos & (1 << i)) == 0) {
[1c1c736]255 // free bit found
[b12ca16]256 *pos |= (1 << i);
257 *index = idx;
[1c1c736]258 return EOK;
259 }
[b12ca16]260 idx++;
[1c1c736]261 }
262 }
263
[7b16549]264 // Free bit not found
[1c1c736]265 return ENOSPC;
266}
267
[052e82d]268/**
269 * @}
270 */
Note: See TracBrowser for help on using the repository browser.