Expand description
The counting #[global_allocator] of the memory tests (plan section
7.3), and the thread it counts: only the thread under test.
libtest runs a test on a thread of its own while its main thread goes on
allocating (just after the spawn, and for its “has been running for over
60 seconds” notice), so an allocator that counts every thread reads such
an allocation inside a measured window as the test’s own, and a check
with no slack fails now and then. The core starts no thread of its own
(crate::waits), so the test’s thread sees every allocation the core
makes for it. A test’s first line is count_this_thread, then
others_are_not_counted with the allocator’s count.
Allocator is every such allocator: a test file installs one with a
Tally of its own, which says what a block that became live or was
freed does to the file’s counts. Only the allocator decides what
happened: a block counts once System has made it, so a failed
allocation or realloc counts nothing and the block a failed realloc
leaves stays counted (review round 4: seven test files’ copies counted
the new block before realloc and freed the old one whatever it
returned). stray_allocators finds a global allocator of another
kind.
Owner: package B.
Structs§
- Allocator
- A global allocator that forwards to
Systemand tells itsTallyof each block made or freed on acountedthread: of a request beforeSystemsees it (Tally::growing, undone byTally::failed), and of its block onceSystemhas answered: a failure counts nothing, and areallocthat fails leaves its old block live and counted. A growingreallocthat succeeds counts its new block before it frees the old one, the worst case of a movingrealloc: both blocks live at once. A shrinking one frees the old block first, since glibc shrinks in place, unlessSystemallocates, copies and frees instead (an alignment overmalloc’s, or a new size below the alignment), which counts as a growth does: the engine’s allocator’s rule (unityrs_batch::alloc). A tally that panics aborts the process (Tally). A test file installs it with its own tally:#[global_allocator] static ALLOC: Allocator<Mine> = Allocator(Mine);.
Traits§
- Tally
- What a test file counts of the blocks its
Allocatorsees. It is called inside the allocator, so it must not allocate, and it must not panic: a global allocator that unwinds is undefined behaviour (GlobalAlloc’s safety contract), soAllocatorends any unwind out of a tally’s method by aborting the process (review round 6: a tally that panicked unwound through the allocator). A failedassert!orunwrapin a tally is therefore aSIGABRT, after the panic’s message, and never a test failure that unwinds.
Functions§
- count_
this_ thread - Counts the calling thread’s allocations from now on.
- counted
- Whether the calling thread’s allocations are counted. It allocates nothing, so a global allocator may ask it.
- others_
are_ not_ counted - Checks that an allocation on another thread does not move
live, the allocator’s count: a second thread allocates 64 KiB and holds it while the calling thread, which allocates nothing meanwhile, readslivebefore and after. Run it before the test takes any baseline: the second thread’s start and end may leave a constant in the count (a block the calling thread allocated for it and the other thread freed). - stop_
counting_ this_ thread - Stops counting the calling thread’s allocations. A registered thread
calls it before it ends: its exit’s frees (its thread-locals’
destructors, its handle) come after
thread::scopehas joined it, and counted they would land in whatever window a later test has armed. - stray_
allocators - Every
file:lineof a.rsfile underroot(targetdirectories left out) that implementsGlobalAllocitself, outside this module: a counting allocator of its own, whose accounting no test here checks. The engine’s (unityrs_batch::alloc, the binary’s) is the one other, its accounting held by its own tests, and its file is left out for that one implementation only: a count there other than one is reported as<file>: <n> implementations, the exemption is for one. Comment lines are not scanned.