Skip to main content

ruda_runtime/runtime/memory_management/
mod.rs

1pub(crate) mod memory_pool;
2
3mod base;
4
5/// Export utilities to keep track of CPU buffers when performing async data copies.
6pub mod drop_queue;
7
8pub use base::*;
9
10/// Dynamic memory management strategy.
11mod memory_manage;
12pub use memory_manage::*;
13mod adaptive;
14
15use alloc::vec::Vec;
16
17/// The type of memory pool to use.
18#[derive(Debug, Clone)]
19pub enum PoolType {
20    /// Use a memory where every allocation is a separate page.
21    ExclusivePages {
22        /// The minimum number of bytes to allocate in this pool.
23        max_alloc_size: u64,
24    },
25    /// Use a memory where each allocation is a slice of a bigger allocation.
26    SlicedPages {
27        /// The page size to allocate.
28        page_size: u64,
29        /// The maximum size of a slice to allocate in the pool.
30        max_slice_size: u64,
31    },
32}
33
34/// Options to create a memory pool.
35#[derive(Debug, Clone)]
36pub struct MemoryPoolOptions {
37    /// What kind of pool to use.
38    pub pool_type: PoolType,
39    /// Period after which allocations are deemed unused and deallocated.
40    ///
41    /// This period is measured in the number of allocations in the parent allocator. If a page
42    /// in the pool was unused for the entire period, it will be deallocated. This period is
43    /// approximmate, as checks are only done occasionally.
44    pub dealloc_period: Option<u64>,
45}
46
47/// High level configuration of memory management.
48#[derive(Clone, Debug)]
49pub enum MemoryConfiguration {
50    /// Opt-in workload-sized pages with relocation of unpinned live allocations.
51    /// Resolved resources and native graphs keep their addresses fixed.
52    #[cfg(not(exclusive_memory_only))]
53    Adaptive,
54    /// The default preset, which uses pools that allocate sub slices.
55    #[cfg(not(exclusive_memory_only))]
56    SubSlices,
57    /// Default preset for using exclusive pages.
58    /// This can be necessary for backends don't support sub-slices.
59    ExclusivePages,
60    /// Custom settings.
61    Custom {
62        /// Options for each pool to construct. When allocating, the first
63        /// possible pool will be picked for an allocation.
64        pool_options: Vec<MemoryPoolOptions>,
65    },
66}
67
68#[allow(clippy::derivable_impls)]
69impl Default for MemoryConfiguration {
70    fn default() -> Self {
71        #[cfg(exclusive_memory_only)]
72        {
73            MemoryConfiguration::ExclusivePages
74        }
75        #[cfg(not(exclusive_memory_only))]
76        {
77            MemoryConfiguration::SubSlices
78        }
79    }
80}
81
82impl MemoryConfiguration {
83    /// Whether workload-sized relocatable pages are explicitly enabled.
84    pub fn is_adaptive(&self) -> bool {
85        #[cfg(not(exclusive_memory_only))]
86        { matches!(self, Self::Adaptive) }
87        #[cfg(exclusive_memory_only)]
88        { false }
89    }
90
91    /// Whether this preset uses sliced pages rather than a whole device heap.
92    /// Custom pools retain their explicitly supplied page sizes.
93    pub fn uses_subslices(&self) -> bool {
94        #[cfg(not(exclusive_memory_only))]
95        { matches!(self, Self::SubSlices | Self::Adaptive) }
96        #[cfg(exclusive_memory_only)]
97        { false }
98    }
99}