light_arena
A lightweight, placement based memory arena for any types which are Sized + Copy.
This crate uses the placement in syntax and placement new protocol and
thus requires nightly Rust.
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!
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.
use light_arena;
let mut arena = new;
let alloc = arena.allocator;
// This would overflow the stack without placement new!
let bytes: & = &alloc <- ;
The arena is untyped and can store anything which is Sized + Copy.
use light_arena;
;
;
Documentation
Rustdoc can be found here
Blockers
- placement_in_syntax and placement_new_protocol are required, see https://github.com/rust-lang/rust/issues/27779