Skip to main content

Arena

Struct Arena 

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

Basic Arena Allocator that uses the crate memmap2 to request wholesale allocation of memory from the system.

An arena is a memory management strategy in which you request a chunk of memory upfront and use it to allocate many objects in sequence. Essentially, it turns memory allocation from a heap problem into a stack problem increasing the speed of this process. It is a technique common in the video game industry to minimize the time spent asking the system for allocations.

Here we offer this small implementation to help speed up parsing operations in other RUMTK crates. This is a standalone crate with no dependencies on other RUMTK crates.

Another feature is that we implement the Allocator trait thus allowing you to provide an instance of the Arena to other standard collections through the nightly compiler’s allocator_api feature. Note that this feature is considered unstable.

§Safety

  • Calling reset simply resets the pointer to 0 and thus technically allows for the potential to leak a prior round of work’s information if a pointer return by allocate is misused.
  • No calls to drop are invoked!!! You have to find a different way to manually do so. This implementation is meant to deal with quick allocation needs and not with self managed resources for which a RAII approach might be more appropriate.

§Example

§Simple initialization and Writing of value.

use crate::rumtk_arena::Arena;

let mut arena = Arena::with_capacity(size_of::<usize>() * 1);
let result_ptr = arena.write(5);

Implementations§

Source§

impl Arena

Source

pub fn new() -> Arena

Allocates a new Arena using the DEFAULT_ARENA_MEMORY_ALLOCATION allocation size.

Source

pub fn with_capacity(capacity: usize) -> Arena

Allocates new Arena with the specified size. At the moment, we use the memmap2 crate’s defaults for this allocation.

Source

pub const fn null() -> Arena

Source

pub fn from_parts(ptr: *mut u8, capacity: usize, dealloc: bool) -> Arena

Source

pub fn split_to(&mut self, len: usize) -> Arena

Source

pub fn freeze(&mut self) -> Arena

Source

pub fn remaining(&self) -> usize

Source

pub fn capacity(&self) -> usize

Source

pub fn can_allocate(&self, size: usize) -> bool

Checks if it is possible to allocate the next object. This is an assertion guarded operation and will panic!!!!!!!

Source

pub fn commit(&mut self, size: usize) -> Result<*mut [u8], AllocError>

Commits a chunk of memory from our memory pool.

§Safety

We call Self::can_allocate to assert that the size requested does not exceed the total pool available. panic if we do not have enough memory to commit.

Source

pub fn write_bytes( &mut self, src: *const u8, data_length: usize, ) -> Result<*mut [u8], AllocError>

Writes a number of bytes into a pre allocated segment from our pool.

Source

pub fn write<T>(&mut self, data: T) -> Result<NonNull<T>, AllocError>

Commits a type object into the memory advancing the internal cursor.

§Order of Operations
  1. Calculate size of object.
  2. Commit a chunk of memory via Self::commit.
  3. Cast object to a byte pointer.
  4. Memcopy from src to dst by the number of bytes calculated in #1.
§Safety

We call Self::commit first before applying a memcopy. Self::commit can panic if there is a bug in this crate due to our call of assert!

Panics if casting to non null pointer somehow fails.

Source

pub fn uncommit(&mut self, length: usize)

We do not truly drop objects. Instead, we move the cursor back by the requested number of bytes.

§Safety

Note that this means old results remain valid and could accidentally end up in a new allocation that could be safety sensitive.

Source

pub fn reset(&mut self)

Resets the internal cursor. No real deallocations occur!

Source

pub fn address(&self) -> *const u8

Source

pub fn is_empty(&self) -> bool

Source

pub fn len(&self) -> usize

Trait Implementations§

Source§

impl AsPtr for Arena

Source§

fn as_ptr(&self) -> *const u8

Source§

fn as_mut_ptr(&mut self) -> *mut u8

Source§

impl AsSlice for Arena

Source§

fn as_slice(&self) -> &'static [u8]

Source§

fn as_slice_mut(&mut self) -> &'static mut [u8]

Source§

fn contains(&self, x: &u8) -> bool

Source§

impl Debug for Arena

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Default for Arena

Source§

fn default() -> Arena

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

impl Index<Range<usize>> for Arena

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, i: Range<usize>) -> &<Arena as Index<Range<usize>>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFrom<usize>> for Arena

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index( &self, i: RangeFrom<usize>, ) -> &<Arena as Index<RangeFrom<usize>>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeFull> for Arena

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, i: RangeFull) -> &<Arena as Index<RangeFull>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeTo<usize>> for Arena

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index(&self, i: RangeTo<usize>) -> &<Arena as Index<RangeTo<usize>>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<RangeToInclusive<usize>> for Arena

Source§

type Output = [u8]

The returned type after indexing.
Source§

fn index( &self, i: RangeToInclusive<usize>, ) -> &<Arena as Index<RangeToInclusive<usize>>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Index<usize> for Arena

Source§

type Output = u8

The returned type after indexing.
Source§

fn index(&self, i: usize) -> &<Arena as Index<usize>>::Output

Performs the indexing (container[index]) operation. Read more
Source§

impl Send for Arena

Source§

impl SizedType for Arena

Source§

fn size(&self) -> usize

Source§

impl Sync for Arena

Auto Trait Implementations§

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.
Source§

impl<T> Ungil for T
where T: Send,

Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V