View source code
							
							
						
								Display the source code in std/experimental/allocator/building_blocks/quantizer.d from which this
								page was generated on github.
							
						
							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
								local clone.
							
						Struct std.experimental.allocator.building_blocks.quantizer.Quantizer
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:
						
				struct Quantizer(ParentAllocator, alias roundingFunction)
				;
						
					
				- 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.
Fields
| Name | Type | Description | 
|---|---|---|
| parent | ParentAllocator | The parent allocator. Depending on whether ParentAllocatorholds state
    or not, this is a member variable or an alias forParentAllocator. | 
Methods
| Name | Description | 
|---|---|
| alignedAllocate | Defined only if parentexists and works similarly toallocateby forwarding toparent. | 
| alignedReallocate | Defined only if ParentAllocatorexists. Expansion
    occurs in place under the conditions required byexpand. Shrinking
    occurs in place ifgoodAllocSize(b. | 
| allocate | Gets a larger buffer bufby callingparent. Ifbufisnull, returnsnull. Otherwise, returnsbuf[0 .. n]. | 
| deallocate | Defined if ParentAllocatorexists and forwards toparent. | 
| expand | First checks whether there's enough slack memory preallocated for bby evaluatingb. If that's
    the case, expandsbin place. Otherwise, attempts to useparentappropriately if present. | 
| goodAllocSize | Returns roundingFunction(n). | 
| reallocate | Expands or shrinks allocated block to an allocated size of     goodAllocSize(s). Expansion occurs in place under the conditions required
    byexpand. Shrinking occurs in place ifgoodAllocSize(b. | 
Preconditions
roundingFunction must satisfy three constraints. These are
not enforced (save for the use of assert) for the sake of efficiency.
- roundingFunction(n) >= nfor all- nof type- size_t;
- roundingFunctionmust be monotonically increasing, i.e.- roundingFunction(n1) <= roundingFunction(n2)for all- n1 < n2;
- roundingFunctionmust be- nothrow,- @safe,- @nogcand- pure, i.e. always return the same value for a given- n.
Example
import stdAuthors
License
					Copyright © 1999-2022 by the D Language Foundation | Page generated by ddox.