Refactor alloc_single API for page and frame

This commit is contained in:
nishirong
2024-06-14 16:21:40 +08:00
committed by Tate, Hongliang Tian
parent bd1d076e8c
commit 2ceba78d47
2 changed files with 11 additions and 7 deletions

View File

@ -54,7 +54,9 @@ impl FrameAllocOptions {
} else { } else {
let mut frame_list = Vec::new(); let mut frame_list = Vec::new();
for _ in 0..self.nframes { for _ in 0..self.nframes {
frame_list.push(allocator::alloc_single().ok_or(Error::NoMemory)?); let page = allocator::alloc_single().ok_or(Error::NoMemory)?;
let frame = Frame { page };
frame_list.push(frame);
} }
FrameVec(frame_list) FrameVec(frame_list)
}; };
@ -73,7 +75,8 @@ impl FrameAllocOptions {
return Err(Error::InvalidArgs); return Err(Error::InvalidArgs);
} }
let frame = allocator::alloc_single().ok_or(Error::NoMemory)?; let page = allocator::alloc_single().ok_or(Error::NoMemory)?;
let frame = Frame { page };
if !self.uninit { if !self.uninit {
frame.writer().fill(0); frame.writer().fill(0);
} }

View File

@ -12,7 +12,10 @@ use buddy_system_allocator::FrameAllocator;
use log::info; use log::info;
use spin::Once; use spin::Once;
use super::{meta::FrameMeta, Page}; use super::{
meta::{FrameMeta, PageMeta},
Page,
};
use crate::{ use crate::{
boot::memory_region::MemoryRegionType, boot::memory_region::MemoryRegionType,
mm::{Frame, FrameVec, Segment, PAGE_SIZE}, mm::{Frame, FrameVec, Segment, PAGE_SIZE},
@ -40,12 +43,10 @@ pub(crate) fn alloc(nframes: usize) -> Option<FrameVec> {
}) })
} }
pub(crate) fn alloc_single() -> Option<Frame> { pub(crate) fn alloc_single<T: PageMeta>() -> Option<Page<T>> {
FRAME_ALLOCATOR.get().unwrap().lock().alloc(1).map(|idx| { FRAME_ALLOCATOR.get().unwrap().lock().alloc(1).map(|idx| {
let paddr = idx * PAGE_SIZE; let paddr = idx * PAGE_SIZE;
Frame { Page::<T>::from_unused(paddr)
page: Page::<FrameMeta>::from_unused(paddr),
}
}) })
} }