Skip to main content

BumpAllocator

Struct BumpAllocator 

Source
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: usize

Heap region start address.

§len: usize

Heap region length in bytes.

Implementations§

Source§

impl BumpAllocator

Source

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.

Source

pub fn used(&self) -> usize

Bytes handed out so far, alignment padding included.

Source

pub fn remaining(&self) -> usize

Bytes left before the declared end of the heap.

Source

pub fn mark(&self) -> HeapMark

The current position of the heap, to return to with release_to.

Source

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

Source§

unsafe fn alloc(&self, layout: Layout) -> *mut u8

Allocates memory as described by the given layout. Read more
Source§

unsafe fn dealloc(&self, _ptr: *mut u8, _layout: Layout)

Deallocates the block of memory at the given ptr pointer with the given layout. Read more
Source§

unsafe fn realloc( &self, ptr: *mut u8, layout: Layout, new_size: usize, ) -> *mut u8

Shrinks or grows a block of memory to the given new_size in bytes. The block is described by the given ptr pointer and layout. Read more
1.28.0 · Source§

unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8

Behaves like alloc, but also ensures that the contents are set to zero before being returned. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.