add alloc_more
This commit is contained in:
parent
191eb13c21
commit
4ce5f89c9c
1 changed files with 36 additions and 0 deletions
|
@ -35,6 +35,7 @@ impl Drop for FrameTracker {
|
||||||
trait FrameAllocator {
|
trait FrameAllocator {
|
||||||
fn new() -> Self;
|
fn new() -> Self;
|
||||||
fn alloc(&mut self) -> Option<PhysPageNum>;
|
fn alloc(&mut self) -> Option<PhysPageNum>;
|
||||||
|
fn alloc_more(&mut self, pages: usize) -> Option<Vec<PhysPageNum>>;
|
||||||
fn dealloc(&mut self, ppn: PhysPageNum);
|
fn dealloc(&mut self, ppn: PhysPageNum);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -69,6 +70,16 @@ impl FrameAllocator for StackFrameAllocator {
|
||||||
Some((self.current - 1).into())
|
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) {
|
fn dealloc(&mut self, ppn: PhysPageNum) {
|
||||||
let ppn = ppn.0;
|
let ppn = ppn.0;
|
||||||
// validity check
|
// validity check
|
||||||
|
@ -104,6 +115,13 @@ pub fn frame_alloc() -> Option<FrameTracker> {
|
||||||
.map(FrameTracker::new)
|
.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) {
|
pub fn frame_dealloc(ppn: PhysPageNum) {
|
||||||
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
FRAME_ALLOCATOR.exclusive_access().dealloc(ppn);
|
||||||
}
|
}
|
||||||
|
@ -125,3 +143,21 @@ pub fn frame_allocator_test() {
|
||||||
drop(v);
|
drop(v);
|
||||||
println!("frame_allocator_test passed!");
|
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…
Add table
Add a link
Reference in a new issue