pub struct Arena { /* private fields */ }Expand description
Where the target allows it the region begins at a 4 GiB-aligned address and is at most MAX_BLOCK_SIZE bytes, so
the low 32 bits of any interior pointer equal its byte offset within the region. Allocation is a pointer bump;
deallocation of individual items is a no-op (every chunk is freed at once on drop; or never if using borrowed
memory).
Implementations§
Source§impl Arena
impl Arena
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a self-allocated arena.
Where address space can be reserved lazily the first chunk is the full MAX_BLOCK_SIZE region, costing no
physical memory until written to, so one non-relocating chunk serves arbitrarily large parses. Elsewhere every
byte of a chunk is paid for up front, so the arena starts small and adds chunks as it fills.
Sourcepub fn with_capacity(size: usize) -> Self
pub fn with_capacity(size: usize) -> Self
Create a self-allocated arena from a capacity hint.
On targets with virtual memory the hint is rounded up to a reusable size class. Elsewhere it is used exactly. The arena may add chunks when the initial region fills.
§Panics
Panics if the backing allocation fails.
Sourcepub unsafe fn from_raw_parts(ptr: NonNull<u8>, size: usize) -> Self
pub unsafe fn from_raw_parts(ptr: NonNull<u8>, size: usize) -> Self
Create an arena over caller-owned memory.
Intended for bindings where the original caller owns the memory, e.g. NAPI JS where ArrayBuffer is owned and
already allocated.
§Safety
ptr must be the base of a live, writable region of at least size bytes that outlives the arena, aligned to
BLOCK_ALIGN and no larger than MAX_BLOCK_SIZE, and it must not be handed to another allocator.
Sourcepub fn transfer_base(&self) -> Option<usize>
pub fn transfer_base(&self) -> Option<usize>
Whether every allocation lives in one region starting at Arena::base_ptr, and so whether the low 32 bits of
every pointer handed out is its offset into that region.
- Over a reserved region this is
Arena::base_ptr, until the arena has to add a chunk: there is then no single region left to be an offset into, and this becomesNone. - Where pointers are 32 bits wide they already are their own offsets, so the base is
0however many chunks the arena holds: the buffer is the whole address space, which on wasm32 is the linear memory the binding layer already has. - On a 64 bit target with nothing to reserve - wasm64 under
memory64, whose linear memory may exceed 4 GiB - a pointer’s upper half is unconstrained, so its low 32 bits mean nothing and this is alwaysNone.
Sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Number of bytes handed out so far.
Trait Implementations§
Source§impl Allocator for &Arena
impl Allocator for &Arena
Source§fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
Source§unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout)
unsafe fn deallocate(&self, _ptr: NonNull<u8>, _layout: Layout)
ptr. Read moreSource§unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
§fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
fn allocate_zeroed(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError>
allocate, but also ensures that the returned memory is zero-initialized. Read more§unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError>
unsafe fn grow_zeroed( &self, ptr: NonNull<u8>, old_layout: Layout, new_layout: Layout, ) -> Result<NonNull<[u8]>, AllocError>
grow, but also ensures that the new contents are set to zero before being
returned. Read more