Skip to content

Commit

Permalink
Fix block bitmap utilisation (#637)
Browse files Browse the repository at this point in the history
  • Loading branch information
vinc authored Jun 6, 2024
1 parent 0806ba6 commit 73ba882
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions src/sys/fs/bitmap_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,46 @@ pub const BITMAP_SIZE: usize = 8 * super::BLOCK_SIZE;
pub struct BitmapBlock {}

impl BitmapBlock {
fn block_index(addr: u32) -> u32 {
fn indexes(addr: u32) -> (u32, usize) {
let sb = SuperBlock::read();
let size = sb.block_size();
let i = addr - sb.data_area();
sb.bitmap_area() + (i / size / 8)
}

fn buffer_index(addr: u32) -> usize {
let sb = SuperBlock::read();
let i = (addr - sb.data_area()) as usize;
i % sb.block_size() as usize
let n = sb.block_size();
(sb.bitmap_area() + (i / n / 8), (i % (n * 8)) as usize)
}

pub fn alloc(addr: u32) {
let mut block = Block::read(BitmapBlock::block_index(addr));
let (a, i) = Self::indexes(addr);
let mut block = Block::read(a);
let bitmap = block.data_mut();
let i = BitmapBlock::buffer_index(addr);
if !bitmap[i / 8].get_bit(i % 8) {
bitmap[i / 8].set_bit(i % 8, true);
block.write();
super_block::inc_alloc_count();
} else {
// TODO: alloc failed
}
}

pub fn free(addr: u32) {
let mut block = Block::read(BitmapBlock::block_index(addr));
let (a, i) = Self::indexes(addr);
let mut block = Block::read(a);
let bitmap = block.data_mut();
let i = BitmapBlock::buffer_index(addr);
bitmap[i / 8].set_bit(i % 8, false);
block.write();
super_block::dec_alloc_count();
}

pub fn next_free_addr() -> Option<u32> {
let sb = SuperBlock::read();
let size = sb.block_size();
let n = sb.block_count() / size / 8;
for i in 0..n {
let n = sb.block_size();
let m = sb.block_count() / n / 8;
for i in 0..m {
let block = Block::read(sb.bitmap_area() + i);
let bitmap = block.data();
let m = size / 8;
for j in 0..m {
for j in 0..n {
for k in 0..8 {
if !bitmap[j as usize].get_bit(k) {
let bs = BITMAP_SIZE as u32;
let addr = sb.data_area() + i * bs + j * 8 + k as u32;
if !bitmap[j as usize].get_bit(k as usize) {
let addr = sb.data_area() + (i * n * 8) + (j * 8) + k;
return Some(addr);
}
}
Expand Down

0 comments on commit 73ba882

Please sign in to comment.