Skip to content

Commit

Permalink
refactor: apply fixes requested in review
Browse files Browse the repository at this point in the history
  • Loading branch information
Piotr Makowiecki committed Oct 27, 2023
1 parent bb234d6 commit 7ad29c4
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions kernel/src/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use x86_64::structures::paging::{
};
use x86_64::{structures::paging::PageTable, PhysAddr, VirtAddr};

const PAGE_FRAME_SIZE: usize = 4096;

pub static MEMORY_MANAGER: Once<Mutex<MemoryManager>> = Once::new();

/// # Panics
Expand Down Expand Up @@ -93,6 +95,11 @@ impl MemoryManager {
.ok_or(MapToError::FrameAllocationFailed)?;

// Map page to allocated memory frame
// SAFETY
// This is safe because we map an unmapped page to USABLE frame.
// In case of mapping already mapped paged an apprioriate Error is returned.
// This function can't remap pages so it's impossible to cause value invalidation and
// provoke UB.
unsafe {
self.mapper
.map_to(page, frame, flags, &mut self.frame_allocator)?
Expand Down Expand Up @@ -168,15 +175,14 @@ impl BootInfoFrameAllocator {

/// Returns an iterator over the usable frames specified in [`MemoryRegions`].
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
let regions = self.memory_regions.iter();
// get usable regions
let usable_regions = regions.filter(|r| r.kind == MemoryRegionKind::Usable);
// map each region to its address range
let addr_ranges = usable_regions.map(|r| r.start..r.end);
// transform to an iterator of frame start addresses
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
// create `PhysFrame` types from the start addresses
frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
self.memory_regions
.iter()
.filter(|r| r.kind == MemoryRegionKind::Usable)
// map each region to its address range
.map(|r| r.start..r.end)
// transform to an iterator of frame start addresses
.flat_map(|r| r.step_by(PAGE_FRAME_SIZE))
.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
}
}

Expand Down

0 comments on commit 7ad29c4

Please sign in to comment.