use core::alloc::{GlobalAlloc, Layout};
use core::marker::PhantomData;
use mnemosyne_local::{thread_alloc_layout, thread_free_layout, thread_realloc};
use crate::{AllocPolicy, LocalAllocatorSelector, StandardPolicy};
use mnemosyne_local::tls_slot::PolicySlotSelection;
pub struct Mnemosyne;
unsafe impl GlobalAlloc for Mnemosyne {
#[inline(always)]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe {
thread_alloc_layout::<StandardPolicy, mnemosyne_backend::MemoryBackendWrapper>(
layout.size(),
layout.align(),
)
}
}
#[inline(always)]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe {
thread_free_layout::<StandardPolicy, mnemosyne_backend::MemoryBackendWrapper>(
ptr,
layout.size(),
layout.align(),
)
}
}
#[inline(always)]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
unsafe {
thread_realloc::<StandardPolicy, mnemosyne_backend::MemoryBackendWrapper>(
ptr, layout, new_size,
)
}
}
}
pub struct MnemosyneAllocator<
P: AllocPolicy,
B: mnemosyne_arena::HasSegmentPool + LocalAllocatorSelector<B> = mnemosyne_backend::MemoryBackendWrapper,
>(PhantomData<(P, B)>);
impl<
P: AllocPolicy + PolicySlotSelection<B>,
B: mnemosyne_arena::HasSegmentPool + LocalAllocatorSelector<B>,
> MnemosyneAllocator<P, B>
{
pub const fn new() -> Self {
Self(PhantomData)
}
}
impl<
P: AllocPolicy + PolicySlotSelection<B>,
B: mnemosyne_arena::HasSegmentPool + LocalAllocatorSelector<B>,
> Default for MnemosyneAllocator<P, B>
{
fn default() -> Self {
Self::new()
}
}
unsafe impl<
P: AllocPolicy + PolicySlotSelection<B>,
B: mnemosyne_arena::HasSegmentPool + LocalAllocatorSelector<B>,
> GlobalAlloc for MnemosyneAllocator<P, B>
{
#[inline(always)]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { thread_alloc_layout::<P, B>(layout.size(), layout.align()) }
}
#[inline(always)]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe { thread_free_layout::<P, B>(ptr, layout.size(), layout.align()) }
}
#[inline(always)]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
unsafe { thread_realloc::<P, B>(ptr, layout, new_size) }
}
}