liveBook · Manning

Custom allocator

As explained in Code Like a Pro in Rust

A custom allocator is a programmer- or library-supplied memory allocation implementation that replaces or supplements a language or platform default allocator.

A custom allocator changes how heap memory is allocated for a particular need. Rust normally uses a system-backed global allocator. Use `GlobalAlloc` to replace it program-wide, or the `Allocator` API to select an allocator for individual data structures. At the time described here, `GlobalAlloc` is stable; the `Allocator` API requires nightly Rust. section 5.8

Consider a custom allocator when the default cannot meet a specific requirement:

  • Constrained embedded systems or performance-critical allocation. section 5.8
  • Protected memory for sensitive data, using operating-system memory locking and access controls. section 5.8 section 5.8.2
  • Special allocation across library or language boundaries, or application-level memory-usage tracking. section 5.8

Page-aligned protected memory

A protected-memory allocator rounds the requested size to a page boundary, adds a page before and after the active region, and allocates the whole area with `posix_memalign()` on UNIX or `VirtualAlloc()` on Windows. It returns the active region rather than the surrounding pages. Deallocation must account for those extra pages: the shown `deallocate()` code moves back to the start of the allocation and restores access to the fore and aft regions. section 5.8.2

Figure 5.2 Diagram showing protected memory layout, with fore and aft regions.
CH05_F02_Matthews

The layout distinguishes the returned active region from the surrounding fore and aft regions. section 5.8.2

Keep nightly-only allocator code behind a crate feature when publishing a crate that also supports stable Rust. A protected-memory module can be included in documentation while excluded from doctests unless the nightly feature is enabled, and marked as nightly-only in generated documentation. Prefer the default allocator unless a concrete constraint justifies the added implementation and platform-specific work. section 3.7.2 section 5.8 section 5.8.2

How Code Like a Pro in Rust covers it

How deep it goes
Distinguishes program-wide and per-structure APIs, then sketches page-aligned allocation and deallocation; the protected allocator code is partial.
Examples
Uses a protected-memory allocator with page-aligned active memory and surrounding regions, plus a feature-gated crate module.
What it assumes
Assumes Rust data-structure familiarity and builds on the distinction between heap and stack allocation.
What you can do after
Apply it via `Allocator` for selected data structures or `GlobalAlloc` program-wide, choosing the scope that fits the requirement.
How it connects
Links heap management to `GlobalAlloc`, the `Allocator` API, memory protection, and conditional compilation.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Code Like a Pro in Rust. The section links point to the parts of the book it draws on.