Arena

Struct Arena 

Source
pub struct Arena { /* private fields */ }
Expand description

An arena allocator.

If you have never used an arena allocator before, think of it as allocating objects on the stack, but the stack is really big. Each time you allocate, memory gets pushed at the end of the stack, each time you deallocate, memory gets popped from the end of the stack.

One reason you’d want to use this is obviously performance: It’s very simple and so it’s also very fast, >10x faster than your system allocator.

However, modern allocators such as mimalloc are just as fast, so why not use them? Because their performance comes at the cost of binary size and we can’t have that.

The biggest benefit though is that it sometimes massively simplifies lifetime and memory management. This can best be seen by this project’s UI code, which uses an arena to allocate a tree of UI nodes. This is infamously difficult to do in Rust, but not so when you got an arena allocator: All nodes have the same lifetime, so you can just use references.

Do not push objects into the arena that require destructors. Destructors are not executed. Use a pool allocator for that.

Implementations§

Source§

impl Arena

Source

pub const fn empty() -> Self

Source

pub fn new(capacity: usize) -> Result<Self>

Source

pub fn offset(&self) -> usize

Source

pub unsafe fn reset(&self, to: usize)

“Deallocates” the memory in the arena down to the given offset.

§Safety

Obviously, this is GIGA UNSAFE. It runs no destructors and does not check whether the offset is valid. You better take care when using this function.

Source

pub fn alloc_uninit<T>(&self) -> &mut MaybeUninit<T>

Source

pub fn alloc_uninit_slice<T>(&self, count: usize) -> &mut [MaybeUninit<T>]

Trait Implementations§

Source§

impl Allocator for Arena

Source§

fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to allocate a block of memory. Read more
Source§

fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like allocate, but also ensures that the returned memory is zero-initialized. Read more
Source§

unsafe fn deallocate(&self, _: NonNull<u8>, _: Layout)

🔬This is a nightly-only experimental API. (allocator_api)
Deallocates the memory referenced by ptr. Read more
Source§

unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to extend the memory block. Read more
Source§

unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Behaves like grow, but also ensures that the new contents are set to zero before being returned. Read more
Source§

unsafe fn shrink( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>

🔬This is a nightly-only experimental API. (allocator_api)
Attempts to shrink the memory block. Read more
Source§

fn by_ref(&self) -> &Self
where Self: Sized,

🔬This is a nightly-only experimental API. (allocator_api)
Creates a “by reference” adapter for this instance of Allocator. Read more
Source§

impl Default for Arena

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Drop for Arena

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl !Freeze for Arena

§

impl !RefUnwindSafe for Arena

§

impl !Send for Arena

§

impl !Sync for Arena

§

impl Unpin for Arena

§

impl UnwindSafe for Arena

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.