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`.
pub fn allocate(options: &VmAllocOptions) -> Result<Self> {
let page_size = options.page_size;
let mut frame_list = Vec::new();
if options.is_contiguous {
let frames = frame_allocator::alloc_continuous(options.page_size);
if frames.is_none() {
return Err(Error::NoMemory);
let frames = if options.is_contiguous {
frame_allocator::alloc_continuous(options.page_size).ok_or(Error::NoMemory)?
} else {
let mut frame_list = Vec::new();
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 {
frame_list.push(frame_allocator::alloc().unwrap());
}
Ok(Self(frame_list))
Ok(frame_vec)
}
pub fn get(&self, index: usize) -> Option<&VmFrame> {
@ -201,6 +203,7 @@ impl<'a> Iterator for VmFrameVecIter<'a> {
pub struct VmAllocOptions {
page_size: usize,
is_contiguous: bool,
uninit: bool,
}
impl VmAllocOptions {
@ -209,6 +212,7 @@ impl VmAllocOptions {
Self {
page_size: len,
is_contiguous: false,
uninit: false,
}
}
@ -222,6 +226,17 @@ impl VmAllocOptions {
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
/// 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();
unsafe {
for i in 0..frame_count {
vector.push(VmFrame::new(
(start + i) * PAGE_SIZE,
VmFrameFlags::NEED_DEALLOC,
))
let frame = VmFrame::new((start + i) * PAGE_SIZE, VmFrameFlags::NEED_DEALLOC);
vector.push(frame);
}
}
vector