Skip to main content

reifydb_allocator/
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(all(feature = "alloc-jemalloc", not(target_env = "msvc")))]
10use reifydb_allocator_jemalloc::{stats, stats_dump};
11
12#[cfg(all(feature = "alloc-jemalloc", not(target_env = "msvc")))]
13pub mod backend {
14	use reifydb_allocator_jemalloc::{ALLOCATOR as JEMALLOC_ALLOCATOR, verify as jemalloc_verify};
15
16	pub type Allocator = reifydb_allocator_jemalloc::Allocator;
17	pub const ALLOCATOR: Allocator = JEMALLOC_ALLOCATOR;
18
19	pub fn verify() {
20		jemalloc_verify();
21	}
22}
23
24#[cfg(all(feature = "alloc-jemalloc", target_env = "msvc"))]
25compile_error!(
26	"reifydb-allocator: jemalloc is not supported on MSVC. \
27	 Use the (Phase 2) `alloc-mimalloc` feature instead. \
28	 See reifydb/plan-alloc.md."
29);
30
31#[macro_export]
32#[cfg(feature = "alloc-jemalloc")]
33macro_rules! set_global_allocator {
34	() => {
35		#[global_allocator]
36		static REIFYDB_GLOBAL_ALLOCATOR: $crate::backend::Allocator = $crate::backend::ALLOCATOR;
37	};
38}
39
40#[macro_export]
41#[cfg(not(feature = "alloc-jemalloc"))]
42macro_rules! set_global_allocator {
43	() => {};
44}
45
46#[cfg(feature = "alloc-jemalloc")]
47pub fn verify() {
48	backend::verify();
49}
50
51#[cfg(not(feature = "alloc-jemalloc"))]
52pub fn verify() {}
53
54pub struct JemallocStats {
55	pub allocated: u64,
56	pub active: u64,
57	pub resident: u64,
58	pub mapped: u64,
59	pub retained: u64,
60	pub metadata: u64,
61}
62
63#[cfg(all(feature = "alloc-jemalloc", not(target_env = "msvc")))]
64pub fn jemalloc_stats() -> Option<JemallocStats> {
65	let (allocated, active, resident, mapped, retained, metadata) = stats();
66	Some(JemallocStats {
67		allocated,
68		active,
69		resident,
70		mapped,
71		retained,
72		metadata,
73	})
74}
75
76#[cfg(not(all(feature = "alloc-jemalloc", not(target_env = "msvc"))))]
77pub fn jemalloc_stats() -> Option<JemallocStats> {
78	None
79}
80
81#[cfg(all(feature = "alloc-jemalloc", not(target_env = "msvc")))]
82pub fn jemalloc_stats_dump() -> Option<String> {
83	stats_dump()
84}
85
86#[cfg(not(all(feature = "alloc-jemalloc", not(target_env = "msvc"))))]
87pub fn jemalloc_stats_dump() -> Option<String> {
88	None
89}