AllocatorList

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 O(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.

  1. struct AllocatorList(Factory, BookkeepingAllocator = GCAllocator)
  2. template AllocatorList(alias factoryFunction, BookkeepingAllocator = GCAllocator)

Members

Aliases

A
alias A = typeof(factoryFunction(size_t(1)))
Undocumented in source.
AllocatorList
alias AllocatorList = .AllocatorList!(Factory, BookkeepingAllocator)
Undocumented in source.

Structs

Factory
struct Factory
Undocumented in source.

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.

Examples

import mir.utility : max;
import stdx.allocator.building_blocks.free_list : ContiguousFreeList;
import stdx.allocator.building_blocks.null_allocator : NullAllocator;
import stdx.allocator.building_blocks.region : Region;
import stdx.allocator.building_blocks.segregator : Segregator;
import stdx.allocator.gc_allocator : GCAllocator;
import stdx.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, 1024u * 4096u)),
    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, 1024u * 4096u)));

// 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, 1024u * 4096u)]),
    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);
assert(b1.length == 1024 * 10);

Meta