std.experimental.allocator.building_blocks.segregator
- struct Segregator(size_t threshold, SmallAllocator, LargeAllocator);
-
Dispatches allocations (and deallocations) between two allocators (
SmallAllocatorandLargeAllocator) depending on the size allocated, as follows. All allocations smaller than or equal tothresholdwill be dispatched toSmallAllocator. The others will go toLargeAllocator.If both allocators are
shared, theSegregatorwill also offersharedmethods.- Examples:
-
import std.experimental.allocator.building_blocks.free_list : FreeList; import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mallocator : Mallocator; alias A = Segregator!( 1024 * 4, Segregator!( 128, FreeList!(Mallocator, 0, 128), GCAllocator), Segregator!( 1024 * 1024, Mallocator, GCAllocator) ); A a; auto b = a.allocate(200); writeln(b.length); // 200 a.deallocate(b);
- enum uint alignment;
-
The alignment offered is the minimum of the two allocators' alignment.
- static size_t goodAllocSize(size_t s);
-
This method is defined only if at least one of the allocators defines it. The good allocation size is obtained from
SmallAllocatorifs <= threshold, orLargeAllocatorotherwise. (If one of the allocators does not definegoodAllocSize, the default implementation in this module applies.) - void[] allocate(size_t);
-
The memory is obtained from
SmallAllocatorifs <= threshold, orLargeAllocatorotherwise. - void[] alignedAllocate(size_t, uint);
-
This method is defined if both allocators define it, and forwards to
SmallAllocatororLargeAllocatorappropriately. - bool expand(ref void[] b, size_t delta);
-
This method is defined only if at least one of the allocators defines it. If
SmallAllocatordefinesexpandandb.length + delta <= threshold, the call is forwarded toSmallAllocator. IfLargeAllocatordefinesexpandandb.length > threshold, the call is forwarded toLargeAllocator. Otherwise, the call returnsfalse. - bool reallocate(ref void[] b, size_t s);
-
This method is defined only if at least one of the allocators defines it. If
SmallAllocatordefinesreallocateandb.length <= threshold && s <= threshold, the call is forwarded toSmallAllocator. IfLargeAllocatordefinesexpandandb.length > threshold && s > threshold, the call is forwarded toLargeAllocator. Otherwise, the call returnsfalse. - bool alignedReallocate(ref void[] b, size_t s, uint a);
-
This method is defined only if at least one of the allocators defines it, and work similarly to
reallocate. - Ternary owns(void[] b);
-
This method is defined only if both allocators define it. The call is forwarded to
SmallAllocatorifb.length <= threshold, orLargeAllocatorotherwise. - bool deallocate(void[] b);
-
This function is defined only if both allocators define it, and forwards appropriately depending on
b.length. - bool deallocateAll();
-
This function is defined only if both allocators define it, and calls
deallocateAllfor them in turn. - Ternary empty();
-
This function is defined only if both allocators define it, and returns the conjunction of
emptycalls for the two. - ref auto allocatorForSize(size_t s)();
-
Composite allocators involving nested instantiations of
Segregatormake it difficult to access individual sub-allocators stored within.allocatorForSizesimplifies the task by supplying the allocator nested inside aSegregatorthat is responsible for a specific sizes.- Example
alias A = Segregator!(300, Segregator!(200, A1, A2), A3); A a; static assert(typeof(a.allocatorForSize!10) == A1); static assert(typeof(a.allocatorForSize!250) == A2); static assert(typeof(a.allocatorForSize!301) == A3);
- template Segregator(Args...) if (Args.length > 3)
-
A
Segregatorwith more than three arguments expands to a composition of elementalSegregators, as illustrated by the following example:alias A = Segregator!( n1, A1, n2, A2, n3, A3, A4 );
With this definition, allocation requests forn1bytes or less are directed toA1; requests betweenn1 + 1andn2bytes (inclusive) are directed toA2; requests betweenn2 + 1andn3bytes (inclusive) are directed toA3; and requests for more thann3bytes are directed toA4. If some particular range should not be handled,NullAllocatormay be used appropriately.- Examples:
-
import std.experimental.allocator.building_blocks.free_list : FreeList; import std.experimental.allocator.gc_allocator : GCAllocator; import std.experimental.allocator.mallocator : Mallocator; alias A = Segregator!( 128, FreeList!(Mallocator, 0, 128), 1024 * 4, GCAllocator, 1024 * 1024, Mallocator, GCAllocator ); A a; auto b = a.allocate(201); writeln(b.length); // 201 a.deallocate(b);
© 1999–2021 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_experimental_allocator_building_blocks_segregator.html