Skip to content

Commit 4d7a635

Browse files
committed
feat(mm): add PageRangeBox, PageBox, FrameBox
1 parent c0bd1c1 commit 4d7a635

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

src/mm/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ use hermit_sync::{Lazy, RawInterruptTicketMutex};
5454
pub use memory_addresses::{PhysAddr, VirtAddr};
5555
use talc::{ErrOnOom, Span, Talc, Talck};
5656

57-
pub use self::page_range_alloc::PageRangeAllocator;
58-
pub use self::physicalmem::FrameAlloc;
59-
pub use self::virtualmem::PageAlloc;
57+
pub use self::page_range_alloc::{PageRangeAllocator, PageRangeBox};
58+
pub use self::physicalmem::{FrameAlloc, FrameBox};
59+
pub use self::virtualmem::{PageAlloc, PageBox};
6060
#[cfg(any(target_arch = "x86_64", target_arch = "riscv64"))]
6161
use crate::arch::mm::paging::HugePageSize;
6262
pub use crate::arch::mm::paging::virtual_to_physical;

src/mm/page_range_alloc.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
use core::alloc::AllocError;
2+
use core::marker::PhantomData;
3+
use core::ops::Deref;
24

35
use free_list::{PageLayout, PageRange};
46

@@ -19,3 +21,32 @@ pub trait PageRangeAllocator {
1921
/// - `range` must described a range of pages _currently allocated_ via this allocator.
2022
unsafe fn deallocate(range: PageRange);
2123
}
24+
25+
pub struct PageRangeBox<A: PageRangeAllocator>(PageRange, PhantomData<A>);
26+
27+
impl<A: PageRangeAllocator> PageRangeBox<A> {
28+
pub fn new(layout: PageLayout) -> Result<Self, AllocError> {
29+
let range = A::allocate(layout)?;
30+
Ok(Self(range, PhantomData))
31+
}
32+
33+
pub unsafe fn from_raw(range: PageRange) -> Self {
34+
Self(range, PhantomData)
35+
}
36+
}
37+
38+
impl<A: PageRangeAllocator> Drop for PageRangeBox<A> {
39+
fn drop(&mut self) {
40+
unsafe {
41+
A::deallocate(self.0);
42+
}
43+
}
44+
}
45+
46+
impl<A: PageRangeAllocator> Deref for PageRangeBox<A> {
47+
type Target = PageRange;
48+
49+
fn deref(&self) -> &Self::Target {
50+
&self.0
51+
}
52+
}

src/mm/physicalmem.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use memory_addresses::{PhysAddr, VirtAddr};
1111
use crate::arch::mm::paging::PageTableEntryFlagsExt;
1212
use crate::arch::mm::paging::{self, HugePageSize, PageSize, PageTableEntryFlags};
1313
use crate::env;
14-
use crate::mm::PageRangeAllocator;
14+
use crate::mm::{PageRangeAllocator, PageRangeBox};
1515
use crate::mm::device_alloc::DeviceAlloc;
1616

1717
static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
@@ -53,6 +53,8 @@ impl fmt::Display for FrameAlloc {
5353
}
5454
}
5555

56+
pub type FrameBox = PageRangeBox<FrameAlloc>;
57+
5658
pub fn total_memory_size() -> usize {
5759
TOTAL_MEMORY.load(Ordering::Relaxed)
5860
}

src/mm/virtualmem.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use free_list::{FreeList, PageLayout, PageRange};
55
use hermit_sync::InterruptTicketMutex;
66
use memory_addresses::VirtAddr;
77

8-
use crate::mm::PageRangeAllocator;
8+
use crate::mm::{PageRangeAllocator, PageRangeBox};
99

1010
static KERNEL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
1111
InterruptTicketMutex::new(FreeList::new());
@@ -45,6 +45,8 @@ impl fmt::Display for PageAlloc {
4545
}
4646
}
4747

48+
pub type PageBox = PageRangeBox<PageAlloc>;
49+
4850
fn init() {
4951
let range = PageRange::new(
5052
kernel_heap_end().as_usize().div_ceil(2),

0 commit comments

Comments
 (0)