std.experimental.allocator.building_blocks.quantizer
Types 1
This allocator sits on top of ParentAllocator and quantizes allocation sizes, usually from arbitrary positive numbers to a small set of round numbers (e.g. powers of two, page sizes etc). This technique is commonly used to:
- Preallocate more memory than requested such that later on, when
reallocation is needed (e.g. to grow an array), expansion can be done quickly in place. Reallocation to smaller sizes is also fast (in-place) when the new size requested is within the same quantum as the existing size. Code that's reallocation-heavy can therefore benefit from fronting a generic allocator with a
Quantizer. These advantages are present even ifParentAllocatordoes not support reallocation at all. - Improve behavior of allocators sensitive to allocation sizes, such as
FreeListandFreeTree. Rounding allocation requests up makes for smaller free lists/trees at the cost of slack memory (internal fragmentation).
The following methods are forwarded to the parent allocator if present: allocateAll, owns, deallocateAll, empty.
Preconditions: roundingFunction must satisfy three constraints. These are not enforced (save for the use of assert) for the sake of efficiency.
roundingFunction(n) >= nfor allnof typesize_t;roundingFunctionmust be monotonically increasing, i.e.roundingFunction(n1) <= roundingFunction(n2)for alln1 < n2;roundingFunctionmust benothrow,@safe,@nogcandpure, i.e.always return the same value for a given
n.
ParentAllocator.alignment alignmentAlignment is identical to that of the parent.size_t goodAllocSize(size_t n)Returns `roundingFunction(n)`.void[] allocate(size_t n)Gets a larger buffer `buf` by calling `parent.allocate(goodAllocSize(n))`. If `buf` is `null`, returns `null`. Otherwise, returns buf[0 .. n].bool expand(ref void[] b, size_t delta)First checks whether there's enough slack memory preallocated for `b` by evaluating b.length + delta <= goodAllocSize(b.length). If that's the case, expands `b` in place. Otherwise, attempts to use...bool reallocate(ref void[] b, size_t s)Expands or shrinks allocated block to an allocated size of goodAllocSize(s). Expansion occurs in place under the conditions required by `expand`. Shrinking occurs in place if goodAllocSize(b.length...