Arena

Struct Arena 

Source
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

Source

pub fn new(chunk_size: usize) -> Self

Creates a new arena with the specified chunk size.

§Arguments
  • chunk_size - Size of each memory chunk in bytes.
§Panics

Panics if chunk_size is 0.

Source

pub fn with_default_size() -> Self

Creates a new arena with default chunk size (64KB).

Source

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.

Source

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.

Source

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)
Source

pub fn copy_slice(&mut self, data: &[u8]) -> &[u8]

Allocates and copies data into the arena.

This is a convenience method that allocates space and copies the provided slice in one operation.

§Arguments
  • data - The data to copy into the arena.
§Returns

A slice pointing to the copied data within the arena.

Source

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.

Source

pub fn bytes_used(&self) -> usize

Returns the total bytes currently allocated.

Source

pub fn capacity(&self) -> usize

Returns the total capacity across all chunks.

Source

pub fn chunk_count(&self) -> usize

Returns the number of chunks allocated.

Trait Implementations§

Source§

impl Debug for Arena

Source§

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

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

impl Default for Arena

Source§

fn default() -> Self

Returns the “default value” for a 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
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.