mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 01:16:26 +04:00
add alloc_more
This commit is contained in:
parent
294386e7a5
commit
a8b675486f
@ -35,6 +35,7 @@ impl Drop for FrameTracker {
|
||||
trait FrameAllocator {
|
||||
fn new() -> Self;
|
||||
fn alloc(&mut self) -> Option<PhysPageNum>;
|
||||
fn alloc_more(&mut self, pages: usize) -> Option<Vec<PhysPageNum>>;
|
||||
fn dealloc(&mut self, ppn: PhysPageNum);
|
||||
}
|
||||
|
||||
@ -69,6 +70,16 @@ impl FrameAllocator for StackFrameAllocator {
|
||||
Some((self.current - 1).into())
|
||||
}
|
||||
}
|
||||
fn alloc_more(&mut self, pages: usize) -> Option<Vec<PhysPageNum>> {
|
||||
if self.current + pages >= self.end {
|
||||
None
|
||||
} else {
|
||||
self.current += pages;
|
||||
let arr:Vec<usize> = (1..pages + 1).collect();
|
||||
let v = arr.iter().map(|x| (self.current - x).into()).collect();
|
||||
Some(v)
|
||||
}
|
||||
}
|
||||
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||
let ppn = ppn.0;
|
||||
// validity check
|
||||
@ -104,6 +115,13 @@ pub fn frame_alloc() -> Option<FrameTracker> {
|
||||
.map(FrameTracker::new)
|
||||
}
|
||||
|
||||
pub fn frame_alloc_more(num: usize) -> Option<Vec<FrameTracker>> {
|
||||
FRAME_ALLOCATOR
|
||||
.exclusive_access()
|
||||
.alloc_more(num)
|
||||
.map(|x| x.iter().map(|&t| FrameTracker::new(t)).collect())
|
||||
}
|
||||
|
||||
pub fn frame_dealloc(ppn: PhysPageNum) {
|
||||
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
||||
}
|
||||
@ -125,3 +143,21 @@ pub fn frame_allocator_test() {
|
||||
drop(v);
|
||||
println!("frame_allocator_test passed!");
|
||||
}
|
||||
|
||||
|
||||
#[allow(unused)]
|
||||
pub fn frame_allocator_alloc_more_test() {
|
||||
let mut v: Vec<FrameTracker> = Vec::new();
|
||||
let frames = frame_alloc_more(5).unwrap();
|
||||
for frame in &frames {
|
||||
println!("{:?}", frame);
|
||||
}
|
||||
v.extend(frames);
|
||||
v.clear();
|
||||
let frames = frame_alloc_more(5).unwrap();
|
||||
for frame in &frames {
|
||||
println!("{:?}", frame);
|
||||
}
|
||||
drop(v);
|
||||
println!("frame_allocator_test passed!");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user