Vec, Box, String, BTreeMap
The four types the heap gave back. Each solves a problem the stack cannot.
They all live in the alloc crate: the middle layer from no-std.md, between
core (needs nothing) and std (needs an OS). extern crate alloc; pulls it in, and it only
works because we supplied a #[global_allocator].
BTreeMap ─┐
String ───┤
Vec ──────┼──▶ #[global_allocator] ──▶ our heap ──▶ a MemoryRegion the ──▶ RAM
Box ──────┘ (talc) program was granted
Where that diagram was drawn matters, and it moved. It was written when the kernel had a
heap, crates/heap and crates/frames under it. Milestone 14's thesis retired that: the kernel
allocates nothing after boot, crates/heap and crates/slab were deleted once nothing referenced
them (heap.md's banner), and kernel/src/ has no #[global_allocator] at all today. So
the chain above is a userspace one. The allocator algorithm is talc's, the memory
under it is a MemoryRegion capability the program's parent granted, and a program nobody granted
one to has no heap rather than a smaller one. The frame allocator still exists, as
crates/page_frames, one level below anything a program can name.
Box<T>: "this lives on the heap, and I own it"
The simplest, and the foundation for the rest.
let b = Box::new(42u64);
Allocates 8 bytes on the heap, moves the value in, hands you a pointer. Dropping it frees.
Box is malloc + free, with the free proved by the compiler.
Box<T> is exactly one pointer on the stack. Zero overhead.
Three reasons you need it:
Recursive types cannot be sized. This is infinite:
struct Node { value: u64, next: Node } // size = 8 + (8 + (8 + ...
This is 16 bytes:
struct Node { value: u64, next: Option<Box<Node>> }
The indirection is what makes the size finite. Every linked list, tree, and graph in Rust rests on it.
Trait objects. Box<dyn Write>: you don't know the concrete type, so you can't know its
size, so it must be behind a pointer.
Large values you don't want on the stack. Box<[Option<PageFrame>; 1024]> would have put the
16 KiB array on the heap, and the milestone 3 (hand out physical memory, and detect a smashed stack) incident (nifeos/nife:notes/stack.md) would never have happened.
Vec<T>: a growable array
Three fields on the stack (24 bytes). The elements are on the heap.
stack: heap:
┌──────────┐ ┌────┬────┬────┬────┬────┬────┐
│ ptr ────┼─────▶│ 10 │ 20 │ 30 │ │ │ │
│ len 3 │ └────┴────┴────┴────┴────┴────┘
│ cap 6 │ used: 3 spare: 3
└──────────┘
push writes at len and increments. When len == cap, it allocates a bigger buffer
(double), copies everything, and frees the old one.
The doubling is the whole trick. Grow by one each time and N pushes cost O(N²) in copying. Double each time and the copies are rare enough that the average cost per push is constant: amortized O(1).
Which is why our vec_works test (1000 pushes) is a real workout for the allocator: it
reallocates about ten times, and each one is an allocate, a copy, and a free through code we
wrote.
Vec is why MAXIMUM_REGIONS = 16 existed. A fixed array must guess its maximum in advance
and fail if it guessed low. memory.rs still returns TooManyRegions on a machine with more
than 16 memory regions, purely because Vec didn't exist when it was written.
String: growable, owned text
Literally a Vec<u8> with one extra promise: the bytes are valid UTF-8. Same three fields,
same doubling.
The distinction from &str is the same distinction as Vec<T> vs &[T], and it is one of
the first walls people hit in Rust:
| Owns the memory? | Can grow? | What it is | |
|---|---|---|---|
String |
yes: heap buffer, freed on drop | yes | ptr + len + capacity |
&str |
no: it is a view | no | ptr + len |
A &str can point at a literal in .rodata, into the middle of a String's heap buffer, or
at bytes on the stack. It doesn't care and it doesn't own.
That's why &str works in no_std and String doesn't: a view needs no allocator.
format! builds a String, which is why it only started working at milestone 4.
BTreeMap<K, V>: an ordered map
A B-tree: a balanced search tree where each node holds many keys, not one.
Why not HashMap?
HashMap is not in alloc. It is std-only.
It needs a randomly-seeded hasher (to resist hash-collision denial-of-service attacks), and a random seed needs entropy from the operating system. We are the operating system.
So in a kernel you use BTreeMap, and it isn't a compromise: you also get ordered iteration
and range queries for free.
Why a B-tree and not a binary search tree?
Cache locality, and it traces straight back to the memory hierarchy in registers.md.
A binary tree node holds one key and two pointers. Every step down is a pointer chase, and every pointer chase is potentially a ~200-cycle trip to RAM. Twenty levels deep is twenty cache misses.
Rust's BTreeMap packs up to 11 keys per node, laid out contiguously. One cache-line
fetch buys eleven comparisons. Far fewer memory round-trips for the same number of elements,
and memory round-trips are the only thing that matters.
Milestone 7 wants one: a process table mapping PID → process.
What they share
Every one is "owns some heap memory, frees it when dropped." That's Drop, and it is what
makes the heap safe in Rust at all: the compiler proves the free happens exactly once, at the
right time. See the table in heap.md: use-after-free, double-free, and leaks are
heap problems, and ownership is the answer to all three.
| Type | On the stack | On the heap |
|---|---|---|
Box<T> |
8 bytes (a pointer) | size_of::<T>() |
Vec<T> |
24 bytes (ptr, len, cap) | cap × size_of::<T>() |
String |
24 bytes (same) | cap bytes of UTF-8 |
BTreeMap |
a couple of words | the nodes |
The rest of what alloc gave us
All verified working in the kernel, not assumed:
| What it is | When we want it | |
|---|---|---|
VecDeque<T> |
ring buffer / double-ended queue | M5 UART receive queue, M6 run queue |
BinaryHeap<T> |
priority queue | M5 timer wheel: "wake me at T", earliest first |
Arc<T> |
shared ownership, atomically refcounted | M7 Arc<AddressSpace>, Arc<File> |
Rc<T> |
shared ownership, not thread-safe | never, in a kernel. See below. |
BTreeSet<T> |
ordered set | |
Cow<T> |
clone-on-write | |
LinkedList<T> |
doubly linked list | almost never: cache-hostile, same argument as the B-tree above |
[T]::sort() |
stable sort | it needs a temporary buffer, which is why it lives in alloc and not core |
Arc is the answer to "who frees this?" when the answer is "the last one out." Milestone
7 wants it badly: multiple threads in a process share one address space and one set of file
descriptors.
The trap: VecDeque allocates, and interrupt handlers must not
VecDeque::push_back can allocate. If the buffer is full it grows: allocate, copy, free.
Which collides head-on with DECISIONS §9:
Interrupt handlers do not allocate.
A UART receive handler pushing into a VecDeque would take the heap lock in interrupt
context, and that is exactly the deadlock in locking.md.
So milestone 5 needs a fixed-capacity ring buffer that never allocates. Either we write one
(~40 lines, pure logic, host-testable) or we pull in heapless, which is a crate of exactly
these: compile-time capacity, no allocator at all.
Leaning toward writing it: a single-producer/single-consumer lock-free ring is genuinely instructive, and it is the one place where the weak-memory-ordering material in portability.md stops being theoretical.
Rc vs Arc: the difference is a use-after-free
Rc increments its refcount with a plain, non-atomic count += 1: a read, an add, a
write.
Two cores do that simultaneously. Both read 1. Both write 2. An increment is lost. The count says 2 while three things hold the value, the last two drops take it to zero, and memory that is still in use gets freed.
An interrupt handler does it too, on one core: the interrupted code is halfway through the read-modify-write when the handler runs and does its own.
Rust catches this: Rc is !Send, so the compiler refuses to move it across threads. But
knowing why matters, because the compiler's protection ends at the boundary of what it can
see, and a kernel spends a great deal of time outside that boundary.
Arc uses an atomic read-modify-write. On our Cortex-A72 (ARMv8.0, no LSE) that is an
LDXR/STXR retry loop rather than a single CAS, which is precisely the cliff
design/fat-binaries.md is about.
What std needs a kernel service for, and where each stands
Everything std has that a bare kernel lacks needs a kernel service, and each one is a milestone.
script/test runs every row below. The full current map of the std surface is std.md,
which is derived from what the compiler actually built.
| Needs | Now | |
|---|---|---|
HashMap |
a randomly-seeded hasher, so OS entropy | Works. Entropy is a capability (milestone 56 (secrets, credentials, and the entropy to make them safe), entropy.md). Granted, the seed is real; ungranted, std's PAL falls back to a splitmix64 stream over the counter and labels it, which is the §42 (a filesystem must be truthful) rule about not inventing a value silently |
Mutex, RwLock |
a scheduler to block on | Compile and work uncontended, because a process is still one thread. Condvar::wait and Once::wait are the two that genuinely have no answer and end the process; see std.md's abort census |
thread::spawn |
a scheduler | Unsupported. The scheduler exists (milestone 6 (threads, the context switch, and preemption)); what is missing is a decision, not a service. See thread-spawn-fork.md |
Instant, SystemTime |
a clock | Both real. Instant is the virtual counter; SystemTime is a clock page a granted program maps read-only (milestone 51 (wall-clock time), clock.md) |
File, Path, fs |
a filesystem | Real and writable. RedoxFS confined as a userspace server (milestone 32 (a real filesystem: RedoxFS behind a capability FS server), fs-server.md), bound into std::fs by milestone 27 (Rust std on the native ABI) phase two; std::fs::write works |
TcpStream |
a network stack | Bound. smoltcp in a confined net server (milestone 30 (the network stack as a confined component), net.md); TcpStream, TcpListener and outbound UdpSocket all speak the socket contract |
(core::time::Duration already exists, incidentally: it is arithmetic on nanoseconds and needs
nothing. It is Instant that needs hardware, because Instant means now, and nothing in
core knows what time it is.)
Add to this file as new collections come up.