Skip to main content

set_stack_allocator

Function set_stack_allocator 

Source
pub unsafe fn set_stack_allocator(allocator: StackAllocator)
Expand description

Register the allocator used for subsequent coroutine Stack allocations.

Platforms whose global allocator places large allocations in external memory that cannot safely back stacks (such as PSRAM on Espressif boards, which is unreachable while writing flash) should call this once during start-up before launching coroutines.

Each Stack stores the deallocate function from the allocator that created it, so already-live stacks are always released through their matching deallocator even if the hook is replaced afterwards. Pass StackAllocator::GLOBAL to restore the default global allocator.

ยงSafety

  • allocator.allocate must return either a null pointer or a non-null pointer to an exclusive, writable memory block of at least layout.size() bytes aligned to layout.align().
  • Any returned memory must remain valid and accessible (including across interrupts and flash writes on bare-metal targets) until released via allocator.deallocate.
  • allocator.deallocate must safely release any non-null pointer returned by allocator.allocate when passed the exact matching layout.
  • Both functions must be safe to call from any thread or core that creates or drops a Stack, without requiring the caller to hold a critical section.