kaspa_alloc/
lib.rs

1#[cfg(not(feature = "heap"))]
2#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
3extern "C" {
4    fn mi_option_set_enabled(_: mi_option_e, val: bool);
5}
6
7#[cfg(not(feature = "heap"))]
8#[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
9#[allow(non_camel_case_types)]
10#[allow(dead_code)]
11#[repr(C)]
12enum mi_option_e {
13    // stable options
14    mi_option_show_errors, // print error messages
15    mi_option_show_stats,  // print statistics on termination
16    mi_option_verbose,     // print verbose messages
17    // the following options are experimental (see src/options.h)
18    mi_option_eager_commit,             // eager commit segments? (after `eager_commit_delay` segments) (=1)
19    mi_option_arena_eager_commit,       // eager commit arenas? Use 2 to enable just on overcommit systems (=2)
20    mi_option_purge_decommits,          // should a memory purge decommit (or only reset) (=1)
21    mi_option_allow_large_os_pages,     // allow large (2MiB) OS pages, implies eager commit
22    mi_option_reserve_huge_os_pages,    // reserve N huge OS pages (1GiB/page) at startup
23    mi_option_reserve_huge_os_pages_at, // reserve huge OS pages at a specific NUMA node
24    mi_option_reserve_os_memory,        // reserve specified amount of OS memory in an arena at startup
25    mi_option_deprecated_segment_cache,
26    mi_option_deprecated_page_reset,
27    mi_option_abandoned_page_purge, // immediately purge delayed purges on thread termination
28    mi_option_deprecated_segment_reset,
29    mi_option_eager_commit_delay,
30    mi_option_purge_delay, // memory purging is delayed by N milli seconds; use 0 for immediate purging or -1 for no purging at all.
31    mi_option_use_numa_nodes, // 0 = use all available numa nodes, otherwise use at most N nodes.
32    mi_option_limit_os_alloc, // 1 = do not use OS memory for allocation (but only programmatically reserved arenas)
33    mi_option_os_tag,      // tag used for OS logging (macOS only for now)
34    mi_option_max_errors,  // issue at most N error messages
35    mi_option_max_warnings, // issue at most N warning messages
36    mi_option_max_segment_reclaim,
37    mi_option_destroy_on_exit, // if set, release all memory on exit; sometimes used for dynamic unloading but can be unsafe.
38    mi_option_arena_reserve,   // initial memory size in KiB for arena reservation (1GiB on 64-bit)
39    mi_option_arena_purge_mult,
40    mi_option_purge_extend_delay,
41    _mi_option_last,
42}
43
44#[cfg(not(feature = "heap"))]
45use mimalloc::MiMalloc;
46#[cfg(not(feature = "heap"))]
47#[global_allocator]
48static GLOBAL: MiMalloc = MiMalloc;
49
50pub fn init_allocator_with_default_settings() {
51    #[cfg(any(target_os = "linux", target_os = "macos", target_os = "windows"))]
52    #[cfg(not(feature = "heap"))]
53    unsafe {
54        // Empirical tests show that this option results in the smallest RSS.
55        mi_option_set_enabled(mi_option_e::mi_option_purge_decommits, false)
56    };
57}