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 const fn new_allocator_wrapper() -> AllocatorWrapper {
29 AllocatorWrapper::new(AllocatorInner {}, 100)
30 }
31 } else {
32 type AllocatorWrapper = AllocatorInner;
33 const fn new_allocator_wrapper() -> AllocatorWrapper {
34 AllocatorInner {}
35 }
36 }
37}
38
39/// Custom allocator.
40pub type Allocator = AllocatorWrapper;
41
42/// Creates a new [custom allocator][Allocator].
43pub const fn new_allocator() -> Allocator {
44 new_allocator_wrapper()
45}