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>
impl<'a> Bitena<'a>
Sourcepub fn new(byte_capacity: usize) -> Result<Self>
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(())
}Sourcepub fn alloc<T>(&self, val: T) -> &mut T
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(())
}pub fn try_alloc<T>(&self, val: T) -> Result<&mut T>
Sourcepub fn alloc_slice<T>(&self, initial_value: T, len: usize) -> &mut [T]
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(())
}pub fn try_alloc_slice<T>( &self, initial_value: T, len: usize, ) -> Result<&mut [T]>
Sourcepub fn alloc_str(&self, st: &str) -> &str
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(())
}pub fn try_alloc_str(&self, st: &str) -> Result<&str>
Sourcepub fn remaining(&self) -> usize
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);
}Sourcepub fn reset(&mut self)
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(())
}