Struct deno_core::arena::ArenaUnique
source · pub struct ArenaUnique<T> { /* private fields */ }
Expand description
An arena-based unique ownership container allowing allocation and deallocation of objects with exclusive ownership semantics.
ArenaUnique
provides exclusive ownership semantics similar to
a Box
. It utilizes a RawArena
for allocation and
deallocation of objects, maintaining the sole ownership of the
allocated data and enabling safe cleanup when the ArenaUnique
instance is dropped.
This container guarantees exclusive access to the allocated data within the arena, allowing single-threaded operations while efficiently managing memory and ensuring cleanup on drop.
Implementations§
source§impl<T> ArenaUnique<T>
impl<T> ArenaUnique<T>
sourcepub const fn overhead() -> usize
pub const fn overhead() -> usize
Returns the constant overhead per allocation to assist with making allocations page-aligned.
sourcepub const fn allocation_size() -> usize
pub const fn allocation_size() -> usize
Returns the size of each allocation.
pub fn with_capacity(capacity: usize) -> Self
sourcepub fn allocate(&self, data: T) -> ArenaBox<T> ⓘ
pub fn allocate(&self, data: T) -> ArenaBox<T> ⓘ
Allocates a new data instance of type T
within the arena, encapsulating it within an ArenaBox
.
This method creates a new instance of type T
within the RawArena
. The provided data
is initialized within the arena, and an ArenaBox
is returned to manage this allocated data.
The ArenaBox
serves as a reference to the allocated data within the arena, providing safe access
and management of the stored value.
§Safety
The provided data
is allocated within the arena and managed by the ArenaBox
. Improper handling
or misuse of the returned ArenaBox
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 ArenaUnique with a specified base capacity
let arena: ArenaUnique<MyStruct> = ArenaUnique::with_capacity(16);
// Allocate a new MyStruct instance within the arena
let data_instance = MyStruct { data: 42 };
let allocated_box = arena.allocate(data_instance);
// Now, allocated_box can be used as a managed reference to the allocated data
assert_eq!(allocated_box.data, 42); // Validate the data stored in the allocated box
sourcepub unsafe fn reserve_space(&self) -> Option<ArenaUniqueReservation<T>>
pub unsafe fn reserve_space(&self) -> Option<ArenaUniqueReservation<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: ArenaUniqueReservation<T>)
pub unsafe fn forget_reservation(&self, reservation: ArenaUniqueReservation<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: ArenaUniqueReservation<T>,
data: T
) -> ArenaBox<T> ⓘ
pub unsafe fn complete_reservation( &self, reservation: ArenaUniqueReservation<T>, data: T ) -> ArenaBox<T> ⓘ
Complete a reservation.
§Safety
Reservations must be either completed or forgotten, and must be provided to the same arena that created them.