diff --git a/posts/DRAFT-allocating-frames.md b/posts/DRAFT-allocating-frames.md index addceb8c..393428c2 100644 --- a/posts/DRAFT-allocating-frames.md +++ b/posts/DRAFT-allocating-frames.md @@ -200,6 +200,16 @@ impl Frame { } ``` +We also add a `FrameAllocator` trait: + +```rust +pub trait FrameAllocator { + fn allocate_frame(&mut self) -> Option; + fn deallocate_frame(&mut self, frame: Frame); +} +``` +This allows us to create another, more advanced frame allocator in the future. + ### The Allocator Now we can put everything together and create the frame allocator. It looks like this: @@ -248,7 +258,7 @@ fn choose_next_area(&mut self) { ``` ```rust -pub fn allocate_frame(&mut self) -> Option { +fn allocate_frame(&mut self) -> Option { match self.current_area { None => None, Some(area) => { diff --git a/src/lib.rs b/src/lib.rs index 92dc1f03..e0ae38fc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -58,6 +58,7 @@ pub extern fn rust_main(multiboot_information_address: usize) { kernel_end as usize, multiboot_start, multiboot_end, memory_map_tag.memory_areas()); for i in 0.. { + use memory::FrameAllocator; if let None = frame_allocator.allocate_frame() { println!("allocated {} frames", i); break; diff --git a/src/memory/area_frame_allocator.rs b/src/memory/area_frame_allocator.rs index ade79217..4a534f4d 100644 --- a/src/memory/area_frame_allocator.rs +++ b/src/memory/area_frame_allocator.rs @@ -1,4 +1,4 @@ -use memory::Frame; +use memory::{Frame, FrameAllocator}; use multiboot2::{MemoryAreaIter, MemoryArea}; /// A frame allocator that uses the memory areas from the multiboot information structure as @@ -33,7 +33,16 @@ impl AreaFrameAllocator { allocator } - pub fn allocate_frame(&mut self) -> Option { + fn choose_next_area(&mut self) { + self.current_area = self.areas.clone().filter(|area| { + let address = area.base_addr + area.length - 1; + Frame::containing_address(address as usize) >= self.next_free_frame + }).min_by(|area| area.base_addr); + } +} + +impl FrameAllocator for AreaFrameAllocator { + fn allocate_frame(&mut self) -> Option { match self.current_area { None => None, Some(area) => { @@ -58,10 +67,5 @@ impl AreaFrameAllocator { } } - fn choose_next_area(&mut self) { - self.current_area = self.areas.clone().filter(|area| { - let address = area.base_addr + area.length - 1; - Frame::containing_address(address as usize) >= self.next_free_frame - }).min_by(|area| area.base_addr); - } + fn deallocate_frame(&mut self, _frame: Frame) {unimplemented!()} } diff --git a/src/memory/mod.rs b/src/memory/mod.rs index 496244c4..385c574f 100644 --- a/src/memory/mod.rs +++ b/src/memory/mod.rs @@ -14,3 +14,8 @@ impl Frame { Frame{ number: address / PAGE_SIZE } } } + +pub trait FrameAllocator { + fn allocate_frame(&mut self) -> Option; + fn deallocate_frame(&mut self, frame: Frame); +}