-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mm/alloc: add GlobalBox implementation
Add a new type, GlobalBox, which wraps around the TryBox type but uses the global SvsmAllocator transparently. The new type implements AsRef<TryBox> and Into<TryBox>, so regular TryBox methods can also be used on the new type. Signed-off-by: Carlos López <[email protected]>
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use super::alloc::{SvsmAllocator, ALLOCATOR}; | ||
use crate::alloc::boxed::TryBox; | ||
use crate::alloc::TryAllocError; | ||
use crate::error::SvsmError; | ||
use core::mem::MaybeUninit; | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
impl From<TryAllocError> for SvsmError { | ||
fn from(err: TryAllocError) -> Self { | ||
SvsmError::TryAlloc(err) | ||
} | ||
} | ||
|
||
/// A [`TryBox`] wrapper which uses the global memory allocator. | ||
#[derive(Debug)] | ||
pub struct GlobalBox<T>(TryBox<T, &'static SvsmAllocator>); | ||
|
||
impl<T> GlobalBox<T> { | ||
pub fn try_new(val: T) -> Result<Self, SvsmError> { | ||
let inner = TryBox::try_new_in(val, &ALLOCATOR)?; | ||
Ok(Self(inner)) | ||
} | ||
|
||
pub fn try_new_uninit() -> Result<GlobalBox<MaybeUninit<T>>, SvsmError> { | ||
let inner = TryBox::try_new_uninit_in(&ALLOCATOR)?; | ||
Ok(GlobalBox(inner)) | ||
} | ||
|
||
pub fn try_new_zeroed() -> Result<GlobalBox<MaybeUninit<T>>, SvsmError> { | ||
let inner = TryBox::try_new_zeroed_in(&ALLOCATOR)?; | ||
Ok(GlobalBox(inner)) | ||
} | ||
|
||
pub fn into_inner(self) -> T { | ||
TryBox::into_inner(self.0) | ||
} | ||
|
||
/// # Safety | ||
/// | ||
/// See the safety requirements for [`TryBox::from_raw_in()`]. | ||
#[inline] | ||
pub unsafe fn from_raw(raw: *mut T) -> Self { | ||
Self(TryBox::from_raw_in(raw, &ALLOCATOR)) | ||
} | ||
} | ||
|
||
impl<T> From<TryBox<T, &'static SvsmAllocator>> for GlobalBox<T> { | ||
fn from(boxed: TryBox<T, &'static SvsmAllocator>) -> Self { | ||
Self(boxed) | ||
} | ||
} | ||
|
||
impl<T> From<GlobalBox<T>> for TryBox<T, &'static SvsmAllocator> { | ||
fn from(boxed: GlobalBox<T>) -> Self { | ||
let ptr = TryBox::into_raw(boxed.0); | ||
// SAFETY: if this GlobalBox was constructed correctly then the inner | ||
// pointer is guaranteed to belong to the global allocator. This | ||
// function also consumes the GlobalBox, so the inner pointer will | ||
// not be used twice. | ||
unsafe { TryBox::from_raw_in(ptr, &ALLOCATOR) } | ||
} | ||
} | ||
|
||
impl<T> AsRef<TryBox<T, &'static SvsmAllocator>> for GlobalBox<T> { | ||
fn as_ref(&self) -> &TryBox<T, &'static SvsmAllocator> { | ||
&self.0 | ||
} | ||
} | ||
|
||
impl<T> AsMut<TryBox<T, &'static SvsmAllocator>> for GlobalBox<T> { | ||
fn as_mut(&mut self) -> &mut TryBox<T, &'static SvsmAllocator> { | ||
&mut self.0 | ||
} | ||
} | ||
|
||
impl<T> Deref for GlobalBox<T> { | ||
type Target = T; | ||
|
||
fn deref(&self) -> &T { | ||
TryBox::deref(self.as_ref()) | ||
} | ||
} | ||
|
||
impl<T> DerefMut for GlobalBox<T> { | ||
fn deref_mut(&mut self) -> &mut T { | ||
TryBox::deref_mut(self.as_mut()) | ||
} | ||
} |
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