1#![no_std]
40
41extern crate alloc;
42
43#[cfg(any(feature = "heap", feature = "cpu"))]
45mod profiling;
46
47#[cfg(feature = "cpu")]
49pub use profiling::{start_cpu_profiling, stop_cpu_profiling};
50
51#[cfg(not(feature = "cpu"))]
53#[inline]
54pub fn start_cpu_profiling(_freq_hz: u32) {}
55
56#[cfg(not(feature = "cpu"))]
57#[inline]
58pub fn stop_cpu_profiling() {}
59
60pub struct ProfilingAllocator;
66
67#[cfg(not(feature = "heap"))]
68mod disabled {
69 use super::ProfilingAllocator;
70 use core::alloc::{GlobalAlloc, Layout};
71
72 unsafe impl GlobalAlloc for ProfilingAllocator {
73 #[inline]
74 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
75 unsafe { libc::malloc(layout.size()) as *mut u8 }
76 }
77
78 #[inline]
79 unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
80 unsafe { libc::free(ptr as *mut libc::c_void) }
81 }
82
83 #[inline]
84 unsafe fn realloc(&self, ptr: *mut u8, _layout: Layout, new_size: usize) -> *mut u8 {
85 unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 }
86 }
87
88 #[inline]
89 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
90 unsafe { libc::calloc(1, layout.size()) as *mut u8 }
91 }
92 }
93}
94
95#[cfg(feature = "heap")]
96mod enabled {
97 use super::{ProfilingAllocator, profiling};
98 use core::alloc::{GlobalAlloc, Layout};
99
100 unsafe impl GlobalAlloc for ProfilingAllocator {
101 #[inline]
102 unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
103 let ptr = unsafe { libc::malloc(layout.size()) as *mut u8 };
104 if !ptr.is_null() {
105 profiling::record_alloc(ptr, layout.size());
106 }
107 ptr
108 }
109
110 #[inline]
111 unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
112 profiling::record_dealloc(ptr, layout.size());
113 unsafe { libc::free(ptr as *mut libc::c_void) }
114 }
115
116 #[inline]
117 unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
118 profiling::record_dealloc(ptr, layout.size());
119 let new_ptr = unsafe { libc::realloc(ptr as *mut libc::c_void, new_size) as *mut u8 };
120 if !new_ptr.is_null() {
121 profiling::record_alloc(new_ptr, new_size);
122 }
123 new_ptr
124 }
125
126 #[inline]
127 unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
128 let ptr = unsafe { libc::calloc(1, layout.size()) as *mut u8 };
129 if !ptr.is_null() {
130 profiling::record_alloc(ptr, layout.size());
131 }
132 ptr
133 }
134 }
135}