Pyrogenesis HEAD
Pyrogenesis, a RTS Engine
PS::Memory::LinearAllocator Class Reference

Linear allocator (also known as an arena allocator or bump allocator) is a simple and fast system that allocates memory sequentially from a single growing memory buffer. More...

#include <LinearAllocator.h>

Collaboration diagram for PS::Memory::LinearAllocator:

Public Member Functions

 LinearAllocator (const std::size_t initialSize, const std::size_t maxCapacity)
 
 ~LinearAllocator ()
 
void * allocate (std::size_t n, std::size_t alignment)
 
void deallocate (void *ptr, std::size_t n)
 
std::size_t GetSize () const
 
std::size_t GetCapacity () const
 
void Release ()
 

Private Attributes

std::size_t m_Size {0u}
 
std::size_t m_Capacity
 
std::size_t m_MaxCapacity
 
std::unique_ptr< std::byte[]> m_Buffer
 
std::uint32_t m_AllocationCount {0u}
 
PS::StaticVector< std::unique_ptr< std::byte[]>, 16 > m_BuffersToFree
 

Detailed Description

Linear allocator (also known as an arena allocator or bump allocator) is a simple and fast system that allocates memory sequentially from a single growing memory buffer.

Each allocation only moves a free pointer forward. Releasing all allocations happens at the Release call. The allocated memory is not initialized.

It's not thread-safe.

It's intendent to use for allocations with a short lifespan (f.e. function scoped).

There is std::pmr::monotonic_buffer_resource but it doesn't provide information about the current capacity and doesn't allow to limit the capacity. Also avoiding virtual calls for frequent allocations.

Constructor & Destructor Documentation

◆ LinearAllocator()

PS::Memory::LinearAllocator::LinearAllocator ( const std::size_t  initialSize,
const std::size_t  maxCapacity 
)
inline

◆ ~LinearAllocator()

PS::Memory::LinearAllocator::~LinearAllocator ( )
inline

Member Function Documentation

◆ allocate()

void * PS::Memory::LinearAllocator::allocate ( std::size_t  n,
std::size_t  alignment 
)
inline

◆ deallocate()

void PS::Memory::LinearAllocator::deallocate ( void *  ptr,
std::size_t  n 
)
inline

◆ GetCapacity()

std::size_t PS::Memory::LinearAllocator::GetCapacity ( ) const
inline

◆ GetSize()

std::size_t PS::Memory::LinearAllocator::GetSize ( ) const
inline

◆ Release()

void PS::Memory::LinearAllocator::Release ( )
inline

Member Data Documentation

◆ m_AllocationCount

std::uint32_t PS::Memory::LinearAllocator::m_AllocationCount {0u}
private

◆ m_Buffer

std::unique_ptr<std::byte[]> PS::Memory::LinearAllocator::m_Buffer
private

◆ m_BuffersToFree

PS::StaticVector<std::unique_ptr<std::byte[]>, 16> PS::Memory::LinearAllocator::m_BuffersToFree
private

◆ m_Capacity

std::size_t PS::Memory::LinearAllocator::m_Capacity
private

◆ m_MaxCapacity

std::size_t PS::Memory::LinearAllocator::m_MaxCapacity
private

◆ m_Size

std::size_t PS::Memory::LinearAllocator::m_Size {0u}
private

The documentation for this class was generated from the following file: