mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Simplify FrameAllocator implementation using Iterator::nth
This commit is contained in:
@@ -68,7 +68,7 @@ impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|||||||
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
||||||
pub struct BootInfoFrameAllocator {
|
pub struct BootInfoFrameAllocator {
|
||||||
memory_map: &'static MemoryMap,
|
memory_map: &'static MemoryMap,
|
||||||
last_yielded_frame: PhysFrame,
|
next: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl BootInfoFrameAllocator {
|
impl BootInfoFrameAllocator {
|
||||||
@@ -76,7 +76,7 @@ impl BootInfoFrameAllocator {
|
|||||||
pub fn init(memory_map: &'static MemoryMap) -> Self {
|
pub fn init(memory_map: &'static MemoryMap) -> Self {
|
||||||
BootInfoFrameAllocator {
|
BootInfoFrameAllocator {
|
||||||
memory_map,
|
memory_map,
|
||||||
last_yielded_frame: PhysFrame::containing_address(PhysAddr::new(0)),
|
next: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,12 +96,8 @@ impl BootInfoFrameAllocator {
|
|||||||
|
|
||||||
impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
||||||
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||||
let usable_frames = self.usable_frames();
|
let frame = self.usable_frames().nth(self.next);
|
||||||
let mut frames = usable_frames.filter(|frame| frame > &self.last_yielded_frame);
|
self.next += 1;
|
||||||
let frame = frames.next();
|
|
||||||
if let Some(frame) = frame {
|
|
||||||
self.last_yielded_frame = frame.clone();
|
|
||||||
}
|
|
||||||
frame
|
frame
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user