Skip to content

Commit 223ae47

Browse files
committed
Add a Mapper::map convenience function
1 parent c65ec51 commit 223ae47

File tree

1 file changed

+28
-0
lines changed
  • src/structures/paging/mapper

1 file changed

+28
-0
lines changed

src/structures/paging/mapper/mod.rs

+28
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,34 @@ pub trait Mapper<S: PageSize> {
148148
/// error otherwise.
149149
fn translate_page(&self, page: Page<S>) -> Result<PhysFrame<S>, TranslateError>;
150150

151+
/// Maps the given page to an unused frame obtained from the frame_allocator.
152+
///
153+
/// This function allocates at least one physical frame from the given
154+
/// frame allocator. It might also need additional physical frames to create
155+
/// new page tables, which are also allocated from the `frame_allocator`
156+
/// argument. At most four frames are required from the allocator in total.
157+
///
158+
/// ## Safety
159+
///
160+
/// This is a convencience function that invokes [`map_to`] internally, so
161+
/// all safety requirements of it also apply for this function.
162+
#[inline]
163+
unsafe fn map<A>(
164+
&mut self,
165+
page: Page<S>,
166+
flags: PageTableFlags,
167+
frame_allocator: &mut A,
168+
) -> Result<MapperFlush<S>, MapToError<S>>
169+
where
170+
Self: Sized,
171+
A: FrameAllocator<Size4KiB> + FrameAllocator<S>,
172+
{
173+
let frame = frame_allocator
174+
.allocate_frame()
175+
.ok_or(MapToError::FrameAllocationFailed)?;
176+
self.map_to(page, frame, flags, frame_allocator)
177+
}
178+
151179
/// Maps the given frame to the virtual page with the same address.
152180
///
153181
/// ## Safety

0 commit comments

Comments
 (0)