Changeset 38542dc in mainline for uspace/lib/ext4/libext4_bitmap.c


Ignore:
Timestamp:
2012-08-12T18:36:10Z (12 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
49505fe
Parents:
b08e7970
Message:

ext4 code review and coding style cleanup

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/ext4/libext4_bitmap.c

    rb08e7970 r38542dc  
    2929/** @addtogroup libext4
    3030 * @{
    31  */
    32 
     31 */
    3332/**
    34  * @file        libext4_bitmap.c
    35  * @brief       Ext4 bitmap operations.
     33 * @file  libext4_bitmap.c
     34 * @brief Ext4 bitmap operations.
    3635 */
    3736
     
    4544 * Index must be checked by caller, if it's not out of bounds.
    4645 *
    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 *
    4949 */
    5050void ext4_bitmap_free_bit(uint8_t *bitmap, uint32_t index)
     
    5252        uint32_t byte_index = index / 8;
    5353        uint32_t bit_index = index % 8;
    54 
     54       
    5555        uint8_t *target = bitmap + byte_index;
    56 
     56       
    5757        *target &= ~ (1 << bit_index);
    5858}
     
    6262 * Index and count must be checked by caller, if they aren't out of bounds.
    6363 *
    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 *
    6768 */
    6869void ext4_bitmap_free_bits(uint8_t *bitmap, uint32_t index, uint32_t count)
     
    7273        uint32_t remaining = count;
    7374        uint32_t byte_index;
    74 
     75       
    7576        /* Align index to multiple of 8 */
    7677        while (((idx % 8) != 0) && (remaining > 0)) {
    77 
    7878                byte_index = idx / 8;
    7979                uint32_t bit_index = idx % 8;
    80 
     80               
    8181                target = bitmap + byte_index;
    82 
    8382                *target &= ~ (1 << bit_index);
    84 
     83               
    8584                idx++;
    8685                remaining--;
    8786        }
    88 
     87       
    8988        /* For < 8 bits this check necessary */
    90         if (remaining == 0) {
     89        if (remaining == 0)
    9190                return;
    92         }
    93 
     91       
    9492        assert((idx % 8) == 0);
    95 
     93       
    9694        byte_index = idx / 8;
    9795        target = bitmap + byte_index;
    98 
     96       
    9997        /* Zero the whole bytes */
    10098        while (remaining >= 8) {
    10199                *target = 0;
    102 
     100               
    103101                idx += 8;
    104102                remaining -= 8;
    105103                target++;
    106104        }
    107 
     105       
    108106        assert(remaining < 8);
    109 
     107       
    110108        /* Zero remaining bytes */
    111109        while (remaining != 0) {
    112 
    113110                byte_index = idx / 8;
    114111                uint32_t bit_index = idx % 8;
    115 
     112               
    116113                target = bitmap + byte_index;
    117 
    118114                *target &= ~ (1 << bit_index);
    119 
     115               
    120116                idx++;
    121117                remaining--;
     
    125121/** Set bit in bitmap to 1 (used).
    126122 *
    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 *
    129126 */
    130127void ext4_bitmap_set_bit(uint8_t *bitmap, uint32_t index)
     
    132129        uint32_t byte_index = index / 8;
    133130        uint32_t bit_index = index % 8;
    134 
     131       
    135132        uint8_t *target = bitmap + byte_index;
    136 
     133       
    137134        *target |= 1 << bit_index;
    138135}
     
    140137/** Check if requested bit is free.
    141138 *
    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 *
    145144 */
    146145bool ext4_bitmap_is_free_bit(uint8_t *bitmap, uint32_t index)
     
    148147        uint32_t byte_index = index / 8;
    149148        uint32_t bit_index = index % 8;
    150 
     149       
    151150        uint8_t *target = bitmap + byte_index;
    152 
    153         if (*target & (1 << bit_index)) {
     151       
     152        if (*target & (1 << bit_index))
    154153                return false;
    155         } else {
     154        else
    156155                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).
    164161 * If byte found, set the first bit as used.
    165162 *
    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 */
     171int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start,
     172    uint32_t *index, uint32_t max)
    173173{
    174174        uint32_t idx;
    175 
     175       
    176176        /* Align idx */
    177         if (start % 8) {
     177        if (start % 8)
    178178                idx = start + (8 - (start % 8));
    179         } else {
     179        else
    180180                idx = start;
    181         }
    182 
     181       
    183182        uint8_t *pos = bitmap + (idx / 8);
    184 
     183       
    185184        /* Try to find free byte */
    186185        while (idx < max) {
    187 
    188186                if (*pos == 0) {
    189187                        *pos |= 1;
    190 
     188                       
    191189                        *index = idx;
    192190                        return EOK;
    193191                }
    194 
     192               
    195193                idx += 8;
    196194                ++pos;
    197195        }
    198 
     196       
    199197        /* Free byte not found */
    200198        return ENOSPC;
     
    205203 * Walk through bitmap and try to find any free bit.
    206204 *
    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 *
    212212 */
    213213int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
    214                 uint32_t *index, uint32_t max)
     214    uint32_t *index, uint32_t max)
    215215{
    216216        uint8_t *pos = bitmap + (start_idx / 8);
    217217        uint32_t idx = start_idx;
    218218        bool byte_part = false;
    219 
     219       
    220220        /* Check the rest of first byte */
    221221        while ((idx % 8) != 0) {
    222222                byte_part = true;
    223 
     223               
    224224                if ((*pos & (1 << (idx % 8))) == 0) {
    225225                        *pos |= (1 << (idx % 8));
     
    227227                        return EOK;
    228228                }
    229 
     229               
    230230                ++idx;
    231231        }
    232 
    233         if (byte_part) {
     232       
     233        if (byte_part)
    234234                ++pos;
    235         }
    236 
     235       
    237236        /* Check the whole bytes (255 = 11111111 binary) */
    238237        while (idx < max) {
    239 
    240238                if ((*pos & 255) != 255) {
    241                         /* free bit found */
     239                        /* Free bit found */
    242240                        break;
    243241                }
    244 
     242               
    245243                idx += 8;
    246244                ++pos;
    247245        }
    248 
     246       
    249247        /* If idx < max, some free bit found */
    250248        if (idx < max) {
    251 
    252249                /* Check which bit from byte is free */
    253250                for (uint8_t i = 0; i < 8; ++i) {
    254251                        if ((*pos & (1 << i)) == 0) {
    255                                 /* free bit found */
    256                                 *pos |=  (1 << i);
     252                                /* Free bit found */
     253                                *pos |= (1 << i);
     254                               
    257255                                *index = idx;
    258256                                return EOK;
    259257                        }
     258                       
    260259                        idx++;
    261260                }
    262261        }
    263 
     262       
    264263        /* Free bit not found */
    265264        return ENOSPC;
     
    268267/**
    269268 * @}
    270  */ 
     269 */
Note: See TracChangeset for help on using the changeset viewer.