#allocator #middleware #memory #track #interface #run #usage


Middleware allocator for keeping track of memory usage

3 releases

✓ Uses Rust 2018 edition

0.1.2 May 14, 2019
0.1.1 May 13, 2019
0.1.0 May 13, 2019

7 downloads per month
Used in 1 crate

MIT license

224 lines


This crate defines an interface for creating allocator middleware, i.e. code that runs when your allocator is run.


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 {

    // The immutable `&self` reference signature is there because the global allocator
    // needs to be thread-safe.
    fn monitor(&self, layout: Layout, action: AllocAction) {
        // Monitors are inherently composable
        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`
static GLOBAL: InterAlloc<System, MyMonitor> = InterAlloc {
    inner: System,
    monitor: &MONITOR,


~19K SLoC