Skip to main content

foundry_cli/utils/
allocator.rs

1//! Abstract global allocator implementation.
2
3#[cfg(all(feature = "jemalloc", unix))]
4use tikv_jemallocator as _;
5
6#[cfg(feature = "mimalloc")]
7use mimalloc as _;
8
9// If neither jemalloc nor mimalloc are enabled, use explicitly the system allocator.
10// By default jemalloc is enabled on Unix systems.
11cfg_if::cfg_if! {
12    if #[cfg(all(feature = "jemalloc", unix))] {
13        type AllocatorInner = tikv_jemallocator::Jemalloc;
14    } else if #[cfg(feature = "mimalloc")] {
15        type AllocatorInner = mimalloc::MiMalloc;
16    } else {
17        type AllocatorInner = std::alloc::System;
18    }
19}
20
21// Wrap the allocator if the `tracy-allocator` feature is enabled.
22cfg_if::cfg_if! {
23    if #[cfg(feature = "tracy-allocator")] {
24        type AllocatorWrapper = tracing_tracy::client::ProfiledAllocator<AllocatorInner>;
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}