pub struct ArenaShared<T> { /* private fields */ }
Expand description

An atomic reference-counted pointer into an arena-allocated object with thread-safe allocation and deallocation capabilities.

This structure ensures atomic access and safe sharing of allocated data across multiple threads while maintaining reference counting to manage the memory deallocation when no longer needed.

It combines a thread-safe RawArena for allocation and deallocation and provides a mutex to guarantee exclusive access to the internal data for safe multi-threaded operation.

The ArenaShared allows multiple threads to allocate, share, and deallocate objects within the arena, ensuring safety and atomicity during these operations.

Implementations§

source§

impl<T> ArenaShared<T>

source

pub fn with_capacity(capacity: usize) -> Self

source

pub fn allocate(&self, data: T) -> ArenaRc<T>

Allocates a new object in the arena and returns an ArenaRc pointing to it.

This method creates a new instance of type T within the RawArena. The provided data is initialized within the arena, and an ArenaRc is returned to manage this allocated data. The ArenaRc serves as an atomic, reference-counted pointer to the allocated data within the arena, ensuring safe concurrent access across multiple threads while maintaining the reference count for memory management.

The allocation process employs a mutex to ensure thread-safe access to the arena, allowing only one thread at a time to modify the internal state, including allocating and deallocating memory.

Safety

The provided data is allocated within the arena and managed by the ArenaRc. Improper handling or misuse of the returned ArenaRc pointer may lead to memory leaks or memory unsafety.

Example

// Define a struct that will be allocated within the arena
struct MyStruct {
    data: usize,
}

// Create a new instance of ArenaShared with a specified base capacity
let arena: ArenaShared<MyStruct> = ArenaShared::with_capacity(16);

// Allocate a new MyStruct instance within the arena
let data_instance = MyStruct { data: 42 };
let allocated_arc = arena.allocate(data_instance);

// Now, allocated_arc can be used as a managed reference to the allocated data
assert_eq!(allocated_arc.data, 42); // Validate the data stored in the allocated arc
source

pub fn allocate_if_space(&self, data: T) -> Result<ArenaRc<T>, T>

Allocates a new object in the arena and returns an ArenaRc pointing to it. If no space is available, returns the original object.

This method creates a new instance of type T within the RawArena. The provided data is initialized within the arena, and an ArenaRc is returned to manage this allocated data. The ArenaRc serves as an atomic, reference-counted pointer to the allocated data within the arena, ensuring safe concurrent access across multiple threads while maintaining the reference count for memory management.

The allocation process employs a mutex to ensure thread-safe access to the arena, allowing only one thread at a time to modify the internal state, including allocating and deallocating memory.

source

pub unsafe fn reserve_space(&self) -> Option<ArenaSharedReservation<T>>

Attempt to reserve space in this arena.

Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

source

pub unsafe fn forget_reservation(&self, reservation: ArenaSharedReservation<T>)

Forget a reservation.

Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

source

pub unsafe fn complete_reservation( &self, reservation: ArenaSharedReservation<T>, data: T ) -> ArenaRc<T>

Complete a reservation.

Safety

Reservations must be either completed or forgotten, and must be provided to the same arena that created them.

Trait Implementations§

source§

impl<T> Drop for ArenaShared<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more

Auto Trait Implementations§

§

impl<T> !RefUnwindSafe for ArenaShared<T>

§

impl<T> !Send for ArenaShared<T>

§

impl<T> !Sync for ArenaShared<T>

§

impl<T> Unpin for ArenaShared<T>

§

impl<T> !UnwindSafe for ArenaShared<T>

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>,

§

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>,

§

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.