Module

alloc::arena

source

Bump allocation for Copy values that share one lifetime and access brand.

Types

ChunkOne chunk of an arena: its data block, how many of its bytes are in use, its size, and the chunk made before it (null for the root).
Arena!A bump allocator over a chain of heap chunks, every value in it under the one brand 'k.
Domain!An arena with the one capability for its domain: the context for code that reads, writes and allocates there (Access, Allocate), and reads text of it (ReadBytes).

Traits

AllocateA context that lends an arena with its capability.

Functions

padding_forThe padding that brings the address at up to the next multiple of alignment.
has_roomWhether need bytes, after padding, fit in a chunk with used of its cap bytes taken.
new_headerAllocates the chunk header for an already-allocated data buffer.
new_chunkA fresh chunk ahead of next, the current one, with room for at least min_cap bytes: twice next's capacity up to 1 MiB, so a small arena stays small and a large one takes few chunks, or bigger if the single allocation that triggered growth needs more than that on its own.
bump_chunkReserves need bytes at alignment in cur, growing into a new chunk when it lacks room, and returns the chunk the bytes are in and their address.
free_chain_after_root_releaseFrees every chunk's header, plus every non-root chunk's data buffer.
seedThe seed the arena a context lends hashes its maps' keys under (Arena!::seed).
ref_invalue, put in the context's arena.
copy_strThe bytes of text, read under the capability from lends, copied into the arena to lends: how text moves from one arena to another, so that the first can be destroyed while the copy lives on.
concatThe bytes of first then second, in one new block of the arena.
copy_bytesCopies count bytes from src to dest, byte by byte (the same loop as Buffer!'s Writer::write_raw).

Instances

Access[Domain!['k] -> 'k, Arena!['k]]A domain lends its capability, with its arena as the rest.
Allocate[Domain!['k] -> 'k, ()]A domain lends its arena with its capability.
ReadBytes[Domain!['k], Str['k]]Text of a domain is read through the domain itself, which lends its capability to read it.

Methods of other modules' types

Buffer!::saveThe buffer's bytes copied into the context's arena in one block of exactly their length, as text that lives as long as the arena; the buffer is freed.

Function declarations

fn padding_forsource

fn padding_for(at: usize, alignment: usize) -> usize { ... }

The padding that brings the address at up to the next multiple of alignment. alignment is a power of two (core::mem::alignof), so this is a mask rather than a division. Callers pass the address, not an offset into a chunk, so the result holds whatever alignment malloc gave the chunk.

fn has_roomsource

fn has_room(used: usize, cap: usize, padding: usize, need: usize) -> bool { ... }

Whether need bytes, after padding, fit in a chunk with used of its cap bytes taken.

Check capacity by subtraction so wrapping usize addition cannot make an out-of-bounds allocation appear to fit.

fn new_headersource

unsafe fn new_header(data: *u8, cap: usize, next: *Chunk) -> *Chunk { ... }

Allocates the chunk header for an already-allocated data buffer. Kept separate from new_chunk so that Arena!::new can pass the root buffer's Uninit! straight to Allocation!::adopt_value, which records the allocator's extent.

The header is a small, separate heap allocation (simpler than packing it into the front of data, at the cost of one extra alloc/drop pair per chunk), and is arena-private in every chunk including the root.

Caller: data is a live block of cap bytes that the chunk now owns, and next is null or the arena's live current chunk.

fn new_chunksource

unsafe fn new_chunk(min_cap: usize, next: *Chunk) -> *Chunk { ... }

A fresh chunk ahead of next, the current one, with room for at least min_cap bytes: twice next's capacity up to 1 MiB, so a small arena stays small and a large one takes few chunks, or bigger if the single allocation that triggered growth needs more than that on its own. Used for chunks 2..n only (Arena!::new builds the root itself, because the root's buffer is the tracked one); its data buffer never becomes an Allocation! and is freed by free_chain_after_root_release.

Caller: next is the arena's live current chunk.

fn bump_chunksource

unsafe fn bump_chunk(cur: *Chunk, need: usize, alignment: usize) -> (*Chunk, *u8) { ... }

Reserves need bytes at alignment in cur, growing into a new chunk when it lacks room, and returns the chunk the bytes are in and their address. Everything in Arena!::alloc that does not depend on its T, kept out of it so each instantiation stays a few statements.

Caller: cur is the arena's live current chunk.

fn free_chain_after_root_releasesource

unsafe fn free_chain_after_root_release(c: *Chunk) -> () { ... }

Frees every chunk's header, plus every non-root chunk's data buffer.

Caller: c is the arena's current chunk, and Arena!::destroy has already freed the root chunk's data buffer through Allocation!::release + HeapAllocator::drop, so freeing it here would be a double free. The walk skips exactly the root because the root is the chain's tail: new_chunk prepends (it stores next: cur), Arena!::new builds the root with a null next, and nothing else writes a chunk's next, so next == null holds for the root alone.

fn seedsource

forall 'k, C, R where Allocate[C -> 'k, R].
fn seed(&cx: C) -> Seed { ... }

The seed the arena a context lends hashes its maps' keys under (Arena!::seed).

fn ref_insource

forall T, 'k, C, R where Copy[T], Allocate[C -> 'k, R], Sized[T].
fn ref_in(&cx: C, value: T) -> Ref[T, 'k] { ... }

value, put in the context's arena. T is Copy, as for Arena!::alloc.

fn copy_strsource

forall 'j, 'k, C, R, D, Q where Access[C -> 'j, R], Allocate[D -> 'k, Q].
fn copy_str(text: Str['j], &from: C, &to: D) -> Str['k] { ... }

The bytes of text, read under the capability from lends, copied into the arena to lends: how text moves from one arena to another, so that the first can be destroyed while the copy lives on.

fn concatsource

forall 'j, C, R where Allocate[C -> 'j, R].
fn concat(&cx: C, first: Str['j], second: Str['j]) -> Str['j] { ... }

The bytes of first then second, in one new block of the arena.

fn copy_bytessource

unsafe fn copy_bytes(dest: *u8, src: *u8, count: usize) -> () { ... }

Copies count bytes from src to dest, byte by byte (the same loop as Buffer!'s Writer::write_raw). alloc_bytes only reserves room, so its callers copy with this. Caller: src[0..count) is readable, dest[0..count) writable, and the two do not overlap.

Instance declarations

instance Access[Domain!['k] -> 'k, Arena!['k]]source

forall 'k.
instance Access[Domain!['k] -> 'k, Arena!['k]] { ... }

A domain lends its capability, with its arena as the rest.

instance Allocate[Domain!['k] -> 'k, ()]source

forall 'k.
instance Allocate[Domain!['k] -> 'k, ()] { ... }

A domain lends its arena with its capability.

instance ReadBytes[Domain!['k], Str['k]]source

forall 'k.
instance ReadBytes[Domain!['k], Str['k]] { ... }

Text of a domain is read through the domain itself, which lends its capability to read it.