Allocator that collects extra data about allocations. Since each piece ofinformation adds size and time overhead, statistics can be individually enabledor disabled through compile-timeflags.
All stats of the form
numXxx record counts of events occurring, such ascalls to functions and specific results. The stats of the form
bytesXxxcollect cumulative sizes.
In addition, the data
callerSize,
callerModule,
callerFile,
callerLine, and
callerTime is associated with each specific allocation.This data prefixes each allocation.
Examples:import std.experimental.allocator.building_blocks.free_list : FreeList;import std.experimental.allocator.gc_allocator : GCAllocator;alias Allocator =StatsCollector!(GCAllocator, Options.all, Options.all);Allocator alloc;auto b = alloc.allocate(10);alloc.reallocate(b, 20);alloc.deallocate(b);import std.file : deleteme, remove;import std.range : walkLength;import std.stdio : File;auto f = deleteme ~"-dlang.std.experimental.allocator.stats_collector.txt";scope(exit) remove(f);Allocator.reportPerCallStatistics(File(f,"w"));alloc.reportStatistics(File(f,"a"));writeln(File(f).byLine.walkLength);// 24
The parent allocator is publicly accessible either as a direct member if it holds state, or as an alias toAllocator.instance otherwise. One may use it for making calls that won't count toward statistics collection.
alias
alignment = Allocator.
alignment;
Alignment offered is equal toAllocator.alignment.
IncrementsnumOwns (per instance and and per call) and forwards to parent.owns(b).
Forwards toparent.allocate. Affects per instance:numAllocate,bytesUsed,bytesAllocated,bytesSlack,numAllocateOK, andbytesHighTide. Affects per call:numAllocate, numAllocateOK, andbytesAllocated.
void[]
alignedAllocate(size_t
n, uint
a);
Forwards toparent.alignedAllocate. Affects per instance:numAlignedAllocate,bytesUsed,bytesAllocated,bytesSlack,numAlignedAllocateOk, andbytesHighTide. Affects per call:numAlignedAllocate,numAlignedAllocateOk, andbytesAllocated.
bool
expand(ref void[]
b, size_t
delta);
Defined whether or notAllocator.expand is defined. Affects per instance:numExpand,numExpandOK,bytesExpanded,bytesSlack,bytesAllocated, andbytesUsed. Affects per call:numExpand,numExpandOK,bytesExpanded, andbytesAllocated.
bool
reallocate(ref void[]
b, size_t
s);
Defined whether or notAllocator.reallocate is defined. Affects per instance:numReallocate,numReallocateOK, numReallocateInPlace,bytesNotMoved,bytesAllocated, bytesSlack,bytesExpanded, andbytesContracted. Affects per call:numReallocate,numReallocateOK,numReallocateInPlace,bytesNotMoved,bytesExpanded,bytesContracted, andbytesMoved.
Defined whether or notAllocator.deallocate is defined. Affects per instance:numDeallocate,bytesUsed, andbytesSlack. Affects per call:numDeallocate andbytesContracted.
Defined only ifAllocator.deallocateAll is defined. Affects per instance and per callnumDeallocateAll.
pure nothrow @nogc @safe Ternary
empty();
Defined only ifOptions.bytesUsed is defined. ReturnsbytesUsed == 0.
void
reportStatistics(R)(auto ref R
output);
Reports per instance statistics tooutput (e.g.stdout). The format is simple: one kind and value per line, separated by a colon, e.g.bytesAllocated:7395404
Defined ifperCallFlags is nonzero.
The file and line of the call.
The options corresponding to the statistics collected.
The values of the statistics. Has the same length asopts.
Format to a string such as:mymodule.d(655): [numAllocate:21, numAllocateOK:21, bytesAllocated:324202].
Defined ifperCallFlags is nonzero. Iterates all monitored file/line instances. The order of iteration is not meaningful (items are inserted at the front of a list upon the first call), so preprocessing the statistics after collection might be appropriate.
void
reportPerCallStatistics(R)(auto ref R
output);
Defined ifperCallFlags is nonzero. Outputs (e.g. to aFile) a simple report of the collected per-call statistics.