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// This is to prevent clippy unused warnings when we do `--all-features`
19cfg_if::cfg_if! {
20    if #[cfg(all(feature = "snmalloc", feature = "jemalloc", unix))] {
21        use snmalloc_rs as _;
22    }
23}
24
25cfg_if::cfg_if! {
26    if #[cfg(feature = "tracy-allocator")] {
27        type AllocatorWrapper = tracy_client::ProfiledAllocator<AllocatorInner>;
28        tracy_client::register_demangler!();
29        const fn new_allocator_wrapper() -> AllocatorWrapper {
30            AllocatorWrapper::new(AllocatorInner {}, 100)
31        }
32    } else {
33        type AllocatorWrapper = AllocatorInner;
34        const fn new_allocator_wrapper() -> AllocatorWrapper {
35            AllocatorInner {}
36        }
37    }
38}
39
40/// Custom allocator.
41pub type Allocator = AllocatorWrapper;
42
43/// Creates a new [custom allocator][Allocator].
44pub const fn new_allocator() -> Allocator {
45    new_allocator_wrapper()
46}