Changeset b12ca16 in mainline for uspace/lib/ext4/libext4_bitmap.c


Ignore:
Timestamp:
2011-11-18T15:30:24Z (13 years ago)
Author:
Frantisek Princ <frantisek.princ@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
d5ba17f
Parents:
e18de3c
Message:

improved block allocator - but has some bugs

File:
1 edited

Legend:

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

    re18de3c rb12ca16  
    7676}
    7777
    78 int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
     78int ext4_bitmap_find_free_byte_and_set_bit(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t max)
    7979{
    80         uint8_t *pos = bitmap + (start / 8) + 1;
     80        uint32_t idx;
     81        if (start % 8) {
     82                idx = start + (8 - (start % 8));
     83        } else {
     84                idx = start;
     85        }
    8186
    82         while (pos < bitmap + size) {
     87        uint8_t *pos = bitmap + (idx / 8);
     88
     89        while (idx < max) {
     90
    8391                if (*pos == 0) {
    8492                        *pos |= 1;
    8593
    86                         *index = (pos - bitmap) * 8;
     94                        *index = idx;
    8795                        return EOK;
    8896                }
    8997
     98                idx += 8;
    9099                ++pos;
    91100        }
    92101
    93102        return ENOSPC;
    94 
    95103}
    96104
    97 int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start, uint32_t *index, uint32_t size)
     105int ext4_bitmap_find_free_bit_and_set(uint8_t *bitmap, uint32_t start_idx,
     106                uint32_t *index, uint32_t max)
    98107{
    99         uint8_t *pos = bitmap + (start / 8);
    100         int i;
    101         uint8_t value, new_value;
     108        uint8_t *pos = bitmap + (start_idx / 8);
     109        uint32_t idx = start_idx;
     110        bool byte_part = false;
    102111
    103         while (pos < bitmap + size) {
     112        // Check the rest of byte
     113        while ((idx % 8) != 0) {
     114                byte_part = true;
     115
     116                if (*pos & (1 << (idx % 8))) {
     117                        *pos |= (1 << (idx % 8));
     118                        *index = idx;
     119                        return EOK;
     120                }
     121
     122                ++idx;
     123        }
     124
     125        if (byte_part) {
     126                ++pos;
     127        }
     128
     129        while (idx < max) {
     130
    104131                if ((*pos & 255) != 255) {
    105132                        // free bit found
     
    107134                }
    108135
     136                idx += 8;
    109137                ++pos;
    110138        }
    111139
    112         // Check the byte containing start
    113         if (pos == bitmap + (start / 8)) {
    114                 for (i = start % 8; i < 8; ++i) {
    115                         value = *pos;
    116                         if ((value & (1 << i)) == 0) {
     140
     141        if (idx < max) {
     142                for (uint8_t i = 0; i < 8; ++i) {
     143                        if ((*pos & (1 << i)) == 0) {
    117144                                // free bit found
    118                                 new_value = value | (1 << i);
    119                                 *pos = new_value;
    120                                 *index = (pos - bitmap) * 8 + i;
     145                                *pos |=  (1 << i);
     146                                *index = idx;
    121147                                return EOK;
    122148                        }
    123                 }
    124         }
    125 
    126         if (pos < bitmap + size) {
    127 
    128                 for(i = 0; i < 8; ++i) {
    129                         value = *pos;
    130 
    131                         if ((value & (1 << i)) == 0) {
    132                                 // free bit found
    133                                 new_value = value | (1 << i);
    134                                 *pos = new_value;
    135                                 *index = (pos - bitmap) * 8 + i;
    136                                 return EOK;
    137                         }
     149                        idx++;
    138150                }
    139151        }
Note: See TracChangeset for help on using the changeset viewer.