mod arena;
pub mod background;
mod base;
mod chunk;
#[cfg(all(unix, not(miri)))]
mod fork;
mod huge;
mod lock;
mod malloc;
mod os;
pub mod prof;
mod size_class;
mod stats;
mod tcache;
pub use stats::{Stats, stats};
use core::alloc::{GlobalAlloc, Layout};
#[derive(Debug, Clone, Copy, Default)]
pub struct Jemalloc {
profiling: bool,
}
impl Jemalloc {
#[must_use]
pub const fn new() -> Self {
Jemalloc { profiling: false }
}
#[must_use]
pub const fn with_profiling(self) -> Self {
Jemalloc { profiling: true }
}
}
unsafe impl GlobalAlloc for Jemalloc {
#[inline(never)]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
malloc::alloc(layout.size(), layout.align(), self.profiling)
}
#[inline(never)]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
malloc::dealloc(ptr, layout.size(), layout.align());
}
#[inline(never)]
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
malloc::alloc_zeroed(layout.size(), layout.align(), self.profiling)
}
#[inline(never)]
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
malloc::realloc(ptr, layout.size(), layout.align(), new_size, self.profiling)
}
}
pub fn purge() {
unsafe {
let t = tcache::current();
if !t.is_null() {
tcache::flush_all(&mut *t);
}
arena::purge();
huge::purge();
}
}