Skip to main content

Module memory

Module memory 

Source
Expand description

The process’s memory guard. A binary that registers Guard as its global allocator counts the bytes it holds in large allocations, of at least LARGE bytes each. The process’s limit is half of physical memory, unless set_limit sets another. The guard refuses an allocation that would take its count past the limit. It writes one line to stderr naming the limit. Rust then aborts the process, before the machine runs out of memory. Before a function makes an array, generated_len asks admit, which gives a Limit error where the array would pass the limit. peak_held gives the most bytes that a call held at once, which the speed tests count in a case’s footprint. On Linux, the guard also asks the kernel for transparent huge pages for each allocation of at least 4 MiB. The kernel gives them where /sys/kernel/mm/transparent_hugepage/enabled is madvise or always. Without huge pages, each 4 KB page of a new block pays a page fault on its first write. On some hosts those faults take several times as long as the elementwise work that fills the block. basedpl’s bpl, Python extension, browser build, reference test and speed test register the guard, as do rustymath’s Python extension and speed test. set_checks turns on test-only checks: of the size that each free of a large block passes, and, in basedpl’s reference and speed tests, of held after each case. A binary that doesn’t register it counts nothing. There, admit refuses only an array that alone would pass the limit.

Structs§

Guard
The global allocator that counts large allocations, refuses those that would pass the process’s limit and, on Linux, asks for huge pages for the largest. It allocates through A: System, unless Guard::over names another global allocator, as the browser build names talc. Register it as #[global_allocator] static GUARD: Guard = Guard::new();.

Constants§

LARGE
The smallest allocation that the guard counts, 64 KiB: an array of 65,536 bytes, or of 8,192 f64 items. A smaller one costs the guard one comparison.

Functions§

admit
A Limit error where an array of len items would take large allocations past the process’s limit. admit counts each item as an f64, 8 bytes. A Boolean or byte array near the limit can therefore get the error early. A complex or nested array can pass admit and still meet the guard’s refusal. An array under LARGE bytes passes after one comparison.
checks
Whether set_checks turned the test-only checks on.
held
The bytes that the process holds in large allocations now. A binary that doesn’t register the guard counts nothing, and gets 0.
peak_held
What f gives, and the most bytes that the process held at once in large allocations while f ran, beyond what it held when f started. Allocations on other threads count too. A binary that doesn’t register the guard counts nothing, and gets 0.
set_checks
Turns test-only checks on or off. With them on, a guard over System aborts with a backtrace where a free or reallocation of a large block passes more bytes than the block holds, and the test harnesses check that held returns to its start after each case.
set_limit
Sets the process’s limit on bytes held in large allocations to bytes, in place of half of physical memory. A bytes of 0 restores that default.