Skip to main content

rumtk_arena/mem/
alloc_opts.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D. <lsantos@medicalmasses.com>
5 *     Copyright (C) 2026  MedicalMasses L.L.C. <contact@medicalmasses.com>
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20#[cfg(feature = "fast_allocator_options")]
21use libmimalloc_sys as mimalloc_opts;
22
23
24#[cfg(feature = "fast_allocator_options")]
25pub struct MiMallocOpts {
26    mem_os_alloc: usize,
27    mem_huge_alloc: usize,
28    mem_arena_alloc: usize,
29    huge_pages: bool,
30    large_pages: bool,
31}
32
33#[cfg(feature = "fast_allocator_options")]
34impl Default for MiMallocOpts {
35    fn default() -> Self {
36        Self::new()
37    }
38}
39
40#[cfg(feature = "fast_allocator_options")]
41impl MiMallocOpts {
42    pub const fn new() -> Self {
43        use crate::mem::constants::mimalloc_constants::*;
44        Self {
45            mem_os_alloc: DEFAULT_GLOBAL_MIMALLOC_ALLOCATION as usize,
46            mem_huge_alloc: 1,
47            mem_arena_alloc: (DEFAULT_GLOBAL_MIMALLOC_ALLOCATION / 2) as usize,
48            huge_pages: true,
49            large_pages: true,
50        }
51    }
52
53    #[inline(always)]
54    pub const fn builder() -> Self {
55        Self::new()
56    }
57
58    #[inline(always)]
59    pub const fn set_os_alloc(&mut self, mem_alloc: usize) -> &mut Self {
60        self.mem_os_alloc = mem_alloc;
61        self
62    }
63
64    #[inline(always)]
65    pub const fn set_huge_alloc(&mut self, mem_alloc: usize) -> &mut Self {
66        self.mem_huge_alloc = mem_alloc;
67        self
68    }
69
70    #[inline(always)]
71    pub const fn set_arena_alloc(&mut self, mem_alloc: usize) -> &mut Self {
72        self.mem_arena_alloc = mem_alloc;
73        self
74    }
75
76    #[inline(always)]
77    pub const fn enable_huge_pages(&mut self) -> &mut Self {
78        self.huge_pages = true;
79        self
80    }
81
82    #[inline(always)]
83    pub const fn enable_large_pages(&mut self) -> &mut Self {
84        self.large_pages = true;
85        self
86    }
87
88    #[inline(always)]
89    pub fn apply(&self) -> &Self {
90        use crate::mem::constants::mimalloc_constants::*;
91        use std::ffi::c_long;
92        unsafe {
93            mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_large_os_pages, self.large_pages);
94            mimalloc_opts::mi_option_set_enabled(OPT_ALLOW_THP, self.huge_pages);
95            mimalloc_opts::mi_option_set(mimalloc_opts::mi_option_reserve_huge_os_pages, self.mem_huge_alloc as c_long);
96            mimalloc_opts::mi_option_set(OPT_ARENA_RESERVE, self.mem_arena_alloc as c_long);
97            mimalloc_opts::mi_option_set(OPT_RESERVE_OS_MEMORY, self.mem_os_alloc as c_long);
98        }
99        self
100    }
101
102    #[inline(always)]
103    pub fn enable_debug(&self) -> &Self {
104        unsafe {
105            mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_show_stats, true);
106            mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_verbose, true);
107        }
108        self
109    }
110}