foundry_cli/utils/
allocator.rs

1//! Abstract global allocator implementation.
2
3#[cfg(feature = "mimalloc")]
4use mimalloc as _;
5#[cfg(all(feature = "jemalloc", unix))]
6use tikv_jemallocator as _;
7
8// If neither jemalloc nor mimalloc are enabled, use explicitly the system allocator.
9// By default jemalloc is enabled on Unix systems.
10cfg_if::cfg_if! {
11    if #[cfg(all(feature = "jemalloc", unix))] {
12        type AllocatorInner = tikv_jemallocator::Jemalloc;
13    } else if #[cfg(feature = "mimalloc")] {
14        type AllocatorInner = mimalloc::MiMalloc;
15    } else {
16        type AllocatorInner = std::alloc::System;
17    }
18}
19
20// Wrap the allocator if the `tracy-allocator` feature is enabled.
21cfg_if::cfg_if! {
22    if #[cfg(feature = "tracy-allocator")] {
23        type AllocatorWrapper = tracing_tracy::client::ProfiledAllocator<AllocatorInner>;
24        const fn new_allocator_wrapper() -> AllocatorWrapper {
25            AllocatorWrapper::new(AllocatorInner {}, 100)
26        }
27    } else {
28        type AllocatorWrapper = AllocatorInner;
29        const fn new_allocator_wrapper() -> AllocatorWrapper {
30            AllocatorInner {}
31        }
32    }
33}
34
35pub type Allocator = AllocatorWrapper;
36
37/// Creates a new [allocator][Allocator].
38pub const fn new_allocator() -> Allocator {
39    new_allocator_wrapper()
40}