summaryrefslogtreecommitdiff
path: root/src/disk/bitmap.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/disk/bitmap.rs')
-rw-r--r--src/disk/bitmap.rs30
1 files changed, 12 insertions, 18 deletions
diff --git a/src/disk/bitmap.rs b/src/disk/bitmap.rs
index 64389c2..b68c341 100644
--- a/src/disk/bitmap.rs
+++ b/src/disk/bitmap.rs
@@ -1,5 +1,4 @@
use crate::block_device::{BlockDevice, BLOCK_SIZE};
-use std::cell::RefCell;
use std::sync::Arc;
pub struct Bitmap {
@@ -10,7 +9,7 @@ pub struct Bitmap {
}
impl Bitmap {
- pub fn new(starting_block: usize, length: usize, device: Arc<dyn BlockDevice>) -> Self {
+ pub(crate) fn new(starting_block: usize, length: usize, device: Arc<dyn BlockDevice>) -> Self {
Self {
starting_block,
length,
@@ -18,8 +17,7 @@ impl Bitmap {
data: vec![0u8; length * BLOCK_SIZE],
}
}
- pub fn allocate(&mut self) -> Option<usize> {
- // let mut data = self.data.borrow_mut();
+ pub(crate) fn allocate(&mut self) -> Option<usize> {
for (i, byte) in self.data.iter_mut().enumerate() {
let leading_ones = byte.leading_ones();
if leading_ones != 8 {
@@ -28,11 +26,9 @@ impl Bitmap {
}
}
None
- // panic!("No more space for allocation!")
}
- pub fn query(&self, index: usize) -> bool {
- // let data = self.data.borrow();
+ pub(crate) fn query(&self, index: usize) -> bool {
if index == 0 {
false
} else {
@@ -40,15 +36,13 @@ impl Bitmap {
}
}
- // fn write_back(&mut self) {
- // for block_index_offset in self.dirty_blocks.iter() {
- // let buffer_front_index = BLOCK_SIZE * block_index_offset;
- // let buffer_back_index = BLOCK_SIZE * (block_index_offset + 1);
- // self.device.write(
- // self.starting_block + block_index_offset,
- // &self.data[buffer_front_index..buffer_back_index],
- // );
- // }
- // self.dirty_blocks = Vec::new();
- // }
+ pub(crate) fn deallocate(&mut self, index: usize) -> bool {
+ if self.query(index) {
+ let mask = !(1u8 << (7 - index % 8));
+ self.data[index / 8] &= mask;
+ true
+ } else {
+ false
+ }
+ }
}