Skip to main content

Bitena

Struct Bitena 

Source
pub struct Bitena<'a> { /* private fields */ }
Expand description

Bitena

A small, extremely fast, lock-free thread-safe arena bump allocator that can hand out multiple mutable elements, structs, slices, or read-only &strs from a single pre-allocated block.

This crate is optimized for performance-critical applications

  • FIXED SIZED ITEMS

  • ALLOCATED ITEMS DON’T DROP

  • FIXED SIZE ARENA

any element types that reserver memory or resources, file handles, vecs, and strings will leak memory if allocated on Bitena because the arena drops in one operation, without dropping each individual item as appropriate.

§Example

use bitena::*;

fn main() -> Result<()> {
    let bitena = Bitena::new(1024)?;
    let slice = bitena.try_alloc_slice(0u32, 4)?;
    for i in 0..4 {
        slice[i] = i as u32;
    }
    println!("{:?}", slice);
    Ok(())
}

Implementations§

Source§

impl<'a> Bitena<'a>

Source

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

Creates a new Arena with the specified byte capacity.

§Example
use bitena::*;

fn main() -> Result<()> {
    let bitena = Bitena::new(1024)?;

    Ok(())
}
Source

pub fn alloc<T>(&self, val: T) -> &mut T

Allocates space for a single element and returns a mutable reference to it.

§Safety

The caller must ensure:

  • The arena has enough remaining memory
  • The reference is not used after the arena is reset or dropped
§Example
use bitena::*;

fn main() -> Result<()> {
    let bitena = Bitena::new(1024)?;
    let value = bitena.try_alloc(42u32)?;
    let num = bitena.try_alloc(-234i32)?;
    *num = 100;
    println!("{}", *num);
    Ok(())
}
Source

pub fn try_alloc<T>(&self, val: T) -> Result<&mut T>

Source

pub fn alloc_slice<T>(&self, initial_value: T, len: usize) -> &mut [T]

Allocates space for a slice and returns a mutable slice reference.

§Safety

The caller must ensure:

  • The arena has enough remaining memory
  • The reference is not used after the arena is reset or dropped
§Example
use bitena::*;

fn main() -> Result<()> {
    let bitena = Bitena::new(1024)?;
        let slice = bitena.try_alloc_slice(0u32, 4)?;
        for i in 0..4 {
            slice[i] = i as u32;
        }
        println!("{:?}", slice);
    Ok(())
}
Source

pub fn try_alloc_slice<T>( &self, initial_value: T, len: usize, ) -> Result<&mut [T]>

Source

pub fn alloc_str(&self, st: &str) -> &str

Allocates space for a str and returns a read-only reference, &str.

§Safety

The caller must ensure:

  • The arena has enough remaining memory
  • The reference is not used after the arena is reset or dropped
§Example
use bitena::*;

fn main() -> Result<()> {
    let bitena = Bitena::new(1024)?;
        let num = bitena.try_alloc(42u32)?;
        *num = 100;
        let stnum = format!("Num: {}", *num);
        let s = bitena.try_alloc_str(&stnum)?;
        println!("{}  {:?}", *num, s);
    Ok(())
}
Source

pub fn try_alloc_str(&self, st: &str) -> Result<&str>

Source

pub fn remaining(&self) -> usize

Returns the number of bytes remaining in the arena.

§Example
use bitena::*;

fn main() {
    let bitena = Bitena::new(1024).unwrap();
    assert_eq!(bitena.remaining(), 1024);
}
Source

pub fn reset(&mut self)

Resets the arena, making all previously allocated memory available again.

§Example
use bitena::*;

fn main() -> Result<()> {
    let mut bitena = Bitena::new(1024)?;
    let slice = bitena.try_alloc_slice(1u8, 100)?;
    assert_eq!(bitena.remaining(), 924);
    bitena.reset();
    assert_eq!(bitena.remaining(), 1024);
    Ok(())
}

Trait Implementations§

Source§

impl Drop for Bitena<'_>

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

impl Send for Bitena<'_>

Source§

impl Sync for Bitena<'_>

Auto Trait Implementations§

§

impl<'a> !Freeze for Bitena<'a>

§

impl<'a> RefUnwindSafe for Bitena<'a>

§

impl<'a> Unpin for Bitena<'a>

§

impl<'a> UnsafeUnpin for Bitena<'a>

§

impl<'a> UnwindSafe for Bitena<'a>

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.