Quantizer
(ParentAllocator, alias roundingFunction);Quantizer
. These advantages are present even ifParentAllocator does notsupport reallocation at all.PreconditionsroundingFunction must satisfy three constraints. These arenot enforced (save for the use ofassert) for the sake of efficiency.
import std.experimental.allocator.building_blocks.free_tree : FreeTree;import std.experimental.allocator.gc_allocator : GCAllocator;size_t roundUpToMultipleOf(size_t s,uint base){auto rem = s % base;return rem ? s + base - rem : s;}// Quantize small allocations to a multiple of cache line, large ones to a// multiple of page sizealias MyAlloc =Quantizer!( FreeTree!GCAllocator, n => roundUpToMultipleOf(n, n <= 16_384 ? 64 : 4096));MyAlloc alloc;const buf = alloc.allocate(256);assert(buf.ptr);
parent
;goodAllocSize
(size_tn
);n
).alignment
;allocate
(size_tn
);allocate
(goodAllocSize(n
)). Ifbuf isnull, returnsnull. Otherwise, returnsbuf[0 .. n].alignedAllocate
(size_tn
, uinta
);alignedAllocate
exists and works similarly toallocate by forwarding toparent.alignedAllocate(goodAllocSize(n), a).expand
(ref void[]b
, size_tdelta
);b
by evaluatingb.length + delta <= goodAllocSize(b.length). If that's the case, expandsb
in place. Otherwise, attempts to useparent.expand
appropriately if present.reallocate
(ref void[]b
, size_ts
);alignedReallocate
(ref void[]b
, size_ts
, uinta
);deallocate
(void[]b
);deallocate
exists and forwards toparent.deallocate(b.ptr[0 .. goodAllocSize(b.length)]).