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 = tracy_client::ProfiledAllocator<AllocatorInner>;
24        tracy_client::register_demangler!();
25        const fn new_allocator_wrapper() -> AllocatorWrapper {
26            AllocatorWrapper::new(AllocatorInner {}, 100)
27        }
28    } else {
29        type AllocatorWrapper = AllocatorInner;
30        const fn new_allocator_wrapper() -> AllocatorWrapper {
31            AllocatorInner {}
32        }
33    }
34}
35
36pub type Allocator = AllocatorWrapper;
37
38/// Creates a new [allocator][Allocator].
39pub const fn new_allocator() -> Allocator {
40    new_allocator_wrapper()
41}