Struct deno_core::arena::ArenaSharedAtomic
source · 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§
pub fn with_capacity(capacity: usize) -> Self
sourcepub fn allocate(&self, data: T) -> ArenaArc<T>
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
sourcepub fn allocate_if_space(&self, data: T) -> Result<ArenaArc<T>, T>
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.
sourcepub unsafe fn reserve_space(&self) -> Option<ArenaSharedAtomicReservation<T>>
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.
sourcepub unsafe fn forget_reservation(
&self,
reservation: ArenaSharedAtomicReservation<T>
)
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.
sourcepub unsafe fn complete_reservation(
&self,
reservation: ArenaSharedAtomicReservation<T>,
data: T
) -> ArenaArc<T>
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.