From 3b05417246f4c01d1298049c054e22b431564b2c Mon Sep 17 00:00:00 2001 From: Charles Hall Date: Sat, 27 Apr 2024 16:08:50 -0700 Subject: [PATCH] handle the case where 0 or >1 allocs are enabled In particular this fixes `cargo build --all-features`. --- src/alloc/default.rs | 7 +++++++ src/alloc/hardened.rs | 2 -- src/alloc/je.rs | 5 ----- src/alloc/mod.rs | 37 +++++++++++++++++++++---------------- src/main.rs | 2 +- 5 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 src/alloc/default.rs diff --git a/src/alloc/default.rs b/src/alloc/default.rs new file mode 100644 index 00000000..1d61682e --- /dev/null +++ b/src/alloc/default.rs @@ -0,0 +1,7 @@ +//! Default allocator with no special features + +/// Always returns the empty string +pub(crate) fn memory_stats() -> String { Default::default() } + +/// Always returns the empty string +pub(crate) fn memory_usage() -> String { Default::default() } diff --git a/src/alloc/hardened.rs b/src/alloc/hardened.rs index 023cb487..9ac84f9a 100644 --- a/src/alloc/hardened.rs +++ b/src/alloc/hardened.rs @@ -1,5 +1,3 @@ -#![cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))] - #[global_allocator] static HMALLOC: hardened_malloc_rs::HardenedMalloc = hardened_malloc_rs::HardenedMalloc; diff --git a/src/alloc/je.rs b/src/alloc/je.rs index db6a56cf..d17c4f0b 100644 --- a/src/alloc/je.rs +++ b/src/alloc/je.rs @@ -1,6 +1,3 @@ -#![cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))] -#![allow(dead_code)] - use std::ffi::{c_char, c_void}; use tikv_jemalloc_ctl as mallctl; @@ -10,8 +7,6 @@ use tikv_jemallocator as jemalloc; #[global_allocator] static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc; -pub(crate) fn version() -> &'static str { mallctl::version::read().expect("version string") } - pub(crate) fn memory_usage() -> String { use mallctl::stats; let allocated = stats::allocated::read().unwrap_or_default() as f64 / 1024.0 / 1024.0; diff --git a/src/alloc/mod.rs b/src/alloc/mod.rs index 775fe8c2..6b7c89a1 100644 --- a/src/alloc/mod.rs +++ b/src/alloc/mod.rs @@ -1,20 +1,25 @@ -pub(crate) mod hardened; -pub(crate) mod je; - -#[cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))] -pub(crate) fn memory_usage() -> String { hardened::memory_usage() } +//! Integration with allocators +// jemalloc #[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))] -pub(crate) fn memory_usage() -> String { je::memory_usage() } - -#[cfg(any(target_env = "msvc", all(not(feature = "jemalloc"), not(feature = "hardened_malloc"))))] -pub(crate) fn memory_usage() -> String { String::default() } - -#[cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))] -pub(crate) fn memory_stats() -> String { hardened::memory_stats() } - +mod je; #[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))] -pub(crate) fn memory_stats() -> String { je::memory_stats() } +pub(crate) use je::{memory_stats, memory_usage}; -#[cfg(any(target_env = "msvc", all(not(feature = "jemalloc"), not(feature = "hardened_malloc"))))] -pub(crate) fn memory_stats() -> String { String::default() } +// hardened_malloc +#[cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))] +mod hardened; +#[cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))] +pub(crate) use hardened::{memory_stats, memory_usage}; + +// default, enabled when none or multiple of the above are enabled +#[cfg(any( + not(any(feature = "jemalloc", feature = "hardened_malloc")), + all(feature = "jemalloc", feature = "hardened_malloc"), +))] +mod default; +#[cfg(any( + not(any(feature = "jemalloc", feature = "hardened_malloc")), + all(feature = "jemalloc", feature = "hardened_malloc"), +))] +pub(crate) use default::{memory_stats, memory_usage}; diff --git a/src/main.rs b/src/main.rs index 3bf77fa1..f5da0f40 100644 --- a/src/main.rs +++ b/src/main.rs @@ -49,7 +49,7 @@ use utils::{ error::{Error, Result}, }; -mod alloc; +pub(crate) mod alloc; mod api; mod config; mod database;