use mountpoint_s3_crt_sys::*;
use std::ptr::NonNull;
#[derive(Debug)]
pub struct Allocator {
pub(crate) inner: NonNull<aws_allocator>,
traced: bool,
}
unsafe impl Send for Allocator {}
unsafe impl Sync for Allocator {}
impl Allocator {
pub fn traced(&self) -> Self {
unsafe {
let inner = aws_mem_tracer_new(
self.inner.as_ptr(),
std::ptr::null_mut(),
aws_mem_trace_level::AWS_MEMTRACE_BYTES,
8,
);
let inner = NonNull::new(inner).expect("Failed to create traced allocator");
Self { inner, traced: true }
}
}
pub fn tracer_dump(&self) {
assert!(self.traced, "cannot call on non-traced allocator");
unsafe {
aws_mem_tracer_dump(self.inner.as_ptr());
}
}
pub fn tracer_bytes(&self) -> usize {
assert!(self.traced, "cannot call on non-traced allocator");
unsafe { aws_mem_tracer_bytes(self.inner.as_ptr()) }
}
pub fn tracer_count(&self) -> usize {
assert!(self.traced, "cannot call on non-traced allocator");
unsafe { aws_mem_tracer_count(self.inner.as_ptr()) }
}
}
impl Default for Allocator {
fn default() -> Self {
let inner = unsafe { aws_default_allocator() };
let inner = NonNull::new(inner).expect("CRT default allocator is never null");
Self { inner, traced: false }
}
}