Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.
Quantizer(ParentAllocator, alias roundingFunction);
- 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
Quantizer. These advantages are present even if ParentAllocator does not support reallocation at all.
- Improve behavior of allocators sensitive to allocation sizes, such as FreeList and FreeTree. Rounding allocation requests up makes for smaller free lists/trees at the cost of slack memory (internal fragmentation).
Preconditions: roundingFunction must satisfy three constraints. These are not enforced (save for the use of assert) for the sake of efficiency.
- roundingFunction(n) >= n for all n of type size_t;
- roundingFunction must be monotonically increasing, i.e. roundingFunction(n1) <= roundingFunction(n2) for all n1 < n2;
- roundingFunction must be pure, i.e. always return the same value for a given n.
import std.experimental.allocator.building_blocks.free_tree : FreeTree; import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.common : roundUpToMultipleOf; // Quantize small allocations to a multiple of cache line, large ones to a // multiple of page size alias MyAlloc = Quantizer!( FreeTree!GCAllocator, n => n.roundUpToMultipleOf(n <= 16_384 ? 64 : 4096)); MyAlloc alloc; const buf = alloc.allocate(256); assert(buf.ptr);
parentallocator. Depending on whether ParentAllocator holds state or not, this is a member variable or an alias for ParentAllocator.instance.
- Returns roundingFunction(
- enum auto
- Alignment is identical to that of the parent.
- Gets a larger buffer buf by calling parent.
n)). If buf is
null. Otherwise, returns buf[0 ..
- Defined only if parent.
alignedAllocateexists and works similarly to allocate by forwarding to parent.
- First checks whether there's enough slack memory preallocated for
b.length). If that's the case, expands
bin place. Otherwise, attempts to use parent.
expandappropriately if present.
- 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) == goodAllocSize(
- Defined only if ParentAllocator.alignedAllocate exists. Expansion occurs in place under the conditions required by expand. Shrinking occurs in place if goodAllocSize(
b.length) == goodAllocSize(
- Defined if ParentAllocator.
deallocateexists and forwards to parent.
b.ptr[0 .. goodAllocSize(
- 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