Skip to main content

kernel_api/mapping/
mappable.rs

1use alloc::boxed::Box;
2use core::num::NonZero;
3
4/// Basic operations to decide how to map memory together.
5///
6/// Implementations of this can be used to instantiate a [`RawMapping`].
7pub trait Mappable {
8	/// The number of pages required to create a mapping with `frame_count` frames
9	fn virtual_size(&self, frame_count: usize) -> NonZero<usize>;
10
11	/// The number of pages to offset the physical memory into the allocated virtual memory
12	fn base_virtual_offset(&self) -> isize;
13}
14
15#[non_exhaustive]
16#[derive(Default)]
17pub struct Mmap {}
18
19impl Mappable for Mmap {
20	fn virtual_size(&self, frame_count: usize) -> NonZero<usize> {
21		NonZero::new(frame_count + 2).unwrap()
22	}
23
24	fn base_virtual_offset(&self) -> isize {
25		1
26	}
27}
28
29pub type Stack = Mmap;
30
31#[non_exhaustive]
32#[derive(Default)]
33pub struct UnsafeMmap {}
34
35impl Mappable for UnsafeMmap {
36	fn virtual_size(&self, frame_count: usize) -> NonZero<usize> {
37		NonZero::new(frame_count).unwrap()
38	}
39
40	fn base_virtual_offset(&self) -> isize {
41		0
42	}
43}
44
45impl<T: ?Sized + Mappable> Mappable for Box<T> {
46	fn virtual_size(&self, frame_count: usize) -> NonZero<usize> {
47		T::virtual_size(&**self, frame_count)
48	}
49
50	fn base_virtual_offset(&self) -> isize {
51		T::base_virtual_offset(&**self)
52	}
53}