1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
//! Shared code between YJIT and ZJIT.
#![warn(unsafe_op_in_unsafe_fn)] // Adopt 2024 edition default when targeting 2021 editions
use std::sync::atomic::{AtomicUsize, Ordering};
use std::alloc::{GlobalAlloc, Layout, System};
#[global_allocator]
pub static GLOBAL_ALLOCATOR: StatsAlloc = StatsAlloc { alloc_size: AtomicUsize::new(0) };
pub struct StatsAlloc {
pub alloc_size: AtomicUsize,
}
unsafe impl GlobalAlloc for StatsAlloc {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
self.alloc_size.fetch_add(layout.size(), Ordering::SeqCst);
unsafe { System.alloc(layout) }
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
self.alloc_size.fetch_sub(layout.size(), Ordering::SeqCst);
unsafe { System.dealloc(ptr, layout) }
}
unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
self.alloc_size.fetch_add(layout.size(), Ordering::SeqCst);
unsafe { System.alloc_zeroed(layout) }
}
unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
if new_size > layout.size() {
self.alloc_size.fetch_add(new_size - layout.size(), Ordering::SeqCst);
} else if new_size < layout.size() {
self.alloc_size.fetch_sub(layout.size() - new_size, Ordering::SeqCst);
}
unsafe { System.realloc(ptr, layout, new_size) }
}
}
|