Replace VmFrame::zero with VmWriter::fill

This commit is contained in:
LI Qing 2024-01-31 15:43:32 +09:00 committed by Tate, Hongliang Tian
parent 195c6a0739
commit 12d338dd9b
4 changed files with 23 additions and 22 deletions

View File

@ -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<T: Pod>(&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> {

View File

@ -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)

View File

@ -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 _);

View File

@ -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(())
}