4 个版本

0.1.3 2021年2月5日
0.1.2 2019年5月14日
0.1.1 2019年5月13日
0.1.0 2019年5月13日

#407 in 内存管理


用于 2 crate

MIT 许可证

13KB
224 代码行

interloc

此crate定义了一个创建分配器中间件的接口,即当运行您的分配器时运行的代码。

示例

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`
#[global_allocator]
static GLOBAL: InterAlloc<System, MyMonitor> = InterAlloc {
    inner: System,
    monitor: &MONITOR,
};

依赖关系

~1.5MB
~23K SLoC