Changeset 38542dc in mainline for uspace/lib/ext4/libext4_bitmap.c
- Timestamp:
- 2012-08-12T18:36:10Z (12 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 49505fe
- Parents:
- b08e7970
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/ext4/libext4_bitmap.c
rb08e7970 r38542dc 29 29 /** @addtogroup libext4 30 30 * @{ 31 */ 32 31 */ 33 32 /** 34 * @file 35 * @brief 33 * @file libext4_bitmap.c 34 * @brief Ext4 bitmap operations. 36 35 */ 37 36 … … 45 44 * Index must be checked by caller, if it's not out of bounds. 46 45 * 47 * @param bitmap pointer to bitmap 48 * @param index index of bit in bitmap 46 * @param bitmap Pointer to bitmap 47 * @param index Index of bit in bitmap 48 * 49 49 */ 50 50 void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index) … … 52 52 uint32_t byte_index = index / 8; 53 53 uint32_t bit_index = index % 8; 54 54 55 55 uint8_t *target = bitmap + byte_index; 56 56 57 57 *target &= ~ (1 << bit_index); 58 58 } … … 62 62 * Index and count must be checked by caller, if they aren't out of bounds. 63 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 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 * 67 68 */ 68 69 void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count) … … 72 73 uint32_t remaining = count; 73 74 uint32_t byte_index; 74 75 75 76 /* Align index to multiple of 8 */ 76 77 while (((idx % 8) != 0) && (remaining > 0)) { 77 78 78 byte_index = idx / 8; 79 79 uint32_t bit_index = idx % 8; 80 80 81 81 target = bitmap + byte_index; 82 83 82 *target &= ~ (1 << bit_index); 84 83 85 84 idx++; 86 85 remaining--; 87 86 } 88 87 89 88 /* For < 8 bits this check necessary */ 90 if (remaining == 0) {89 if (remaining == 0) 91 90 return; 92 } 93 91 94 92 assert((idx % 8) == 0); 95 93 96 94 byte_index = idx / 8; 97 95 target = bitmap + byte_index; 98 96 99 97 /* Zero the whole bytes */ 100 98 while (remaining >= 8) { 101 99 *target = 0; 102 100 103 101 idx += 8; 104 102 remaining -= 8; 105 103 target++; 106 104 } 107 105 108 106 assert(remaining < 8); 109 107 110 108 /* Zero remaining bytes */ 111 109 while (remaining != 0) { 112 113 110 byte_index = idx / 8; 114 111 uint32_t bit_index = idx % 8; 115 112 116 113 target = bitmap + byte_index; 117 118 114 *target &= ~ (1 << bit_index); 119 115 120 116 idx++; 121 117 remaining--; … … 125 121 /** Set bit in bitmap to 1 (used). 126 122 * 127 * @param bitmap pointer to bitmap 128 * @param index index of bit to set 123 * @param bitmap Pointer to bitmap 124 * @param index Index of bit to set 125 * 129 126 */ 130 127 void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index) … … 132 129 uint32_t byte_index = index / 8; 133 130 uint32_t bit_index = index % 8; 134 131 135 132 uint8_t *target = bitmap + byte_index; 136 133 137 134 *target |= 1 << bit_index; 138 135 } … … 140 137 /** Check if requested bit is free. 141 138 * 142 * @param bitmap pointer to bitmap 143 * @param index index of bit to be checked 144 * @return true if bit is free, else false 139 * @param bitmap Pointer to bitmap 140 * @param index Index of bit to be checked 141 * 142 * @return True if bit is free, else false 143 * 145 144 */ 146 145 bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index) … … 148 147 uint32_t byte_index = index / 8; 149 148 uint32_t bit_index = index % 8; 150 149 151 150 uint8_t *target = bitmap + byte_index; 152 153 if (*target & (1 << bit_index)) {151 152 if (*target & (1 << bit_index)) 154 153 return false; 155 } else {154 else 156 155 return true; 157 } 158 159 } 160 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). 156 } 157 158 /** Try to find free byte and set the first bit as used. 159 * 160 * Walk through bitmap and try to find free byte (equal to 0). 164 161 * If byte found, set the first bit as used. 165 162 * 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 */ 172 int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max) 163 * @param bitmap Pointer to bitmap 164 * @param start Index of bit, where the algorithm will begin 165 * @param index Output value - index of bit (if found free byte) 166 * @param max Maximum index of bit in bitmap 167 * 168 * @return Error code 169 * 170 */ 171 int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, 172 uint32_t *index, uint32_t max) 173 173 { 174 174 uint32_t idx; 175 175 176 176 /* Align idx */ 177 if (start % 8) {177 if (start % 8) 178 178 idx = start + (8 - (start % 8)); 179 } else {179 else 180 180 idx = start; 181 } 182 181 183 182 uint8_t *pos = bitmap + (idx / 8); 184 183 185 184 /* Try to find free byte */ 186 185 while (idx < max) { 187 188 186 if (*pos == 0) { 189 187 *pos |= 1; 190 188 191 189 *index = idx; 192 190 return EOK; 193 191 } 194 192 195 193 idx += 8; 196 194 ++pos; 197 195 } 198 196 199 197 /* Free byte not found */ 200 198 return ENOSPC; … … 205 203 * Walk through bitmap and try to find any free bit. 206 204 * 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 205 * @param bitmap Pointer to bitmap 206 * @param start_idx Index of bit, where algorithm will begin 207 * @param index Output value - index of set bit (if found) 208 * @param max Maximum index of bit in bitmap 209 * 210 * @return Error code 211 * 212 212 */ 213 213 int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx, 214 214 uint32_t *index, uint32_t max) 215 215 { 216 216 uint8_t *pos = bitmap + (start_idx / 8); 217 217 uint32_t idx = start_idx; 218 218 bool byte_part = false; 219 219 220 220 /* Check the rest of first byte */ 221 221 while ((idx % 8) != 0) { 222 222 byte_part = true; 223 223 224 224 if ((*pos & (1 << (idx % 8))) == 0) { 225 225 *pos |= (1 << (idx % 8)); … … 227 227 return EOK; 228 228 } 229 229 230 230 ++idx; 231 231 } 232 233 if (byte_part) {232 233 if (byte_part) 234 234 ++pos; 235 } 236 235 237 236 /* Check the whole bytes (255 = 11111111 binary) */ 238 237 while (idx < max) { 239 240 238 if ((*pos & 255) != 255) { 241 /* free bit found */239 /* Free bit found */ 242 240 break; 243 241 } 244 242 245 243 idx += 8; 246 244 ++pos; 247 245 } 248 246 249 247 /* If idx < max, some free bit found */ 250 248 if (idx < max) { 251 252 249 /* Check which bit from byte is free */ 253 250 for (uint8_t i = 0; i < 8; ++i) { 254 251 if ((*pos & (1 << i)) == 0) { 255 /* free bit found */ 256 *pos |= (1 << i); 252 /* Free bit found */ 253 *pos |= (1 << i); 254 257 255 *index = idx; 258 256 return EOK; 259 257 } 258 260 259 idx++; 261 260 } 262 261 } 263 262 264 263 /* Free bit not found */ 265 264 return ENOSPC; … … 268 267 /** 269 268 * @} 270 */ 269 */
Note:
See TracChangeset
for help on using the changeset viewer.