libdd_profiling_heap_allocator/
allocator.rs1use core::alloc::{GlobalAlloc, Layout};
5use std::alloc::System;
6
7#[cfg(target_os = "linux")]
8use libdd_profiling_heap_sampler::{
9 dd_allocation_created, dd_allocation_freed, dd_allocation_requested,
10};
11
12pub struct SampledAllocator<A> {
23 inner: A,
24}
25
26impl<A> SampledAllocator<A> {
27 pub const fn new(inner: A) -> Self {
30 Self { inner }
31 }
32}
33
34impl SampledAllocator<System> {
35 pub const DEFAULT: Self = Self { inner: System };
38}
39
40unsafe impl<A: GlobalAlloc> GlobalAlloc for SampledAllocator<A> {
41 #[cfg(target_os = "linux")]
42 #[inline]
43 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
44 if !libdd_profiling_heap_sampler::heap_sampling_enabled() {
50 return self.inner.alloc(layout);
51 }
52 let req = dd_allocation_requested(layout.size(), layout.align());
53 let inner_layout = Layout::from_size_align_unchecked(req.size, layout.align());
56 let raw = self.inner.alloc(inner_layout);
57 dd_allocation_created(raw.cast(), req).cast()
58 }
59
60 #[cfg(not(target_os = "linux"))]
61 #[inline(always)]
62 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
63 self.inner.alloc(layout)
64 }
65
66 #[cfg(target_os = "linux")]
67 #[inline]
68 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
69 if !libdd_profiling_heap_sampler::heap_sampling_enabled() {
71 return self.inner.dealloc(ptr, layout);
72 }
73 let freed = dd_allocation_freed(ptr.cast(), layout.size(), layout.align());
74 let inner_layout = Layout::from_size_align_unchecked(freed.size, layout.align());
82 self.inner.dealloc(freed.ptr.cast(), inner_layout);
83 }
84
85 #[cfg(not(target_os = "linux"))]
86 #[inline(always)]
87 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
88 self.inner.dealloc(ptr, layout);
89 }
90}
91
92#[cfg(all(test, not(miri)))]
95mod tests {
96 use super::*;
97 use core::sync::atomic::{AtomicUsize, Ordering};
98 #[cfg(target_os = "linux")]
99 use libdd_profiling_heap_sampler::dd_tl_state_get;
100
101 struct CountingSystem {
105 alloc_count: AtomicUsize,
106 dealloc_count: AtomicUsize,
107 last_alloc_size: AtomicUsize,
108 }
109
110 impl CountingSystem {
111 const fn new() -> Self {
112 Self {
113 alloc_count: AtomicUsize::new(0),
114 dealloc_count: AtomicUsize::new(0),
115 last_alloc_size: AtomicUsize::new(0),
116 }
117 }
118 }
119
120 unsafe impl GlobalAlloc for CountingSystem {
121 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
122 self.alloc_count.fetch_add(1, Ordering::Relaxed);
123 self.last_alloc_size.store(layout.size(), Ordering::Relaxed);
124 System.alloc(layout)
125 }
126 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
127 self.dealloc_count.fetch_add(1, Ordering::Relaxed);
128 System.dealloc(ptr, layout);
129 }
130 }
131
132 #[test]
133 fn alloc_dealloc_forwards_to_inner() {
134 let sampled = SampledAllocator::new(CountingSystem::new());
135 let layout = Layout::from_size_align(128, 16).unwrap();
136
137 unsafe {
138 let p = sampled.alloc(layout);
139 assert!(!p.is_null());
140
141 assert_eq!(sampled.inner.alloc_count.load(Ordering::Relaxed), 1);
142 assert!(sampled.inner.last_alloc_size.load(Ordering::Relaxed) >= 128);
145
146 sampled.dealloc(p, layout);
147 assert_eq!(sampled.inner.dealloc_count.load(Ordering::Relaxed), 1);
148 }
149 }
150
151 #[cfg(target_os = "linux")]
153 #[test]
154 fn lazy_init_populates_tls_on_first_alloc() {
155 std::thread::spawn(|| unsafe {
157 assert!(
158 dd_tl_state_get().is_null(),
159 "fresh thread should have NULL sampler TLS"
160 );
161
162 let sampled = SampledAllocator::<System>::DEFAULT;
163 let layout = Layout::from_size_align(64, 8).unwrap();
164 let p = sampled.alloc(layout);
165 assert!(!p.is_null());
166
167 assert!(
168 !dd_tl_state_get().is_null(),
169 "TLS should be populated after the first alloc"
170 );
171
172 sampled.dealloc(p, layout);
173 })
174 .join()
175 .unwrap();
176 }
177
178 #[test]
179 fn default_matches_new_system() {
180 let _ = SampledAllocator::<System>::DEFAULT;
181 let _ = SampledAllocator::new(System);
182 }
183}