From b8afa7cfb02b32278e268924e189170496f81c1b Mon Sep 17 00:00:00 2001 From: Chuyan Zhang Date: Thu, 23 Nov 2023 02:11:16 -0800 Subject: Add some callback (not finished) --- src/memory/cached_block.rs | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) (limited to 'src/memory/cached_block.rs') diff --git a/src/memory/cached_block.rs b/src/memory/cached_block.rs index 9e266ef..eb59a4b 100644 --- a/src/memory/cached_block.rs +++ b/src/memory/cached_block.rs @@ -2,6 +2,7 @@ use crate::block_device::{BlockDevice, BLOCK_SIZE}; use crate::disk::block::Block; use crate::AyaFS; use and_then_some::BoolExt; +use log::debug; use lru::LruCache; use std::num::NonZeroUsize; use std::sync::Arc; @@ -46,11 +47,9 @@ impl BlockCache { pub(crate) fn load_block(&mut self, index: usize) -> bool { if self.cache.contains(&index) == false { - let block = T::default(); - let buffer = unsafe { - std::slice::from_raw_parts_mut(&block as *const T as *mut u8, BLOCK_SIZE) - }; - self.device.read(index, buffer); + let mut buffer = [0u8; BLOCK_SIZE]; + self.device.read(index, &mut buffer); + let block: T = unsafe { std::mem::transmute_copy(&buffer) }; let cached_block = CachedBlock { block, index, @@ -77,8 +76,11 @@ impl BlockCache { /// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载. pub(crate) fn get_block_mut(&mut self, index: usize) -> Option<&mut CachedBlock> { - self.load_block(index) - .and_then(|| self.cache.get_mut(&index).map(convert_mut::)) + debug!("Blockcache get block mut"); + self.load_block(index).and_then(|| { + debug!("block loaded"); + self.cache.get_mut(&index).map(convert_mut::) + }) } /// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载. -- cgit v1.2.3-70-g09d2