Expand description
Closure placement without a heap and without alloc.
thread::spawn needs to move a FnOnce value into a task’s memory and
later run it from a backend-agnostic entry point that knows nothing about
the closure’s type. Two pieces make that work with zero dependencies:
- A tiny
#[repr(C)]header carrying a monomorphized runner function pointer, so the generic type is recovered at run time:(hdr.run)(base + hdr.data_off). - The closure value itself is
ptr::write-n into arena memory carved bycrate::arena, andptr::read-n back out when it runs — the exact ownership danceBox<dyn FnOnce()>performs, done by hand.
This is why the kernel does not need extern crate alloc, a
#[global_allocator], or the libc/heapless/linked_list_allocator
crates on bare metal.
Structs§
- Closure
Header - Header written at the start of a closure block.
Constants§
- HEADER_
SIZE - Size of the header; also the alignment the block is guaranteed to have.
Functions§
- block_
align_ for - Alignment required for a block holding a closure of type
F. - block_
size_ for - Size of the block needed to hold a closure of type
F. - drop_
blob ⚠ - Destroy a closure that will never run (used on spawn-failure paths), so its captures are dropped properly instead of leaking.
- place⚠
- Write
finto the block atbaseand returnbase(the pointer handed to the task trampoline). - run⚠
- Execute and consume the closure stored at
base.