pub struct ArenaSharedAtomic<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 ArenaSharedAtomic allows multiple threads to allocate, share, and deallocate objects within the arena, ensuring safety and atomicity during these operations.

Implementations§

source§

impl<T> ArenaSharedAtomic<T>

source

pub const fn overhead() -> usize

Returns the constant overhead per allocation to assist with making allocations page-aligned.

source

pub const fn allocation_size() -> usize

Returns the size of each allocation.

source

pub fn with_capacity(capacity: usize) -> Self

source

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

Allocates a new object in the arena and returns an ArenaArc 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 ArenaArc is returned to manage this allocated data. The ArenaArc 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 ArenaArc. Improper handling or misuse of the returned ArenaArc 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 ArenaSharedAtomic with a specified base capacity
let arena: ArenaSharedAtomic<MyStruct> = ArenaSharedAtomic::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<ArenaArc<T>, T>

Allocates a new object in the arena and returns an ArenaArc 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 ArenaArc is returned to manage this allocated data. The ArenaArc 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<ArenaSharedAtomicReservation<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: ArenaSharedAtomicReservation<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: ArenaSharedAtomicReservation<T>, data: T ) -> ArenaArc<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 ArenaSharedAtomic<T>

source§

fn drop(&mut self)

Executes the destructor for this type. Read more
source§

impl<T> Send for ArenaSharedAtomic<T>

Auto Trait Implementations§

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.