use ample::traits::AllocatableResult;
use crate::Origin;
ample::r#struct!(
#[derive(Debug)]
pub struct Allocator {}
);
pub type AllocatorPointer = *mut Allocator;
impl ample::traits::Allocatable<Origin> for Allocator {
type Ok = crate::Ok;
type Error = crate::Error;
fn allocate(numerosity_of_bytes: usize) -> crate::Result {
match crate::target::os::syscall::mmap(
core::ptr::null_mut(),
numerosity_of_bytes,
(crate::target::os::syscall::mmap::Prot::Read
| crate::target::os::syscall::mmap::Prot::Write) as i32,
(crate::target::os::syscall::mmap::Flag::Anonymous
| crate::target::os::syscall::mmap::Flag::Private) as i32,
-1,
0,
) {
core::result::Result::Ok(crate::Ok::Target(crate::target::Ok::Os(
crate::target::os::Ok::Syscall(crate::target::os::syscall::Ok::MMap(
crate::target::os::syscall::mmap::Ok::Default(m),
)),
))) => core::result::Result::Ok(crate::Ok::Memory(crate::memory::Ok::HeapAllocate(
m as *mut Self,
))),
_ => panic!("Failed to allocate memory"),
}
}
fn deallocate(ptr: *mut Self, numerosity_of_bytes: usize) -> crate::Result {
match crate::target::os::syscall::munmap(ptr as *mut u8, numerosity_of_bytes) {
_ => true,
};
core::result::Result::Ok(crate::Ok::Memory(crate::memory::Ok::HeapAllocate(
ptr as *mut Self,
)))
}
}
pub trait Allocating<T> {
fn allocate(count: usize) -> *mut T;
fn allocate_slice(count: usize) -> &'static mut [T];
fn deallocate(ptr: *mut T, count: usize) -> bool;
fn deallocate_slice(slice: &mut [T]) -> bool;
}
impl<T> Allocating<T> for T
where
T: ample::traits::Bytes<Origin, Origin>,
{
fn allocate(numerosity: usize) -> *mut T {
let numerosity_of_bytes = numerosity * T::BYTES_SIZE + T::BYTES_ALIGN;
match <Allocator as ample::traits::Allocatable<Origin>>::allocate(numerosity_of_bytes) {
Ok(m) => m.as_ptr() as *mut T,
_ => core::ptr::null_mut(),
}
}
fn deallocate(ptr: *mut T, numerosity: usize) -> bool {
let total_size = numerosity * T::BYTES_SIZE + T::BYTES_ALIGN;
let aligned_size =
(total_size + crate::memory::page::SIZE - 1) & !(crate::memory::page::SIZE - 1);
match <Allocator as ample::traits::Allocatable<Origin>>::deallocate(
ptr as *mut Allocator,
aligned_size,
) {
Ok(_) => true,
Err(_) => false,
}
}
fn allocate_slice(numerosity: usize) -> &'static mut [T] {
let ptr = Self::allocate(numerosity) as *mut T;
unsafe { core::slice::from_raw_parts_mut(ptr, numerosity) }
}
fn deallocate_slice(slice: &mut [T]) -> bool {
Self::deallocate(slice.as_mut_ptr() as *mut T, slice.len())
}
}
impl<T> Allocating<T> for &[T]
where
T: ample::traits::Bytes<Origin, Origin>,
{
fn allocate(numerosity: usize) -> *mut T {
match <Allocator as ample::traits::Allocatable<Origin>>::allocate(
numerosity * T::BYTES_SIZE,
) {
Ok(ptr) => ptr.as_ptr() as *mut T,
Err(_) => core::ptr::null_mut(),
}
}
fn deallocate(ptr: *mut T, numerosity: usize) -> bool {
match <Allocator as ample::traits::Allocatable<Origin>>::deallocate(
ptr as *mut Allocator,
numerosity,
) {
Ok(_) => true,
Err(_) => false,
}
}
fn allocate_slice(numerosity: usize) -> &'static mut [T] {
let ptr = match <Allocator as ample::traits::Allocatable<Origin>>::allocate(numerosity) {
Ok(ptr) => ptr.as_ptr() as *mut T,
Err(_) => core::ptr::null_mut(),
};
unsafe { core::slice::from_raw_parts_mut(ptr, numerosity) }
}
fn deallocate_slice(slice: &mut [T]) -> bool {
match <Allocator as ample::traits::Allocatable<Origin>>::deallocate(
slice.as_mut_ptr() as *mut Allocator,
slice.len(),
) {
Ok(_) => true,
Err(_) => false,
}
}
}
impl ample::traits::AllocatableResult for crate::Ok {
fn as_ptr(&self) -> *mut u8 {
match self {
crate::Ok::Memory(crate::memory::Ok::HeapAllocate(m)) => *m as *mut u8,
_ => core::ptr::null_mut(),
}
}
fn from_raw(raw: *mut u8) -> Self {
crate::Ok::Memory(crate::memory::Ok::HeapAllocate(raw as *mut Allocator))
}
}
impl ample::traits::AllocatableResult for crate::Error {
fn as_ptr(&self) -> *mut u8 {
match self {
_ => core::ptr::null_mut(),
}
}
fn from_raw(_raw: *mut u8) -> Self {
crate::Error::Error(3)
}
}