#arena #memory #heap-memory #placement #stack #allocator #copy

light_arena

A lightweight, placement based memory arena for types which are Sized + Copy. This crate requires nightly.

5 releases (2 stable)

Uses old Rust 2015

1.0.1 Jun 15, 2019
0.1.2 Jan 27, 2018
0.1.1 Jun 24, 2017
0.1.0 Jun 24, 2017

#334 in Memory management

Download history 8/week @ 2024-02-23 8/week @ 2024-03-01 3/week @ 2024-03-08 1/week @ 2024-03-15 52/week @ 2024-03-29

57 downloads per month

MIT license

14KB
132 lines

light_arena

Temporarily a more simple memory pool for keeping stack alloc objects in copied into a shared heap rather than a true placement new memory arena. Unfortunately the path forward for placement new in Rust does not look good right now, so I've reverted this crate to work more like a memory heap where stuff can be put, but not constructed in place. This mimics similar behavior, but allocations are limited to the stack size and must first be made on the stack then copied in.

This crate is written to solve a specific problem I have in tray_rust, where I want to store trait objects and f32 arrays in a memory arena which is then reset and reused for each pixel rendered (but not free'd and reallocated!). The key features to enable this are the use of the nightly placement new feature, letting us actually construct objects in place instead of copying from a stack temporary, and reusing the previously allocated space via the Allocator scopes. If you have a similar problem, this might be the right crate for you!

Crate Version Badge Build Status

Examples

Allocations in a MemoryArena are made using an allocator and the placement in syntax. The Allocator grants exclusive access to the arena while it's in scope, allowing to make allocations. Once the Allocator is dropped the space used is marked available again for subsequent allocations. Note that Drop is never called on objects allocated in the arena, and thus the restriction that T: Sized + Copy.

#![feature(placement_in_syntax)]
use light_arena;

let mut arena = light_arena::MemoryArena::new(8);
let alloc = arena.allocator();
// This would overflow the stack without placement new!
let bytes: &[u8] = &alloc <- [0u8; 8 * 1024 * 1024];

The arena is untyped and can store anything which is Sized + Copy.

#![feature(placement_in_syntax)]
use light_arena;

trait Foo {
	fn speak(&self);
}

#[derive(Copy, Clone)]
struct Bar(i32);
impl Foo for Bar {
	fn speak(&self) {
		println!("Bar! val = {}", self.0);
	}
}

#[derive(Copy, Clone)]
struct Baz;
impl Foo for Baz {
	fn speak(&self) {
		println!("Baz!");
	}
}

fn main() {
	let mut arena = light_arena::MemoryArena::new(2);
	let allocator = arena.allocator();
	let a: &Foo = &allocator <- Baz;
	let b: &Foo = &allocator <- Bar(10);
	let c: &Foo = &allocator <- Bar(14);
	a.speak();
	b.speak();
	c.speak();
	// Storing 0-sized types can give some interesting results
	println!("a = {:p}", a as *const Foo);
	println!("b = {:p}", b as *const Foo);
	println!("c = {:p}", c as *const Foo);
}

Documentation

Rustdoc can be found here

Blockers

Dependencies

~0–1.4MB
~34K SLoC