diff options
Diffstat (limited to 'ayafs/src/disk/bitmap.rs')
-rw-r--r-- | ayafs/src/disk/bitmap.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/ayafs/src/disk/bitmap.rs b/ayafs/src/disk/bitmap.rs new file mode 100644 index 0000000..b68c341 --- /dev/null +++ b/ayafs/src/disk/bitmap.rs @@ -0,0 +1,48 @@ +use crate::block_device::{BlockDevice, BLOCK_SIZE}; +use std::sync::Arc; + +pub struct Bitmap { + pub starting_block: usize, + pub length: usize, + pub device: Arc<dyn BlockDevice>, + pub data: Vec<u8>, +} + +impl Bitmap { + pub(crate) fn new(starting_block: usize, length: usize, device: Arc<dyn BlockDevice>) -> Self { + Self { + starting_block, + length, + device, + data: vec![0u8; length * BLOCK_SIZE], + } + } + 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 { + *byte |= (1 << (7 - leading_ones)) as u8; + return Some(i * 8 + leading_ones as usize); + } + } + None + } + + pub(crate) fn query(&self, index: usize) -> bool { + if index == 0 { + false + } else { + self.data[index / 8] & ((1 << (7 - index % 8)) as u8) != 0 + } + } + + 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 + } + } +} |