Add comments in ch6

This commit is contained in:
hypocrasy 2022-04-30 16:04:58 +08:00
parent cea2febe35
commit f9346edad1
35 changed files with 374 additions and 116 deletions

View file

@ -3,11 +3,15 @@ use alloc::collections::VecDeque;
use alloc::sync::Arc;
use lazy_static::*;
use spin::Mutex;
/// Cached block inside memory
pub struct BlockCache {
/// cached block data
cache: [u8; BLOCK_SZ],
/// underlying block id
block_id: usize,
/// underlying block device
block_device: Arc<dyn BlockDevice>,
/// whether the block is dirty
modified: bool,
}
@ -23,7 +27,7 @@ impl BlockCache {
modified: false,
}
}
/// Get the address of an offset inside the cached block data
fn addr_of_offset(&self, offset: usize) -> usize {
&self.cache[offset] as *const _ as usize
}
@ -70,7 +74,7 @@ impl Drop for BlockCache {
self.sync()
}
}
/// Use a block cache of 16 blocks
const BLOCK_CACHE_SIZE: usize = 16;
pub struct BlockCacheManager {
@ -118,10 +122,11 @@ impl BlockCacheManager {
}
lazy_static! {
/// The global block cache manager
pub static ref BLOCK_CACHE_MANAGER: Mutex<BlockCacheManager> =
Mutex::new(BlockCacheManager::new());
}
/// Get the block cache corresponding to the given block id and block device
pub fn get_block_cache(
block_id: usize,
block_device: Arc<dyn BlockDevice>,
@ -130,7 +135,7 @@ pub fn get_block_cache(
.lock()
.get_block_cache(block_id, block_device)
}
/// Sync all block cache to block device
pub fn block_cache_sync_all() {
let manager = BLOCK_CACHE_MANAGER.lock();
for (_, cache) in manager.queue.iter() {