liveBook · Manning

Chapter 7: Managing stack and heap in resource-constrained systems

What you will learn in chapter 7 of Embedded Software with Rust

The chapter introduction explains why embedded Rust programmers must make deliberate memory-placement choices despite Rust’s ownership guarantees.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The chapter introduction explains why embedded Rust programmers must make deliberate memory-placement choices despite Rust’s ownership guarantees.

Key terms: ownership model, stack, static storage, heap, RAM, virtual memory, fragmentation, static data

7.1 Understanding memory in embedded Rust

This section introduces the memory concepts needed for later discussions of allocation, fixed-capacity containers, and stack protection.

7.1.1 The three memory regions of an embedded Rust program

This section explains where embedded Rust programs place data and how static memory, the stack, and an optional heap differ in lifetime, sizing, and cost.

Key terms: static memory, .text, .data, .bss, stack, stack pointer, stack frame, heap, no_std, allocator

7.1.2 What core provides and what alloc adds

This section distinguishes heap-free core from heap-dependent alloc in no_std Rust and explains when and how to use allocation on a microcontroller.

Key terms: no_std, core, alloc, stack and static memory, dynamic allocation, extern crate alloc, global allocator, designated region of RAM, stack/static storage, allocation failure

7.1.3 Manual memory management: from C to unsafe Rust

This section compares C-style manual heap allocation with unsafe Rust and explains why safe abstractions are normally preferred.

Key terms: unsafe, Layout, null check, deallocation, borrow checker, raw pointer dereferences, invariants, Box::new, dealloc, scope

7.2 Custom allocators for heap usage in embedded Rust

This section introduces the setup needed to use heap-backed collections in a no_std embedded Rust program.

Key terms: alloc crate, no_std, global allocator, region of RAM, Box, Vec

7.2.1 Why heap allocation is an explicit choice in no_std

Explains why heap allocation on a no_std microcontroller requires explicit setup rather than working automatically as it does on a desktop.

Key terms: no_std, GlobalAlloc, #[global_allocator], static byte array, Box::new, Vec::push

7.2.2 Installing a global allocator with embedded-alloc

This section explains how to install and initialize an embedded-alloc global allocator on Cortex-M, including its backing RAM, allocator choices, and critical-section requirement.

Key terms: LlffHeap, llff, TlsfHeap, tlsf, first-fit, alloc, #[global_allocator], backing store, init, MaybeUninit<u8>

7.2.3 An alternative: the buddy system

This section explains how a buddy allocator splits and rejoins heap blocks, what that costs in wasted space, and when it may be preferable to first-fit allocation.

Key terms: buddy allocator, buddies, split, merge, first-fit, buddy system, internal fragmentation, bounded fragmentation, embedded-alloc, buddy_system_allocator

7.2.4 Using the heap: a worked example

The section uses a dynamic firmware message log to show what a global allocator enables, then explains the default response to failed allocations and a fallible alternative.

Key terms: global allocator, Vec<String>, record_event, capacity, backing array, format!, String, alloc, fixed-size buffer, write!

7.2.5 Choosing between allocators in practice

Recommends a default heap allocator for new embedded Rust projects and explains when evidence warrants a different choice.

Key terms: embedded-alloc, llff, tlsf, buddy_system_allocator, profiling, allocator-induced fragmentation

7.2.6 Sizing the heap and avoiding allocation pitfalls

The section shows how to size an embedded heap from observed usage, reduce avoidable Vec reallocations, and place DMA buffers where the peripheral can reach them.

Key terms: HEAP.used(), HEAP.free(), PEAK_USAGE, Vec::with_capacity, Vec::new, reallocation, fragmentation, DMA, allocator’s backing region, statically allocated buffers

7.3 Alternatives to heap allocation in embedded Rust

The section explains why a known maximum buffer size can make fixed-capacity, inline-array containers a useful alternative to heap allocation.

Key terms: fixed-capacity container, inline array, allocator overhead, fragmentation risk

7.3.1 Fixed-capacity containers with heapless

Introduces heapless fixed-capacity containers, their explicit overflow handling, and their use in a UART parser and an interrupt-to-main-loop queue.

Key terms: heapless, fixed-capacity, inline-storage, Vec<T, N>, String<N>, Result<(), T>, Vec::push, String::push_str, fixed capacity, UART command parser

7.3.2 Lighter-weight alternative: arrayvec

This section explains when arrayvec is a lighter fixed-capacity alternative to heapless and how to choose between its panicking and fallible overflow APIs.

Key terms: ArrayVec::push, ArrayString::push_str, panic on overflow, fallible counterparts, arrayvec, ArrayVec<T, N>, ArrayString<N>, heapless::Vec, heapless::String

7.3.3 Hybrid containers: tinyvec and smallvec

Explains how tinyvec and smallvec avoid common-case heap allocation, what they still require in no_std firmware, and when to choose them over fixed-capacity containers.

Key terms: tinyvec, smallvec, inline array, inline capacity, heap allocation, no_std, extern crate alloc, global allocator, Default, unsafe

7.3.4 Choosing among the alternatives

This section gives criteria for choosing heapless, arrayvec, or a hybrid container in embedded firmware.

Key terms: heapless, arrayvec, allocator, Result-returning APIs, tinyvec, smallvec, hybrid pattern, worst-case buffer size

7.4 Managing the stack for memory safety

This section explains why stack overflow can silently corrupt an embedded Rust program and why stack safety requires more than compiler analysis alone.

Key terms: stack, stack overflow, exception entry, worst-case stack usage, static analysis, recursion, function pointers, interrupt nesting

7.4.1 How stack overflows happen

This subsection explains how a Cortex-M stack can exceed its reserved region and silently corrupt static data.

Key terms: stack pointer (SP), stack overflow, stack region, interrupt nesting, cortex-m-rt, .bss, .data, static memory, deterministic fault

7.4.2 Estimating stack usage with static analysis

This section shows how to estimate stack usage with cargo-call-stack, account for what its estimate misses, and distinguish stack analysis from section-size checks.

Key terms: cargo-call-stack, -Z emit-stack-sizes, rust-src, LTO, memory.x, indirect calls, function pointers, trait objects, dyn Trait, interrupt handlers

7.4.3 Detecting overflow at link time with flip-link

This section explains how flip-link changes the Cortex-M memory layout so a main-stack overflow causes a diagnosable fault instead of silent memory corruption.

Key terms: flip-link, main stack, .bss, .data, bus fault, linker wrapper, .cargo/config.toml, rustc, rust-lld, memory.x

7.4.4 Software canaries as a fallback

This section explains how software canaries estimate actual stack usage by painting unused stack memory and checking it later.

Key terms: software canaries, stack depth, _stack_start, _stack_bottom, paint_stack, memory.x, unused_stack_words(), defmt, RTT

7.4.5 Hardware-enforced guard regions with the MPU

This section explains how MPU guard regions and, on supported Armv8-M cores, stack limit registers detect stack overflow in hardware.

Key terms: Memory Protection Unit, MPU, no-access guard region, memory management fault, MemoryManagement exception, RegisterBlock, CTRL, RNR, RBAR, RASR

7.5 Summary

Concepts explained in this chapter

← Chapter 6: Debugging, observability, and profiling Chapter 8: Rust-C Integration and Migration →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.