-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(mm): add
DeviceAlloc
for communicating with devices
Signed-off-by: Martin Kröning <martin.kroening@eonerc.rwth-aachen.de>
- Loading branch information
Showing
2 changed files
with
31 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use core::alloc::{AllocError, Allocator, Layout}; | ||
use core::ptr::{self, NonNull}; | ||
|
||
use align_address::Align; | ||
|
||
use crate::arch::mm::paging::{BasePageSize, PageSize}; | ||
|
||
/// An [`Allocator`] for memory that is used to communicate with devices. | ||
/// | ||
/// Allocations from this allocator always correspond to contiguous physical memory. | ||
pub struct DeviceAlloc; | ||
|
||
unsafe impl Allocator for DeviceAlloc { | ||
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> { | ||
assert!(layout.align() <= BasePageSize::SIZE as usize); | ||
let size = layout.size().align_up(BasePageSize::SIZE as usize); | ||
let ptr = super::allocate(size, true).as_mut_ptr::<u8>(); | ||
let slice = ptr::slice_from_raw_parts_mut(ptr, size); | ||
Ok(NonNull::new(slice).unwrap()) | ||
} | ||
|
||
unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) { | ||
assert!(layout.align() <= BasePageSize::SIZE as usize); | ||
let size = layout.size().align_up(BasePageSize::SIZE as usize); | ||
let addr = ptr.as_ptr().expose_provenance().into(); | ||
super::deallocate(addr, size); | ||
} | ||
} |
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