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]
50 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
51 direct_alloc(layout.size())
52 }
53
54 #[cfg(not(feature = "fast_global_allocator"))]
55 #[inline]
56 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
57 self.allocate(layout)
58 }
59 #[cfg(feature = "fast_global_allocator")]
60 #[inline]
61 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
62 direct_dealloc(ptr, _layout.size())
63 }
64
65 #[cfg(not(feature = "fast_global_allocator"))]
66 #[inline]
67 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
68 self.deallocate(ptr, _layout)
69 }
70}
71
72#[macro_export]
73macro_rules! rumtk_dune_new {
74 ( ) => {{
75 use $crate::constants::DEFAULT_GLOBAL_MB_ALLOCATION;
76 rumtk_dune_new!(DEFAULT_GLOBAL_MB_ALLOCATION)
77 }};
78 ( $size:expr ) => {{
79 use std::sync::LazyLock;
80 use $crate::dune::{Arrakis};
81 use $crate::constants::DEFAULT_GLOBAL_MB_ALLOCATION;
82 Arrakis::with_capacity(DEFAULT_GLOBAL_MB_ALLOCATION)
83 }}
84}
85
86