pub struct BumpAllocator {
pub start: usize,
pub len: usize,
}Expand description
A bump allocator over the SVM heap region.
Single pass and never frees, like the Solana SDK’s and pinocchio’s: the
first word of the heap holds the cursor and dealloc does nothing. It
is the right allocator for the cold paths of a program that wants
alloc (a Vec while building a CPI) while the hot path allocates
nothing. For programs that must never allocate, prefer no_allocator!
so a stray allocation traps.
§It grows upward, so a requested heap frame is usable
The cursor starts just above Hopper’s reserved scratch and moves up. A
program that declares a larger heap with default_allocator!(heap = N)
can use all of it when the transaction carries
RequestHeapFrame(N). When the transaction does not, the first 32 KiB
still work exactly as before, because small allocations land at the
bottom either way; only an allocation that reaches past the memory the
VM mapped faults, and the VM, not the allocator, stops it. An allocator
that grows downward from the top of a 256 KiB region would fault on its
first allocation in every transaction that forgot the request.
§The last allocation resizes in place
realloc of the most recent allocation moves the cursor and copies
nothing. A Vec that grows while nothing else allocates, the usual
case in a handler, costs its final size and not the sum of every size
it passed through.
§Checkpoints
mark and release_to give a loop
the heap back on every iteration, which a bump allocator otherwise
cannot do.
Install it with default_allocator!.
Fields§
§start: usizeHeap region start address.
len: usizeHeap region length in bytes.
Implementations§
Source§impl BumpAllocator
impl BumpAllocator
Sourcepub const fn new(len: usize) -> Self
pub const fn new(len: usize) -> Self
An allocator over the SVM heap, len bytes long. len is the heap
the program expects: 32 KiB by default, up to 256 KiB when its
transactions request a heap frame. Anything else is a compile
error in a static.
Sourcepub fn mark(&self) -> HeapMark
pub fn mark(&self) -> HeapMark
The current position of the heap, to return to with
release_to.
Sourcepub unsafe fn release_to(&self, mark: HeapMark)
pub unsafe fn release_to(&self, mark: HeapMark)
Give back every allocation made since mark was taken.
§Safety
Nothing allocated after mark may be used again: every Box,
Vec, and String created since must already be dropped or
forgotten. mark must come from this allocator in this invocation.
Trait Implementations§
Source§impl GlobalAlloc for BumpAllocator
impl GlobalAlloc for BumpAllocator
Source§unsafe fn alloc(&self, layout: Layout) -> *mut u8
unsafe fn alloc(&self, layout: Layout) -> *mut u8
layout. Read more