nalloc

Crates.ionalloc
lib.rsnalloc
version0.1.2
sourcesrc
created_at2020-06-05 18:47:15.045911
updated_at2020-06-05 19:09:19.354386
descriptionAn allocator wrapper that can be turned on and off
homepage
repositoryhttps://gitlab.com/mcarton/nalloc/-/tree/master
max_upload_size
id250423
size22,042
Martin Carton (mcarton)

documentation

https://docs.rs/nalloc/*/nalloc/

README

This crate contains an allocator that can be used to wrap another allocator to turn allocation on and off. This is meant to be used in unit tests.

To use it, declare a static variable with the #[global_allocator] attribute. It can wrap any allocator implementing GlobalAlloc.

# extern crate std;
#[global_allocator]
static ALLOCATOR: nalloc::NAlloc<std::alloc::System> = {
    nalloc::NAlloc::new(std::alloc::System)
};

Allocation is allowed by default. To prevent it, call the deny method on the allocator. When allocation is attempted while a lock is alive, the process will abort.

let this_is_allowed = vec![1, 2, 3];

let _lock = ALLOCATOR.deny();
let this_will_abort = vec![4, 5, 6];

Limitations

Parallel tests

Note that by nature, the default test executor will use this allocator if you add it in your test module. This will cause issues as the test executor itself allocate memory. You can circumvent this by using cargo test -- --test-threads=1.

Aborting

If allocation is attempted while a lock is alive, the process will abort. This means the entire process will be killed, rather than a single thread, and it is not catchable with catch_unwind.

Commit count: 0

cargo fmt