std.experimental.allocator.building_blocks.allocator_list
- struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator); 
 template AllocatorList(alias factoryFunction, BookkeepingAllocator = GCAllocator)
- 
Given an object factory of type Factoryor a factory functionfactoryFunction, and optionally alsoBookkeepingAllocatoras a supplemental allocator for bookkeeping,AllocatorListcreates 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), ordeallocateare tried for new allocations in order of their most recent use. Thus, although core operations take in theory Ο(k) time forkallocators in current use, in many workloads the factor is sublinear. Details of the actual strategy may change in future releases.
 AllocatorListis 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 usingAllocatorListwith a large number of allocators should be satisfactory as long as the most-recently-used strategy is fast enough for the application.
 AllocatorListmakes 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.- 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- Factoryobject should be used.- BookkeepingAllocator - Allocator used for storing bookkeeping data. The size of bookkeeping data is proportional to the number of allocators. If - BookkeepingAllocatoris- NullAllocator, then- AllocatorListis "ouroboros-style", i.e. it keeps the bookkeeping data in memory obtained from the allocators themselves. Note that for ouroboros-style management, the size- npassed to- makewill 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 - sweatshopof type- Factory,- sweatshop(n)should return an allocator able to allocate at least- nbytes (i.e.- Factorymust define- opCall(size_t)to return an allocator object). Usually the capacity of allocators created should be much larger than- nsuch that an allocator can be used for many subsequent allocations.- nis passed only to ensure the minimum necessary for the next allocation. The factory object is allowed to hold state, which will be stored inside- AllocatorListas a direct- publicmember called- factory.
 - Examples:
- 
import std.algorithm.comparison : max; import std.experimental.allocator.building_blocks.free_list : ContiguousFreeList; import std.experimental.allocator.building_blocks.null_allocator : NullAllocator; import std.experimental.allocator.building_blocks.region : Region; import std.experimental.allocator.building_blocks.segregator : Segregator; import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mmap_allocator : MmapAllocator; // Ouroboros allocator list based upon 4MB regions, fetched directly from // mmap. All memory is released upon destruction. alias A1 = AllocatorList!((n) => Region!MmapAllocator(max(n, 1024 * 4096)), NullAllocator); // Allocator list based upon 4MB regions, fetched from the garbage // collector. All memory is released upon destruction. alias A2 = AllocatorList!((n) => Region!GCAllocator(max(n, 1024 * 4096))); // Ouroboros allocator list based upon 4MB regions, fetched from the garbage // collector. Memory is left to the collector. alias A3 = AllocatorList!( (n) => Region!NullAllocator(new ubyte[max(n, 1024 * 4096)]), NullAllocator); // Allocator list that creates one freelist for all objects alias A4 = Segregator!( 64, AllocatorList!( (n) => ContiguousFreeList!(NullAllocator, 0, 64)( cast(ubyte[])(GCAllocator.instance.allocate(4096)))), GCAllocator); A4 a; auto small = a.allocate(64); assert(small); a.deallocate(small); auto b1 = a.allocate(1024 * 8192); assert(b1 !is null); // still works due to overdimensioning b1 = a.allocate(1024 * 10); writeln(b1.length); // 1024 * 10
 - alias Allocator = typeof(Factory.init(1));
- 
Alias for typeof(Factory()(1)), i.e. the type of the individual allocators.
- BookkeepingAllocator bkalloc;
- 
If BookkeepingAllocatoris notNullAllocator,bkallocis defined and accessible.
- this(ref Factory plant); 
 this(Factory plant);
- 
Constructs an AllocatorListgiven a factory object. This constructor is defined only ifFactoryhas state.
- enum uint alignment;
- 
The 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 callingmake(s)and delegates the request to it. However, if the allocation fresh off a newly created allocator fails, subsequent calls toallocatewill not cause more calls tomake.
- void[] alignedAllocate(size_t s, uint theAlignment);
- 
Allocate a block of size swith alignmenta. First tries to allocate from the existing list of already-created allocators. If neither can satisfy the request, creates a new allocator by callingmake(s + a - 1)and delegates the request to it. However, if the allocation fresh off a newly created allocator fails, subsequent calls toalignedAllocatewill not cause more calls tomake.
- Ternary owns(void[] b);
- 
Defined only if Allocatordefinesowns. Tries each allocator in turn, in most-recently-used order. If the owner is found, it is moved to the front of the list as a side effect under the assumption it will be used soon.- Returns:
- 
Ternary.yesif one allocator was found to returnTernary.yes,Ternary.noif all component allocators returnedTernary.no, andTernary.unknownif no allocator returnedTernary.yesand at least one returnedTernary.unknown.
 
- bool expand(ref void[] b, size_t delta);
- 
Defined only if Allocator.expandis defined. Finds the owner ofband callsexpandfor it. The owner is not brought to the head of the list.
- bool reallocate(ref void[] b, size_t s);
- 
Defined only if Allocator.reallocateis defined. Finds the owner ofband callsreallocatefor it. If that fails, calls the globalreallocate, which allocates a new block and moves memory.
- bool deallocate(void[] b);
- 
Defined if Allocator.deallocateandAllocator.ownsare defined.
- bool deallocateAll();
- 
Defined only if Allocator.ownsandAllocator.deallocateAllare defined.
- const pure nothrow @nogc @safe Ternary empty();
- 
Returns Ternary.yesif no allocators are currently active,Ternary.nootherwise. This methods never returnsTernary.unknown.
 
    © 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
    https://dlang.org/phobos/std_experimental_allocator_building_blocks_allocator_list.html