#[cfg(feature = "fast_allocator_options")]
use libmimalloc_sys as mimalloc_opts;
#[cfg(feature = "fast_allocator_options")]
pub struct MiMallocOpts {
mem_os_alloc: usize,
mem_huge_alloc: usize,
mem_arena_alloc: usize,
huge_pages: bool,
large_pages: bool,
}
#[cfg(feature = "fast_allocator_options")]
impl Default for MiMallocOpts {
fn default() -> Self {
Self::new()
}
}
#[cfg(feature = "fast_allocator_options")]
impl MiMallocOpts {
pub const fn new() -> Self {
use crate::mem::constants::mimalloc_constants::*;
Self {
mem_os_alloc: DEFAULT_GLOBAL_MIMALLOC_ALLOCATION as usize,
mem_huge_alloc: 1,
mem_arena_alloc: (DEFAULT_GLOBAL_MIMALLOC_ALLOCATION / 2) as usize,
huge_pages: true,
large_pages: true,
}
}
#[inline(always)]
pub const fn builder() -> Self {
Self::new()
}
#[inline(always)]
pub const fn set_os_alloc(&mut self, mem_alloc: usize) -> &Self {
self.mem_os_alloc = mem_alloc;
self
}
#[inline(always)]
pub const fn set_huge_alloc(&mut self, mem_alloc: usize) -> &Self {
self.mem_huge_alloc = mem_alloc;
self
}
#[inline(always)]
pub const fn set_arena_alloc(&mut self, mem_alloc: usize) -> &Self {
self.mem_arena_alloc = mem_alloc;
self
}
#[inline(always)]
pub const fn enable_huge_pages(&mut self) -> &Self {
self.huge_pages = true;
self
}
#[inline(always)]
pub const fn enable_large_pages(&mut self) -> &Self {
self.large_pages = true;
self
}
#[inline(always)]
pub fn apply(&self) -> &Self {
use crate::mem::constants::mimalloc_constants::*;
use std::ffi::c_long;
unsafe {
mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_large_os_pages, self.large_pages);
mimalloc_opts::mi_option_set_enabled(OPT_ALLOW_THP, self.huge_pages);
mimalloc_opts::mi_option_set(mimalloc_opts::mi_option_reserve_huge_os_pages, self.mem_huge_alloc as c_long);
mimalloc_opts::mi_option_set(OPT_ARENA_RESERVE, self.mem_arena_alloc as c_long);
mimalloc_opts::mi_option_set(OPT_RESERVE_OS_MEMORY, self.mem_os_alloc as c_long);
}
self
}
#[inline(always)]
pub fn enable_debug(&self) -> &Self {
unsafe {
mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_show_stats, true);
mimalloc_opts::mi_option_set_enabled(mimalloc_opts::mi_option_verbose, true);
}
self
}
}