Direct3D 12 Memory Allocator
Loading...
Searching...
No Matches
Linear allocation algorithm

Each D3D12 memory block managed by this library has accompanying metadata that keeps track of used and unused regions. By default, the metadata structure and algorithm tries to find best place for new allocations among free regions to optimize memory usage. This way you can allocate and free objects in any order.

Default allocation algorithm

Sometimes there is a need to use simpler, linear allocation algorithm. You can create custom pool that uses such algorithm by adding flag D3D12MA::POOL_FLAG_ALGORITHM_LINEAR to D3D12MA::POOL_DESC::Flags while creating D3D12MA::Pool object. Then an alternative metadata management is used. It always creates new allocations after last one and doesn't reuse free regions after allocations freed in the middle. It results in better allocation performance and less memory consumed by metadata.

Linear allocation algorithm

With this one flag, you can create a custom pool that can be used in many ways: free-at-once, stack, double stack, and ring buffer. See below for details. You don't need to specify explicitly which of these options you are going to use - it is detected automatically.

Free-at-once

In a pool that uses linear algorithm, you still need to free all the allocations individually by calling allocation->Release(). You can free them in any order. New allocations are always made after last one - free space in the middle is not reused. However, when you release all the allocation and the pool becomes empty, allocation starts from the beginning again. This way you can use linear algorithm to speed up creation of allocations that you are going to release all at once.

Free-at-once

This mode is also available for pools created with D3D12MA::POOL_DESC::MaxBlockCount value that allows multiple memory blocks.

Stack

When you free an allocation that was created last, its space can be reused. Thanks to this, if you always release allocations in the order opposite to their creation (LIFO - Last In First Out), you can achieve behavior of a stack.

Stack

This mode is also available for pools created with D3D12MA::POOL_DESC::MaxBlockCount value that allows multiple memory blocks.

Double stack

The space reserved by a custom pool with linear algorithm may be used by two stacks:

  • First, default one, growing up from offset 0.
  • Second, "upper" one, growing down from the end towards lower offsets.

To make allocation from the upper stack, add flag D3D12MA::ALLOCATION_FLAG_UPPER_ADDRESS to D3D12MA::ALLOCATION_DESC::Flags.

Double stack

Double stack is available only in pools with one memory block - D3D12MA::POOL_DESC::MaxBlockCount must be 1. Otherwise behavior is undefined.

When the two stacks' ends meet so there is not enough space between them for a new allocation, such allocation fails with usual E_OUTOFMEMORY error.

Ring buffer

When you free some allocations from the beginning and there is not enough free space for a new one at the end of a pool, allocator's "cursor" wraps around to the beginning and starts allocation there. Thanks to this, if you always release allocations in the same order as you created them (FIFO - First In First Out), you can achieve behavior of a ring buffer / queue.

Ring buffer

Ring buffer is available only in pools with one memory block - D3D12MA::POOL_DESC::MaxBlockCount must be 1. Otherwise behavior is undefined.

Additional considerations

Linear algorithm can also be used with Virtual allocator. See flag D3D12MA::VIRTUAL_BLOCK_FLAG_ALGORITHM_LINEAR.