use core::alloc::{GlobalAlloc, Layout};
use std::alloc::System;
#[cfg(target_os = "linux")]
use libdd_profiling_heap_sampler::{
dd_allocation_created, dd_allocation_freed, dd_allocation_requested,
};
pub struct SampledAllocator<A> {
inner: A,
}
impl<A> SampledAllocator<A> {
pub const fn new(inner: A) -> Self {
Self { inner }
}
}
impl SampledAllocator<System> {
pub const DEFAULT: Self = Self { inner: System };
}
unsafe impl<A: GlobalAlloc> GlobalAlloc for SampledAllocator<A> {
#[cfg(target_os = "linux")]
#[inline]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe {
if !libdd_profiling_heap_sampler::heap_sampling_enabled() {
return self.inner.alloc(layout);
}
let req = dd_allocation_requested(layout.size(), layout.align());
let inner_layout = Layout::from_size_align_unchecked(req.size, layout.align());
let raw = self.inner.alloc(inner_layout);
dd_allocation_created(raw.cast(), req).cast()
}
}
#[cfg(not(target_os = "linux"))]
#[inline(always)]
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe { self.inner.alloc(layout) }
}
#[cfg(target_os = "linux")]
#[inline]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe {
if !libdd_profiling_heap_sampler::heap_sampling_enabled() {
return self.inner.dealloc(ptr, layout);
}
let freed = dd_allocation_freed(ptr.cast(), layout.size(), layout.align());
let inner_layout = Layout::from_size_align_unchecked(freed.size, layout.align());
self.inner.dealloc(freed.ptr.cast(), inner_layout);
}
}
#[cfg(not(target_os = "linux"))]
#[inline(always)]
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe {
self.inner.dealloc(ptr, layout);
}
}
}
#[cfg(all(test, not(miri)))]
mod tests {
use super::*;
use core::sync::atomic::{AtomicUsize, Ordering};
#[cfg(target_os = "linux")]
use libdd_profiling_heap_sampler::dd_tl_state_get;
struct CountingSystem {
alloc_count: AtomicUsize,
dealloc_count: AtomicUsize,
last_alloc_size: AtomicUsize,
}
impl CountingSystem {
const fn new() -> Self {
Self {
alloc_count: AtomicUsize::new(0),
dealloc_count: AtomicUsize::new(0),
last_alloc_size: AtomicUsize::new(0),
}
}
}
unsafe impl GlobalAlloc for CountingSystem {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
unsafe {
self.alloc_count.fetch_add(1, Ordering::Relaxed);
self.last_alloc_size.store(layout.size(), Ordering::Relaxed);
System.alloc(layout)
}
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
unsafe {
self.dealloc_count.fetch_add(1, Ordering::Relaxed);
System.dealloc(ptr, layout);
}
}
}
#[test]
fn alloc_dealloc_forwards_to_inner() {
let sampled = SampledAllocator::new(CountingSystem::new());
let layout = Layout::from_size_align(128, 16).unwrap();
unsafe {
let p = sampled.alloc(layout);
assert!(!p.is_null());
assert_eq!(sampled.inner.alloc_count.load(Ordering::Relaxed), 1);
assert!(sampled.inner.last_alloc_size.load(Ordering::Relaxed) >= 128);
sampled.dealloc(p, layout);
assert_eq!(sampled.inner.dealloc_count.load(Ordering::Relaxed), 1);
}
}
#[cfg(target_os = "linux")]
#[test]
fn alloc_skips_tls_when_no_profiler_attached() {
std::thread::spawn(|| unsafe {
assert!(dd_tl_state_get().is_null());
let sampled = SampledAllocator::<System>::DEFAULT;
let layout = Layout::from_size_align(64, 8).unwrap();
let p = sampled.alloc(layout);
assert!(!p.is_null());
assert!(dd_tl_state_get().is_null());
sampled.dealloc(p, layout);
})
.join()
.unwrap();
}
#[test]
fn default_matches_new_system() {
let _ = SampledAllocator::<System>::DEFAULT;
let _ = SampledAllocator::new(System);
}
}