-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathalloc.rs
More file actions
30 lines (25 loc) · 847 Bytes
/
alloc.rs
File metadata and controls
30 lines (25 loc) · 847 Bytes
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
use std::alloc::{GlobalAlloc, Layout, System};
use std::sync::atomic::{AtomicUsize, Ordering::SeqCst};
#[global_allocator]
static ALLOC: A = A;
static ALLOC_AMT: AtomicUsize = AtomicUsize::new(0);
struct A;
unsafe impl GlobalAlloc for A {
unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
let ptr = System.alloc(layout);
if !ptr.is_null() {
ALLOC_AMT.fetch_add(layout.size(), SeqCst);
}
return ptr;
}
unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
// Poison all deallocations to try to catch any use-after-free in the
// bindings as early as possible.
std::ptr::write_bytes(ptr, 0xde, layout.size());
ALLOC_AMT.fetch_sub(layout.size(), SeqCst);
System.dealloc(ptr, layout)
}
}
pub fn get() -> usize {
ALLOC_AMT.load(SeqCst)
}