handle the case where 0 or >1 allocs are enabled

In particular this fixes `cargo build --all-features`.
This commit is contained in:
Charles Hall 2024-04-27 16:08:50 -07:00 committed by June
parent e0c0d51a05
commit 3b05417246
5 changed files with 29 additions and 24 deletions

7
src/alloc/default.rs Normal file
View file

@ -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() }

View file

@ -1,5 +1,3 @@
#![cfg(all(not(target_env = "msvc"), feature = "hardened_malloc", target_os = "linux", not(feature = "jemalloc")))]
#[global_allocator] #[global_allocator]
static HMALLOC: hardened_malloc_rs::HardenedMalloc = hardened_malloc_rs::HardenedMalloc; static HMALLOC: hardened_malloc_rs::HardenedMalloc = hardened_malloc_rs::HardenedMalloc;

View file

@ -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 std::ffi::{c_char, c_void};
use tikv_jemalloc_ctl as mallctl; use tikv_jemalloc_ctl as mallctl;
@ -10,8 +7,6 @@ use tikv_jemallocator as jemalloc;
#[global_allocator] #[global_allocator]
static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc; static JEMALLOC: jemalloc::Jemalloc = jemalloc::Jemalloc;
pub(crate) fn version() -> &'static str { mallctl::version::read().expect("version string") }
pub(crate) fn memory_usage() -> String { pub(crate) fn memory_usage() -> String {
use mallctl::stats; use mallctl::stats;
let allocated = stats::allocated::read().unwrap_or_default() as f64 / 1024.0 / 1024.0; let allocated = stats::allocated::read().unwrap_or_default() as f64 / 1024.0 / 1024.0;

View file

@ -1,20 +1,25 @@
pub(crate) mod hardened; //! Integration with allocators
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() }
// jemalloc
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))] #[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))]
pub(crate) fn memory_usage() -> String { je::memory_usage() } mod je;
#[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() }
#[cfg(all(not(target_env = "msvc"), feature = "jemalloc", not(feature = "hardened_malloc")))] #[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"))))] // hardened_malloc
pub(crate) fn memory_stats() -> String { String::default() } #[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};

View file

@ -49,7 +49,7 @@ use utils::{
error::{Error, Result}, error::{Error, Result},
}; };
mod alloc; pub(crate) mod alloc;
mod api; mod api;
mod config; mod config;
mod database; mod database;