tikv_jemallocator_global/lib.rs
1//! Sets `jemalloc` as the `#[global_allocator]` on targets that support it.
2//!
3//! Just add `jemallocator-global` as a dependency:
4//!
5//! ```toml
6//! # Cargo.toml
7//! [dependencies]
8//! jemallocator-global = "0.6.0"
9//! ```
10//!
11//! and `jemalloc` will be used as the `#[global_allocator]` on targets that
12//! support it.
13//!
14//! To unconditionally set `jemalloc` as the `#[global_allocator]` enable the
15//! `force_global_jemalloc` cargo feature.
16
17#[macro_use]
18extern crate cfg_if;
19
20cfg_if! {
21 if #[cfg(any(
22 feature = "force_global_jemalloc",
23 target_os = "linux",
24 target_os = "macos",
25 target_os = "freebsd",
26 target_os = "openbsd",
27 target_os = "netbsd"
28 ))] {
29 /// Sets `jemalloc` as the `#[global_allocator]`.
30 #[global_allocator]
31 pub static JEMALLOC: tikv_jemallocator::Jemalloc = tikv_jemallocator::Jemalloc;
32 }
33}
34
35#[cfg(test)]
36mod tests {
37 // Test that jemallocator-global is enabled automatically in those targets in
38 // which it should be enabled:
39
40 macro_rules! check {
41 () => {
42 #[test]
43 fn foo() {
44 let _ = super::JEMALLOC;
45 }
46 };
47 ($os_name:tt) => {
48 #[cfg(target_os = $os_name)]
49 check!();
50 };
51 ($($os_name:tt),*) => {
52 $(check!($os_name);)*
53 }
54 }
55
56 // If the `force_global_jemalloc` feature is enabled, then it
57 // should always be set as the global allocator:
58 #[cfg(feature = "force_global_jemalloc")]
59 check!();
60
61 // If the `force_global_jemalloc` feature is not enabled, then in the
62 // following targets it should be automatically enabled anyways:
63 #[cfg(not(feature = "force_global_jemalloc"))]
64 check!("linux", "android", "macos", "ios", "freebsd", "netbsd", "openbsd");
65}