class FrameAllocator

Frame allocator.

Performs very fast allocations using a bump allocator pattern but can only free all of its memory at once. Perfect for allocations that last just a single frame.

Frames can be nested within each other using MarkFrame() and Clear(). Each MarkFrame() increases the frame depth, and Clear() decreases it. Memory allocated at a specific depth must be freed at the same depth. In debug builds, attempting to free memory at a different depth than it was allocated will trigger an assertion.

Public

Constructors

FrameAllocator

FrameAllocator(u32 blockSize = kDefaultBlockSize)

Methods

~FrameAllocator

~FrameAllocator() noexcept

Allocate

u8 *Allocate(u32 allocationSize)

Allocates a new block of memory of the specified size.

allocationSize
Amount of memory to allocate, in bytes.

AllocateAligned

u8 *AllocateAligned(u32 allocationSize, u32 alignment)

Allocates a new block of memory of the specified size aligned to the specified boundary.

If the aligment is less or equal to 16 it is more efficient to use the AllocAligned16() alternative of this method.

allocationSize
Amount of memory to allocate, in bytes.
alignment
Alignment of the allocated memory. Must be power of two.

Free

void Free(u8 *data)

Deallocates a previously allocated block of memory.

MarkFrame

void MarkFrame()

Marks the beginning of a new frame scope.

Increments the frame depth counter. The next call to Clear() will only clear memory allocated past this point.

Clear

void Clear()

Deallocates all allocated memory since the last call to MarkFrame() (or all the memory if there was no call to MarkFrame()).

Decrements the frame depth counter.

Private

Methods

AllocateBlock

MemoryBlock *AllocateBlock(u32 wantedSize)

Allocates a dynamic block of memory of the wanted size.

The exact allocation size might be slightly higher in order to store block meta data.

DeallocateBlock

void DeallocateBlock(MemoryBlock *block)

Frees a memory block.

Fields

mBlockSize

u32 mBlockSize

mBlocks

Vector<MemoryBlock *> mBlocks

mFreeBlock

MemoryBlock * mFreeBlock

mNextBlockIndex

u32 mNextBlockIndex

mTotalAllocatedBytes

std::atomic<u32> mTotalAllocatedBytes

mLastFrame

void * mLastFrame

mCurrentFrameDepth

u32 mCurrentFrameDepth