Skip to main content

Module closure

Module closure 

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

  1. 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).
  2. The closure value itself is ptr::write-n into arena memory carved by crate::arena, and ptr::read-n back out when it runs — the exact ownership dance Box<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§

ClosureHeader
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 f into the block at base and return base (the pointer handed to the task trampoline).
run⚠
Execute and consume the closure stored at base.