Skip to main content

reifydb_allocator_jemalloc/
lib.rs

1// SPDX-License-Identifier: Apache-2.0
2// Copyright (c) 2026 ReifyDB
3
4#![cfg_attr(not(debug_assertions), deny(clippy::disallowed_methods))]
5#![cfg_attr(debug_assertions, warn(clippy::disallowed_methods))]
6#![cfg_attr(not(debug_assertions), deny(warnings))]
7#![allow(clippy::tabs_in_doc_comments)]
8
9#[cfg(not(target_env = "msvc"))]
10use tikv_jemalloc_ctl::{
11	epoch, stats,
12	stats_print::{self, Options},
13};
14#[cfg(not(target_env = "msvc"))]
15use tikv_jemallocator::Jemalloc;
16
17#[cfg(not(target_env = "msvc"))]
18pub type Allocator = Jemalloc;
19
20#[cfg(not(target_env = "msvc"))]
21pub const ALLOCATOR: Allocator = Jemalloc;
22
23#[cfg(not(target_env = "msvc"))]
24pub fn verify() {
25	epoch::advance().expect(
26		"reifydb-allocator-jemalloc: jemalloc epoch advance failed; jemalloc is not the active allocator for this process",
27	);
28	let allocated = stats::allocated::read()
29		.expect("reifydb-allocator-jemalloc: jemalloc stats::allocated read failed; jemalloc is not the active allocator for this process");
30	assert!(
31		allocated > 0,
32		"reifydb-allocator-jemalloc: jemalloc reports 0 allocated bytes at startup; allocator is not bound to this process"
33	);
34}
35
36#[cfg(not(target_env = "msvc"))]
37pub fn stats() -> (u64, u64, u64, u64, u64, u64) {
38	let _ = epoch::advance();
39	(
40		stats::allocated::read().unwrap_or(0) as u64,
41		stats::active::read().unwrap_or(0) as u64,
42		stats::resident::read().unwrap_or(0) as u64,
43		stats::mapped::read().unwrap_or(0) as u64,
44		stats::retained::read().unwrap_or(0) as u64,
45		stats::metadata::read().unwrap_or(0) as u64,
46	)
47}
48
49#[cfg(not(target_env = "msvc"))]
50pub fn stats_dump() -> Option<String> {
51	let _ = epoch::advance();
52	let mut options = Options::default();
53	options.skip_per_arena = true;
54	options.skip_mutex_statistics = true;
55	let mut buf = Vec::new();
56	stats_print::stats_print(&mut buf, options).ok()?;
57	String::from_utf8(buf).ok()
58}
59
60#[cfg(target_env = "msvc")]
61compile_error!("reifydb-allocator-jemalloc does not support MSVC; use the (Phase 2) mimalloc backend instead");