1use crate::{direct_alloc, direct_dealloc, MemoryPool};
21use std::alloc::GlobalAlloc;
22use std::alloc::Layout;
23use std::sync::Mutex;
24
25pub struct Arrakis {
26 dunes: Mutex<MemoryPool>,
27}
28
29impl Arrakis {
30 pub const fn with_capacity(allocation_size: usize) -> Self {
31 Self { dunes: Mutex::new(MemoryPool::with_chunk_size(allocation_size)) }
32 }
33
34 #[inline]
35 unsafe fn allocate(&self, layout: Layout) -> *mut u8 {
36 let mut dunes = self.dunes.lock().unwrap();
37 dunes.allocate(layout)
38 }
39
40 #[inline]
41 unsafe fn deallocate(&self,ptr: *mut u8, layout: Layout) {
42 let mut dunes = self.dunes.lock().unwrap();
43 dunes.deallocate(ptr, layout);
44 }
45}
46
47unsafe impl GlobalAlloc for Arrakis {
48 #[cfg(feature = "fast_global_allocator")]
49 #[inline(always)]
50 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
51 direct_alloc(layout)
52 }
53
54 #[cfg(not(feature = "fast_global_allocator"))]
55 #[inline(always)]
56 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
57 self.allocate(layout)
58 }
59
60 #[cfg(feature = "fast_global_allocator")]
61 #[inline(always)]
62 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
63 direct_dealloc(ptr, _layout)
64 }
65
66 #[cfg(not(feature = "fast_global_allocator"))]
67 #[inline(always)]
68 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
69 self.deallocate(ptr, _layout)
70 }
71}
72
73#[macro_export]
74macro_rules! rumtk_dune_new {
75 ( ) => {{
76 use $crate::mem::constants::DEFAULT_GLOBAL_MB_ALLOCATION;
77 rumtk_dune_new!(DEFAULT_GLOBAL_MB_ALLOCATION)
78 }};
79 ( $size:expr ) => {{
80 use std::sync::LazyLock;
81 use $crate::dune::{Arrakis};
82
83 Arrakis::with_capacity($size)
84 }};
85}
86
87