Struct any_arena::AnyArena
[−]
[src]
pub struct AnyArena<'longer_than_self> { /* fields omitted */ }
A slower reflection-based arena that can allocate objects of any type.
This arena uses RawVec<u8>
as a backing store to allocate objects from.
For each allocated object, the arena stores a pointer to the type descriptor
followed by the object (potentially with alignment padding after each
element). When the arena is destroyed, it iterates through all of its
chunks, and uses the tydesc information to trace through the objects,
calling the destructors on them. One subtle point that needs to be
addressed is how to handle panics while running the user provided
initializer function. It is important to not run the destructor on
uninitialized objects, but how to detect them is somewhat subtle. Since
alloc()
can be invoked recursively, it is not sufficient to simply exclude
the most recent object. To solve this without requiring extra space, we
use the low order bit of the tydesc pointer to encode whether the object
it describes has been fully initialized.
As an optimization, objects with destructors are stored in different chunks
than objects without destructors. This reduces overhead when initializing
plain-old-data (Copy
types) and means we don't need to waste time running
their destructors.
Methods
impl<'longer_than_self> AnyArena<'longer_than_self>
[src]
fn new() -> AnyArena<'longer_than_self>
Allocates a new AnyArena with 32 bytes preallocated.
fn new_with_size(initial_size: usize) -> AnyArena<'longer_than_self>
Allocates a new AnyArena with initial_size
bytes preallocated.
fn alloc<T: 'longer_than_self, F>(&self, op: F) -> &mut T where
F: FnOnce() -> T,
F: FnOnce() -> T,
Allocates a new item in the arena, using op
to initialize the value,
and returns a reference to it.
fn alloc_bytes(&self, len: usize) -> &mut [u8]
Allocates a slice of bytes of requested length. The bytes are not guaranteed to be zero if the arena has previously been cleared.
Panics
Panics if the requested length is too large and causes overflow.
fn clear(&mut self)
Clears the arena. Deallocates all but the longest chunk which may be reused.