Add comments in ch6
This commit is contained in:
parent
cea2febe35
commit
f9346edad1
35 changed files with 374 additions and 116 deletions
|
@ -1,16 +1,16 @@
|
|||
use super::{get_block_cache, BlockDevice, BLOCK_SZ};
|
||||
use alloc::sync::Arc;
|
||||
|
||||
/// A bitmap block
|
||||
type BitmapBlock = [u64; 64];
|
||||
|
||||
/// Number of bits in a block
|
||||
const BLOCK_BITS: usize = BLOCK_SZ * 8;
|
||||
|
||||
/// A bitmap
|
||||
pub struct Bitmap {
|
||||
start_block_id: usize,
|
||||
blocks: usize,
|
||||
}
|
||||
|
||||
/// Return (block_pos, bits64_pos, inner_pos)
|
||||
/// Decompose bits into (block_pos, bits64_pos, inner_pos)
|
||||
fn decomposition(mut bit: usize) -> (usize, usize, usize) {
|
||||
let block_pos = bit / BLOCK_BITS;
|
||||
bit %= BLOCK_BITS;
|
||||
|
@ -18,13 +18,14 @@ fn decomposition(mut bit: usize) -> (usize, usize, usize) {
|
|||
}
|
||||
|
||||
impl Bitmap {
|
||||
/// A new bitmap from start block id and number of blocks
|
||||
pub fn new(start_block_id: usize, blocks: usize) -> Self {
|
||||
Self {
|
||||
start_block_id,
|
||||
blocks,
|
||||
}
|
||||
}
|
||||
|
||||
/// Allocate a new block from a block device
|
||||
pub fn alloc(&self, block_device: &Arc<dyn BlockDevice>) -> Option<usize> {
|
||||
for block_id in 0..self.blocks {
|
||||
let pos = get_block_cache(
|
||||
|
@ -52,7 +53,7 @@ impl Bitmap {
|
|||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Deallocate a block
|
||||
pub fn dealloc(&self, block_device: &Arc<dyn BlockDevice>, bit: usize) {
|
||||
let (block_pos, bits64_pos, inner_pos) = decomposition(bit);
|
||||
get_block_cache(block_pos + self.start_block_id, Arc::clone(block_device))
|
||||
|
@ -62,7 +63,7 @@ impl Bitmap {
|
|||
bitmap_block[bits64_pos] -= 1u64 << inner_pos;
|
||||
});
|
||||
}
|
||||
|
||||
/// Get the max number of allocatable blocks
|
||||
pub fn maximum(&self) -> usize {
|
||||
self.blocks * BLOCK_BITS
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue