Expand description
§interloc
This crate defines an interface for creating allocator middleware, i.e. code that runs when your allocator is run.
§Examples
use interloc::{AllocMonitor, AllocAction, InterAlloc, StatsMonitor, ThreadMonitor};
use std::alloc::System;
use core::alloc::Layout;
struct MyMonitor {
pub global: StatsMonitor,
pub local: ThreadMonitor
}
impl MyMonitor {
// This needs to be const to be usable in static variable declarations.
pub const fn new() -> Self {
Self {
global: StatsMonitor::new(),
local: ThreadMonitor::new(),
}
}
}
impl AllocMonitor for MyMonitor {
fn monitor(&self, layout: Layout, action: AllocAction) {
self.global.monitor(layout, action);
self.local.monitor(layout, action);
}
}
static MONITOR: MyMonitor = MyMonitor::new();
// This needs to be done at the project root, i.e. `lib.rs` or `main.rs`
#[global_allocator]
static GLOBAL: InterAlloc<System, MyMonitor> = InterAlloc {
inner: System,
monitor: &MONITOR,
};
fn use_monitor_in_thread() {
let alloc_info = MONITOR.local.info();
let _allocation_test = Vec::<u8>::with_capacity(100);
println!("{:#?}", MONITOR.local.info().relative_to(&alloc_info));
}
Structs§
- Alloc
Info - Information about allocations by the allocator.
- Inter
Alloc - An allocator that watches the calls to its API, sends them to a struct, and then makes the calls.
- Layout
- Layout of a block of memory.
- Stats
Monitor - Monitor of global memory usage statistics. Uses a read-write lock to prevent data corruption.
- Thread
Monitor - Thread-local statistics on memory usage.
Enums§
- Alloc
Action - An action that an allocator can take, either right before, or right after it happens.
- Alloc
Rel - Before or after an allocation call is executed.
Traits§
- Alloc
Monitor - When attached to an
InterAlloc
instance, this struct’smonitor
method is called before and after calls to the inner allocator. The ordering of these method calls is enforced bystd::sync::atomic::fence
withstd::sync::atomic::Ordering::SeqCst
.