Skip to main content

reth_cli_util/
allocator.rs

1//! Custom allocator implementation.
2//!
3//! We provide support for jemalloc and snmalloc on unix systems, and prefer jemalloc if both are
4//! enabled.
5
6// We provide jemalloc allocator support, alongside snmalloc. If both features are enabled, jemalloc
7// is prioritized.
8cfg_if::cfg_if! {
9    if #[cfg(all(feature = "jemalloc", unix))] {
10        type AllocatorInner = tikv_jemallocator::Jemalloc;
11    } else if #[cfg(all(feature = "snmalloc", unix))] {
12        type AllocatorInner = snmalloc_rs::SnMalloc;
13    } else {
14        type AllocatorInner = std::alloc::System;
15    }
16}
17
18// Re-export jemalloc-sys so that binaries can `use` it in main.rs to make it
19// visible to the linker, which is required for `override_allocator_on_supported_platforms`.
20#[cfg(all(feature = "jemalloc", unix))]
21pub use tikv_jemalloc_sys;
22
23// This is to prevent clippy unused warnings when we do `--all-features`
24cfg_if::cfg_if! {
25    if #[cfg(all(feature = "snmalloc", feature = "jemalloc", unix))] {
26        use snmalloc_rs as _;
27    }
28}
29
30cfg_if::cfg_if! {
31    if #[cfg(feature = "tracy-allocator")] {
32        type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>;
33        const fn new_allocator_wrapper() -> AllocatorWrapper {
34            AllocatorWrapper::new(AllocatorInner {}, 100)
35        }
36    } else {
37        type AllocatorWrapper = AllocatorInner;
38        const fn new_allocator_wrapper() -> AllocatorWrapper {
39            AllocatorInner {}
40        }
41    }
42}
43
44/// Custom allocator.
45pub type Allocator = AllocatorWrapper;
46
47/// Creates a new [custom allocator][Allocator].
48pub const fn new_allocator() -> Allocator {
49    new_allocator_wrapper()
50}