std.experimental.allocator.building_blocks.allocator_list
Types 2
Given an object factory of type Factory or a factory function factoryFunction, and optionally also BookkeepingAllocator as a supplemental allocator for bookkeeping, AllocatorList creates an allocator that lazily creates as many allocators are needed for satisfying client allocation requests.
An embedded list builds a most-recently-used strategy: the most recent allocators used in calls to either allocate, owns (successful calls only), or deallocate are tried for new allocations in order of their most recent use. Thus, although core operations take in theory k time for k allocators in current use, in many workloads the factor is sublinear. Details of the actual strategy may change in future releases.
AllocatorList is primarily intended for coarse-grained handling of allocators, i.e. the number of allocators in the list is expected to be relatively small compared to the number of allocations handled by each allocator. However, the per-allocator overhead is small so using AllocatorList with a large number of allocators should be satisfactory as long as the most-recently-used strategy is fast enough for the application.
AllocatorList makes an effort to return allocated memory back when no longer used. It does so by destroying empty allocators. However, in order to avoid thrashing (excessive creation/destruction of allocators under certain use patterns), it keeps unused allocators for a while.
The shared version of AllocatorList is SharedAllocatorList, which has identical semantics to its single-threaded version. Both BookkeepingAllocator and Allocator provided by factoryFunction must be shared, in order to ensure corectness.
Parameters
factoryFunction | A function or template function (including function literals). New allocators are created by calling factoryFunction(n) with strictly positive numbers n. Delegates that capture their enviroment are not created amid concerns regarding garbage creation for the environment. When the factory needs state, a Factory object should be used. |
BookkeepingAllocator | Allocator used for storing bookkeeping data. The size of bookkeeping data is proportional to the number of allocators. If BookkeepingAllocator is NullAllocator, then AllocatorList is "ouroboros-style", i.e. it keeps the bookkeeping data in memory obtained from the allocators themselves. Note that for ouroboros-style management, the size n passed to make will be occasionally different from the size requested by client code. |
Factory | Type of a factory object that returns new allocators on a need basis. For an object sweatshop of type Factory, sweatshop(n) should return an allocator able to allocate at least n bytes (i.e. Factory must define opCall(size_t) to return an allocator object). Usually the capacity of allocators created should be much larger than n such that an allocator can be used for many subsequent allocations. n is passed only to ensure the minimum necessary for the next allocation. The factory object is allowed to hold state, which will be stored inside AllocatorList as a direct public member called factory. |
private is(BookkeepingAllocator == NullAllocator) ouroborosprivate Node[] allocatorsprivate Node * rootuint alignmentThe alignment offered.void[] allocate(size_t s)Allocate a block of size `s`. First tries to allocate from the existing list of already-created allocators. If neither can satisfy the request, creates a new allocator by calling `make(s)` and dele...void moveAllocators(void[] newPlace)NodeDitto
AllocatorList!(Factory, BookkeepingAllocator) implSpinLock lockimpl.alignment alignmentThe alignment offered.Templates 2
Ditto
Ditto