pub struct Arena { /* private fields */ }Expand description
A bump allocator for transaction-scoped memory.
Provides fast allocation by simply incrementing a pointer. Memory is organized in chunks that grow as needed. The arena can be reset for reuse without deallocating the underlying memory.
§Security
The arena zeros all memory on reset to prevent data leakage between transactions. This is critical when the arena may hold sensitive data like encryption keys or user credentials.
§Example
use thunderdb::arena::Arena;
let mut arena = Arena::new(1024);
// Fast bump allocation
let slice1 = arena.alloc(32);
slice1.fill(0xAA);
// Copy data into arena
let data = b"hello world";
let copied = arena.copy_slice(data);
assert_eq!(copied, data);
// Reset for reuse (memory is zeroed)
arena.reset();
assert_eq!(arena.bytes_used(), 0);Implementations§
Source§impl Arena
impl Arena
Sourcepub fn with_default_size() -> Self
pub fn with_default_size() -> Self
Creates a new arena with default chunk size (64KB).
Sourcepub fn with_capacity(initial_capacity: usize) -> Self
pub fn with_capacity(initial_capacity: usize) -> Self
Creates a new arena with specified initial capacity.
Pre-allocates a single chunk of the given size.
Sourcepub fn alloc(&mut self, size: usize) -> &mut [u8] ⓘ
pub fn alloc(&mut self, size: usize) -> &mut [u8] ⓘ
Allocates size bytes from the arena.
Returns a mutable slice of zeroed memory. The allocation is bump-pointer style and very fast (O(1) typical).
If the current chunk doesn’t have enough space, a new chunk is allocated.
§Arguments
size- Number of bytes to allocate.
§Returns
A mutable slice of size bytes, initialized to zero.
Sourcepub fn alloc_aligned(&mut self, size: usize, align: usize) -> &mut [u8] ⓘ
pub fn alloc_aligned(&mut self, size: usize, align: usize) -> &mut [u8] ⓘ
Allocates size bytes with specified alignment.
Useful for cache-line alignment (64 bytes) or page alignment to avoid false sharing and improve memory access patterns.
§Arguments
size- Number of bytes to allocate.align- Required alignment (must be power of 2).
§Returns
A mutable slice of size bytes, with the starting address
aligned to align bytes.
§Panics
Panics if align is not a power of 2.
§Performance
Common alignments:
- 64: Cache line (avoids false sharing)
- 4096: Page alignment (for mmap-friendly buffers)
- 32768: HPC page size (matches PAGE_SIZE)
Sourcepub fn copy_slice(&mut self, data: &[u8]) -> &[u8] ⓘ
pub fn copy_slice(&mut self, data: &[u8]) -> &[u8] ⓘ
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the arena for reuse.
This clears all allocations but keeps the underlying memory for reuse. All memory is zeroed to prevent data leakage.
§Security
Memory is explicitly zeroed to ensure no sensitive data from previous transactions leaks to new allocations.
Sourcepub fn bytes_used(&self) -> usize
pub fn bytes_used(&self) -> usize
Returns the total bytes currently allocated.
Sourcepub fn chunk_count(&self) -> usize
pub fn chunk_count(&self) -> usize
Returns the number of chunks allocated.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more