Skip to content

Commit 0887b57

Browse files
committed
feat(mm): add PageRangeBox, PageBox, FrameBox
1 parent e3f4261 commit 0887b57

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use core::alloc::AllocError;
2+
use core::marker::PhantomData;
3+
use core::mem::ManuallyDrop;
4+
use core::ops::Deref;
25

36
use free_list::{PageLayout, PageRange};
47

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

src/mm/physicalmem.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ 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;
1514
use crate::mm::device_alloc::DeviceAlloc;
15+
use crate::mm::{PageRangeAllocator, PageRangeBox};
1616

1717
static PHYSICAL_FREE_LIST: InterruptTicketMutex<FreeList<16>> =
1818
InterruptTicketMutex::new(FreeList::new());
@@ -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)