Skip to main content

kernel_api/allocator/
vmm.rs

1use crate::memory::RawPage;
2use crate::allocator::AllocError;
3
4/// A virtual memory allocator.
5pub trait Vmm {
6	/// Allocates `count` number of contiguous pages.
7	///
8	/// # Errors
9	///
10	/// Returns an [`AllocError`] if the memory could not be allocated. This
11	/// does not mean there is no free memory, just that is there no region
12	/// of contiguous memory large enough.
13	#[must_use = "memory will be leaked unless the page is explicitly deallocated"]
14	fn allocate_contiguous(&self, len: usize) -> Result<RawPage, AllocError>;
15
16	/// Allocates `count` number of contiguous pages with the first frame being at `at`.
17	///
18	/// # Errors
19	///
20	/// Returns an [`AllocError`] if the memory could not be allocated. This
21	/// does not mean there is no free memory, just that is there no region
22	/// of contiguous memory large enough starting at `at`.
23	#[must_use = "memory will be leaked unless the page is explicitly deallocated"]
24	fn allocate_contiguous_at(&self, at: RawPage, len: usize) -> Result<RawPage, AllocError>;
25
26	/// Deallocates `count` frames starting at `base`.
27	// FIXME: why isn't this `unsafe` like the pmm equivalent is
28	fn deallocate_contiguous(&self, base: RawPage, len: usize);
29}
30
31// todo: check why this exists
32#[doc(hidden)]
33impl Vmm for ! {
34	fn allocate_contiguous(&self, _len: usize) -> Result<RawPage, AllocError> {
35		*self
36	}
37
38	fn allocate_contiguous_at(&self, _at: RawPage, _len: usize) -> Result<RawPage, AllocError> {
39		*self
40	}
41
42	fn deallocate_contiguous(&self, _base: RawPage, _len: usize) {}
43}