Crate allocandrescu

Source
Expand description

A collection of various allocators and allocator combinators

This library is inspired by Andrei Alexandrescu’s CppCon 2015 talk std::allocator Is to Allocation what std::vector Is to Vexation and the Zig programming language.

allocandrescu allows you to safely compose allocators using combinators such as cond and fallback. It also provides a variety of simple allocators like Stack.

This crate depends on allocator-api2, a polyfill for the unstable allocator_api feature.

§Usage

To extend allocators with methods from this crate, import the Allocandrescu trait:

use allocandrescu::Allocandrescu as _;

or import the prelude:

use allocandrescu::prelude::*;

§Example

Allocator that allocates objects smaller than 16 bytes on a stack of size 1024 bytes. For larger objects, it falls back to using the system allocator. Additionally, it prints all allocation results.

use allocandrescu::{alloc::Stack, prelude::*};
use allocator_api2::vec;

let stack = Stack::<1024>::new();
let alloc = stack
    .by_ref()
    .cond(|layout| layout.size() <= 16)
    .fallback(std::alloc::System)
    .inspect(|layout, result| println!("layout: {layout:?}, result: {result:?}"));
let v = vec![in &alloc; 0; 100];

§Feature flags

  • bumpalo enables support for bumpalo crate.

Re-exports§

pub use bumpalo;bumpalo

Modules§

alloc
Basic allocators.
combinator
A collection of allocator combinator structs.
prelude
Prelude exports all the allocator-related traits.

Traits§

Allocandrescu
Extension trait for Allocator trait that provides methods for combining allocators.
ArenaAllocator
Allocator that uses region-based memory management.