Add uninit option for VmAllocOption

This commit is contained in:
Jianfeng Jiang
2023-06-01 16:21:39 +08:00
committed by Tate, Hongliang Tian
parent 7304e06c88
commit 7803d9037e
4 changed files with 30 additions and 19 deletions

View File

@ -33,18 +33,20 @@ impl VmFrameVec {
/// For more information, see `VmAllocOptions`. /// For more information, see `VmAllocOptions`.
pub fn allocate(options: &VmAllocOptions) -> Result<Self> { pub fn allocate(options: &VmAllocOptions) -> Result<Self> {
let page_size = options.page_size; let page_size = options.page_size;
let mut frame_list = Vec::new(); let frames = if options.is_contiguous {
if options.is_contiguous { frame_allocator::alloc_continuous(options.page_size).ok_or(Error::NoMemory)?
let frames = frame_allocator::alloc_continuous(options.page_size); } else {
if frames.is_none() { let mut frame_list = Vec::new();
return Err(Error::NoMemory); for _ in 0..page_size {
frame_list.push(frame_allocator::alloc().ok_or(Error::NoMemory)?);
} }
return Ok(Self(frames.unwrap())); frame_list
};
let frame_vec = Self(frames);
if !options.uninit {
frame_vec.zero();
} }
for i in 0..page_size { Ok(frame_vec)
frame_list.push(frame_allocator::alloc().unwrap());
}
Ok(Self(frame_list))
} }
pub fn get(&self, index: usize) -> Option<&VmFrame> { pub fn get(&self, index: usize) -> Option<&VmFrame> {
@ -201,6 +203,7 @@ impl<'a> Iterator for VmFrameVecIter<'a> {
pub struct VmAllocOptions { pub struct VmAllocOptions {
page_size: usize, page_size: usize,
is_contiguous: bool, is_contiguous: bool,
uninit: bool,
} }
impl VmAllocOptions { impl VmAllocOptions {
@ -209,6 +212,7 @@ impl VmAllocOptions {
Self { Self {
page_size: len, page_size: len,
is_contiguous: false, is_contiguous: false,
uninit: false,
} }
} }
@ -222,6 +226,17 @@ impl VmAllocOptions {
self self
} }
/// Sets whether the allocated frames should be uninitialized.
///
/// If `uninit` is set as `false`, the frame will be zeroed once allocated.
/// If `uninit` is set as `true`, the frame will **NOT** be zeroed and should *NOT* be read before writing.
///
/// The default value is false.
pub fn uninit(&mut self, uninit: bool) -> &mut Self {
self.uninit = uninit;
self
}
/// Sets whether the pages can be accessed by devices through /// Sets whether the pages can be accessed by devices through
/// Direct Memory Access (DMA). /// Direct Memory Access (DMA).
/// ///

View File

@ -31,10 +31,8 @@ pub fn alloc_continuous(frame_count: usize) -> Option<Vec<VmFrame>> {
let mut vector = Vec::new(); let mut vector = Vec::new();
unsafe { unsafe {
for i in 0..frame_count { for i in 0..frame_count {
vector.push(VmFrame::new( let frame = VmFrame::new((start + i) * PAGE_SIZE, VmFrameFlags::NEED_DEALLOC);
(start + i) * PAGE_SIZE, vector.push(frame);
VmFrameFlags::NEED_DEALLOC,
))
} }
} }
vector vector

View File

@ -112,7 +112,8 @@ struct Page {
impl Page { impl Page {
pub fn alloc() -> Result<Self> { pub fn alloc() -> Result<Self> {
let frame = { let frame = {
let vm_alloc_option = VmAllocOptions::new(1); let mut vm_alloc_option = VmAllocOptions::new(1);
vm_alloc_option.uninit(true);
let mut frames = VmFrameVec::allocate(&vm_alloc_option)?; let mut frames = VmFrameVec::allocate(&vm_alloc_option)?;
frames.pop().unwrap() frames.pop().unwrap()
}; };
@ -126,7 +127,6 @@ impl Page {
let frame = { let frame = {
let vm_alloc_option = VmAllocOptions::new(1); let vm_alloc_option = VmAllocOptions::new(1);
let mut frames = VmFrameVec::allocate(&vm_alloc_option)?; let mut frames = VmFrameVec::allocate(&vm_alloc_option)?;
frames.zero();
frames.pop().unwrap() frames.pop().unwrap()
}; };
Ok(Self { Ok(Self {

View File

@ -204,9 +204,7 @@ impl Vmo_ {
let frames = match &inner.pager { let frames = match &inner.pager {
None => { None => {
let vm_alloc_option = VmAllocOptions::new(1); let vm_alloc_option = VmAllocOptions::new(1);
let frames = VmFrameVec::allocate(&vm_alloc_option)?; VmFrameVec::allocate(&vm_alloc_option)?
frames.iter().for_each(|frame| frame.zero());
frames
} }
Some(pager) => { Some(pager) => {
let frame = pager.commit_page(offset)?; let frame = pager.commit_page(offset)?;