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