template<typename Derived>
class pw::allocator::BasicBlock< Derived >
Base mix-in for block implementations.
This CRTP-style type can be combined with block mix-in types. Block mix-ins are stateless and trivially constructible. Mix-ins require the derived class to implement methods to access and modify state, such has how to return a block's size or a pointer to its usable memory.
The mix-ins also follow the NVI pattern. This allows mix-ins to layer behavior for a particular method. For example, the implementation of AllocFirst
in AlignableBlock
handles allocation with larger than default alignment requirements, and delegates to AllocatableBlock
for other requests. The derived type provided by the concrete block implementation can implement the public method that delegates to the correct mix-in. Overridable methods are named Do...
.
These mix-ins guarantee a number of invariants hold at the beginning and end of each regular public API call. Each mix-in can enforce invariants by overriding DoCheckInvariants
. The concrete block implementation should override the same method by calling each of the relevant mix-in methods. Calling a public API method within an implementation of DoCheckInvariants
will lead to infinite recursion. To avoid this, mix-ins provide and/or require versions of methods named ...Unchecked
that skip checking invariants. These should only be used within the context of DoCheckInvariants
or other ...Unchecked
methods.
This mix-in requires its derived type provide the following symbols:
- static constexpr size_t DefaultAlignment()
- Returns the alignment of the block type. Must be a power of two.
- static constexpr size_t BlockOverhead()
- Returns the size of the metadata at the start of a block, before its usable space.
- static constexpr size_t MaxAddressableSize()
- Size of the largest region that can be addressed by a block.
- static constexpr size_t MinInnerSize()
- Returns the minimum inner size of a block. Can be 1 unless the usable space is used to track blocks when they are free.
- static Derived* AsBlock(BytesSpan)
- Instantiates and returns a block for the given region of memory.
- size_t OuterSizeUnchecked() const
- Returns the size of the block. Must be multiple of
kAlignment
.