stdx.allocator.building_blocks.segregator

  • Declaration

    struct Segregator(size_t threshold, SmallAllocator, LargeAllocator);

    Dispatches allocations (and deallocations) between two allocators ( and ) depending on the size allocated, as follows. All allocations smaller than or equal to will be dispatched to . The others will go to .

    Discussion

    If both allocators are , the will also offer methods.

    Examples

    1. import stdx.allocator.building_blocks.free_list : FreeList; import stdx.allocator.gc_allocator : GCAllocator; import stdx.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); assert(b.length == 200); a.deallocate(b);

    • Declaration

      enum uint alignment;

      The alignment offered is the minimum of the two allocators' alignment.

    • Declaration

      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 if , or otherwise. (If one of the allocators does not define , the default implementation in this module applies.)

    • Declaration

      void[] allocate(size_t);

      The memory is obtained from if , or otherwise.

    • Declaration

      void[] alignedAllocate(size_t, uint);

      This method is defined if both allocators define it, and forwards to or appropriately.

    • Declaration

      bool expand(ref void[] b, size_t delta);

      This method is defined only if at least one of the allocators defines it. If defines and , the call is forwarded to . If defines and , the call is forwarded to . Otherwise, the call returns .

    • Declaration

      bool reallocate(ref void[] b, size_t s);

      This method is defined only if at least one of the allocators defines it. If defines and , the call is forwarded to . If defines and , the call is forwarded to . Otherwise, the call returns .

    • Declaration

      bool alignedReallocate(ref void[] b, size_t s);

      This method is defined only if at least one of the allocators defines it, and work similarly to .

    • Declaration

      Ternary owns(void[] b);

      This method is defined only if both allocators define it. The call is forwarded to if , or otherwise.

    • Declaration

      bool deallocate(void[] b);

      This function is defined only if both allocators define it, and forwards appropriately depending on .

    • Declaration

      bool deallocateAll();

      This function is defined only if both allocators define it, and calls for them in turn.

    • Declaration

      Ternary empty();

      This function is defined only if both allocators define it, and returns the conjunction of calls for the two.

    • Declaration

      ref auto allocatorForSize(size_t s)();

      Composite allocators involving nested instantiations of make it difficult to access individual sub-allocators stored within. simplifies the task by supplying the allocator nested inside a that is responsible for a specific size .

      Example:

      1. 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);

  • Declaration

    template Segregator(Args...) if (Args.length > 3)

    A with more than three arguments expands to a composition of elemental s, as illustrated by the following example:

    Discussion

    1. alias A = Segregator!( n1, A1, n2, A2, n3, A3, A4 );


    With this definition, allocation requests for bytes or less are directed to ; requests between and bytes (inclusive) are directed to ; requests between and bytes (inclusive) are directed to ; and requests for more than bytes are directed to . If some particular range should not be handled, may be used appropriately.

    Examples

    1. import stdx.allocator.building_blocks.free_list : FreeList; import stdx.allocator.gc_allocator : GCAllocator; import stdx.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); assert(b.length == 201); a.deallocate(b);