From 12d338dd9ba7cfba536e7fd6bf3e713ff72a6f10 Mon Sep 17 00:00:00 2001 From: LI Qing Date: Wed, 31 Jan 2024 15:43:32 +0900 Subject: [PATCH] Replace `VmFrame::zero` with `VmWriter::fill` --- framework/aster-frame/src/vm/frame.rs | 33 ++++++++++---------- framework/aster-frame/src/vm/options.rs | 8 +++-- services/libs/aster-std/src/fs/ext2/inode.rs | 2 +- services/libs/aster-std/src/fs/ramfs/fs.rs | 2 +- 4 files changed, 23 insertions(+), 22 deletions(-) diff --git a/framework/aster-frame/src/vm/frame.rs b/framework/aster-frame/src/vm/frame.rs index abd5bcebd..6dc7147c0 100644 --- a/framework/aster-frame/src/vm/frame.rs +++ b/framework/aster-frame/src/vm/frame.rs @@ -6,6 +6,7 @@ use core::{ marker::PhantomData, ops::{BitAnd, BitOr, Not, Range}, }; +use pod::Pod; use crate::{config::PAGE_SIZE, prelude::*, Error}; @@ -57,11 +58,6 @@ impl VmFrameVec { Ok(()) } - /// zero all internal vm frames - pub fn zero(&self) { - self.0.iter().for_each(|vm_frame| vm_frame.zero()) - } - /// Truncate some frames. /// /// If `new_len >= self.len()`, then this method has no effect. @@ -228,12 +224,6 @@ impl VmFrame { (self.frame_index() + 1) * PAGE_SIZE } - /// Fills the frame with zero. - pub fn zero(&self) { - // Safety: The range of memory is valid for writes of one page data. - unsafe { core::ptr::write_bytes(self.as_mut_ptr(), 0, PAGE_SIZE) } - } - fn need_dealloc(&self) -> bool { (*self.frame_index & VmFrameFlags::NEED_DEALLOC.bits()) != 0 } @@ -418,12 +408,6 @@ impl VmSegment { self.nframes() * PAGE_SIZE } - /// Fills the page frames with zero. - pub fn zero(&self) { - // Safety: The range of memory is valid for writes of `self.nbytes()` data. - unsafe { core::ptr::write_bytes(self.as_mut_ptr(), 0, self.nbytes()) } - } - fn need_dealloc(&self) -> bool { (self.inner.start_frame_index & VmFrameFlags::NEED_DEALLOC.bits()) != 0 } @@ -700,6 +684,21 @@ impl<'a> VmWriter<'a> { } copy_len } + + /// Fills the available space by repeating `value`. + /// + /// # Panic + /// + /// The size of the available space must be a multiple of the size of `value`. + /// Otherwise, the method would panic. + pub fn fill(&mut self, value: T) { + assert!(self.avail() / value.as_bytes().len() > 0); + assert!(self.avail() % value.as_bytes().len() == 0); + + while self.avail() > 0 { + self.write(&mut value.as_bytes().into()); + } + } } impl<'a> From<&'a mut [u8]> for VmWriter<'a> { diff --git a/framework/aster-frame/src/vm/options.rs b/framework/aster-frame/src/vm/options.rs index 67c5c9986..f88e71350 100644 --- a/framework/aster-frame/src/vm/options.rs +++ b/framework/aster-frame/src/vm/options.rs @@ -59,7 +59,9 @@ impl VmAllocOptions { VmFrameVec(frame_list) }; if !self.uninit { - frames.zero(); + for frame in frames.iter() { + frame.writer().fill(0); + } } Ok(frames) @@ -73,7 +75,7 @@ impl VmAllocOptions { let frame = frame_allocator::alloc_single(self.flags()).ok_or(Error::NoMemory)?; if !self.uninit { - frame.zero(); + frame.writer().fill(0); } Ok(frame) @@ -90,7 +92,7 @@ impl VmAllocOptions { let segment = frame_allocator::alloc_contiguous(self.nframes, self.flags()).ok_or(Error::NoMemory)?; if !self.uninit { - segment.zero(); + segment.writer().fill(0); } Ok(segment) diff --git a/services/libs/aster-std/src/fs/ext2/inode.rs b/services/libs/aster-std/src/fs/ext2/inode.rs index e5a638476..9ce7f1b57 100644 --- a/services/libs/aster-std/src/fs/ext2/inode.rs +++ b/services/libs/aster-std/src/fs/ext2/inode.rs @@ -845,7 +845,7 @@ impl InodeImpl_ { debug_assert!(field::DIRECT.contains(&(bid as usize))); if self.blocks_hole_desc.is_hole(bid as usize) { - block.zero(); + block.writer().fill(0); return Ok(()); } let device_bid = Bid::new(self.desc.data[bid as usize] as _); diff --git a/services/libs/aster-std/src/fs/ramfs/fs.rs b/services/libs/aster-std/src/fs/ramfs/fs.rs index 238f6e319..f3c2df22a 100644 --- a/services/libs/aster-std/src/fs/ramfs/fs.rs +++ b/services/libs/aster-std/src/fs/ramfs/fs.rs @@ -414,7 +414,7 @@ impl RamInode { impl PageCacheBackend for RamInode { fn read_page(&self, _idx: usize, frame: &VmFrame) -> Result<()> { // Initially, any block/page in a RamFs inode contains all zeros - frame.zero(); + frame.writer().fill(0); Ok(()) }