-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implements stage 1 kernel heap (#956)
- Loading branch information
1 parent
da46138
commit 8bd35ba
Showing
3 changed files
with
38 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,42 @@ | ||
use core::alloc::{GlobalAlloc, Layout}; | ||
use core::ptr::{null_mut, NonNull}; | ||
use talc::{ClaimOnOom, Span, Talc}; | ||
|
||
/// Implementation of [`GlobalAlloc`] for objects belong to kernel space. | ||
pub struct KernelHeap {} | ||
/// | ||
/// This allocator has 2 stages. The first stage will allocate a memory from a static buffer (AKA | ||
/// arena). This stage will be primary used for bootstrapping the kernel. The second stage will be | ||
/// activated once the required subsystems has been initialized. | ||
pub struct KernelHeap { | ||
stage1: spin::Mutex<Talc<ClaimOnOom>>, | ||
} | ||
|
||
impl KernelHeap { | ||
pub const fn new() -> Self { | ||
Self {} | ||
/// # Safety | ||
/// The specified memory must be valid for reads and writes and it must be exclusively available | ||
/// to [`KernelHeap`]. | ||
pub const unsafe fn new(stage1: *mut u8, len: usize) -> Self { | ||
let stage1 = Talc::new(unsafe { ClaimOnOom::new(Span::from_base_size(stage1, len)) }); | ||
|
||
Self { | ||
stage1: spin::Mutex::new(stage1), | ||
} | ||
} | ||
} | ||
|
||
unsafe impl GlobalAlloc for KernelHeap { | ||
unsafe fn alloc(&self, _: Layout) -> *mut u8 { | ||
todo!() | ||
unsafe fn alloc(&self, layout: Layout) -> *mut u8 { | ||
// SAFETY: GlobalAlloc::alloc required layout to be non-zero. | ||
self.stage1 | ||
.lock() | ||
.malloc(layout) | ||
.map(|v| v.as_ptr()) | ||
.unwrap_or(null_mut()) | ||
} | ||
|
||
unsafe fn dealloc(&self, _: *mut u8, _: Layout) { | ||
todo!() | ||
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { | ||
// SAFETY: GlobalAlloc::dealloc required ptr to be the same one that returned from our | ||
// GlobalAlloc::alloc and layout to be the same one that passed to it. | ||
self.stage1.lock().free(NonNull::new_unchecked(ptr), layout); | ||
} | ||
} |