-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
84: Make Mapper trait object safe by adding `Self: Sized` bounds on generic functions r=phil-opp a=phil-opp See #80 for more information I'm not quite sure whether this is a **breaking change**, but I think it is. 89: Add new UnsafePhysFrame type and use it in Mapper::map_to r=phil-opp a=phil-opp Fixes #88 This pull request adds a new `UnsafePhysFrame` type that wraps a `PhysFrame`. The type is unsafe to construct and the caller must guarantee that the frame is not mapped already. This type allows to make the `map_to` and `identity_map` methods of the `Mapper` trait safe. This PR also adjust the `FrameAllocator` to use the new type. This is a **breaking change**. Co-authored-by: Philipp Oppermann <dev@phil-opp.com>
- Loading branch information
Showing
5 changed files
with
77 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,51 @@ | ||
//! Traits for abstracting away frame allocation and deallocation. | ||
|
||
use crate::structures::paging::{PageSize, PhysFrame}; | ||
use crate::structures::paging::{PageSize, PhysFrame, Size4KiB}; | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
/// A trait for types that can allocate a frame of memory. | ||
/// | ||
/// This trait is unsafe to implement because the implementer must guarantee that | ||
/// the `allocate_frame` method returns only unique unused frames. | ||
pub unsafe trait FrameAllocator<S: PageSize> { | ||
/// Allocate a frame of the appropriate size and return it if possible. | ||
fn allocate_frame(&mut self) -> Option<PhysFrame<S>>; | ||
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame<S>>; | ||
} | ||
|
||
/// A trait for types that can deallocate a frame of memory. | ||
pub trait FrameDeallocator<S: PageSize> { | ||
/// Deallocate the given frame of memory. | ||
fn deallocate_frame(&mut self, frame: PhysFrame<S>); | ||
fn deallocate_frame(&mut self, frame: UnusedPhysFrame<S>); | ||
} | ||
|
||
/// Represents a physical frame that is not used for any mapping. | ||
#[derive(Debug)] | ||
pub struct UnusedPhysFrame<S: PageSize = Size4KiB>(PhysFrame<S>); | ||
|
||
impl<S: PageSize> UnusedPhysFrame<S> { | ||
/// Creates a new UnusedPhysFrame from the given frame. | ||
/// | ||
/// This method is unsafe because the caller must guarantee | ||
/// that the given frame is unused. | ||
pub unsafe fn new(frame: PhysFrame<S>) -> Self { | ||
Self(frame) | ||
} | ||
|
||
pub fn frame(self) -> PhysFrame<S> { | ||
self.0 | ||
} | ||
} | ||
|
||
impl<S: PageSize> Deref for UnusedPhysFrame<S> { | ||
type Target = PhysFrame<S>; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<S: PageSize> DerefMut for UnusedPhysFrame<S> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
&mut self.0 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters