MAGE  v0.171.0
Matthias Advanced Game Engine
mage::MemoryArena Class Reference

#include <memory_arena.hpp>

Classes

class  Allocator
 

Public Member Functions

 MemoryArena (std::size_t maximum_block_size, std::size_t alignment)
 
 MemoryArena (const MemoryArena &arena)=delete
 
 MemoryArena (MemoryArena &&arena)
 
 ~MemoryArena ()
 
MemoryArenaoperator= (const MemoryArena &arena)=delete
 
MemoryArenaoperator= (MemoryArena &&arena)=delete
 
std::size_t GetAlignment () const noexcept
 
std::size_t GetMaximumBlockSize () const noexcept
 
std::size_t GetCurrentBlockSize () const noexcept
 
std::size_t GetTotalBlockSize () const noexcept
 
void * GetCurrentBlockPtr () const noexcept
 
void Reset ()
 
void * Alloc (std::size_t size)
 
template<typename T >
T * AllocData (std::size_t count=1u, bool initialization=false)
 
template<typename T >
Allocator< T > GetAllocator () noexcept
 

Private Types

using MemoryBlock = std::pair< std::size_t, U8 *>
 

Private Attributes

const std::size_t m_alignment
 
const std::size_t m_maximum_block_size
 
MemoryBlock m_current_block
 
std::size_t m_current_block_pos
 
std::list< MemoryBlockm_used_blocks
 
std::list< MemoryBlockm_available_blocks
 

Detailed Description

A class of memory arenas.

Member Typedef Documentation

◆ MemoryBlock

using mage::MemoryArena::MemoryBlock = std::pair< std::size_t, U8* >
private

A type definition for a memory block.

Constructor & Destructor Documentation

◆ MemoryArena() [1/3]

mage::MemoryArena::MemoryArena ( std::size_t  maximum_block_size,
std::size_t  alignment 
)
explicit

Constructs a memory arena with the given block size.

Parameters
[in]maximum_block_sizeThe maximum block size in bytes.
[in]alignmentThe alignment in bytes.

◆ MemoryArena() [2/3]

mage::MemoryArena::MemoryArena ( const MemoryArena arena)
delete

Constructs a memory arena from the given memory arena.

Parameters
[in]arenaA reference to the memory arena to copy.

◆ MemoryArena() [3/3]

mage::MemoryArena::MemoryArena ( MemoryArena &&  arena)
default

Constructs a memory arena by moving the given memory arena.

Parameters
[in]arenaA reference to the memory arena to move.

◆ ~MemoryArena()

mage::MemoryArena::~MemoryArena ( )

Destructs this memory arena.

Member Function Documentation

◆ Alloc()

void * mage::MemoryArena::Alloc ( std::size_t  size)

Allocates a block of memory of the given size on this memory arena.

Parameters
[in]sizeThe requested size in bytes to allocate in memory.
Returns
nullptr if the allocation failed.
A pointer to the memory block that was allocated. The pointer is a multiple of the alignment.

◆ AllocData()

template<typename T >
T* mage::MemoryArena::AllocData ( std::size_t  count = 1u,
bool  initialization = false 
)

Allocates a block of memory on this memory arena.

Template Parameters
TThe data type.
Parameters
[in]countThe number of objects of type T to allocate in memory.
[in]initializationFlag indicating whether the objects need to be initialized (i.e. the constructor needs to be called).
Returns
nullptr if the allocation failed.
A pointer to the memory block that was allocated. The pointer is a multiple of the alignment.
Note
The objects will be constructed with their default empty constructor.

◆ GetAlignment()

std::size_t mage::MemoryArena::GetAlignment ( ) const
noexcept

Returns the alignment of this memory arena.

Returns
The alignment in bytes of this memory arena.

◆ GetAllocator()

template<typename T >
Allocator< T > mage::MemoryArena::GetAllocator ( )
noexcept

Returns an allocator for this memory arena.

Template Parameters
TThe data type of the allocator.
Returns
An allocator for this memory arena.

◆ GetCurrentBlockPtr()

void* mage::MemoryArena::GetCurrentBlockPtr ( ) const
noexcept

Returns a pointer to the current block of this memory arena.

Returns
A pointer to the current block of this memory arena.

◆ GetCurrentBlockSize()

std::size_t mage::MemoryArena::GetCurrentBlockSize ( ) const
noexcept

Returns the block size (in bytes) of the current block of this memory arena.

Returns
The block size (in bytes) of the current block of this memory arena.

◆ GetMaximumBlockSize()

std::size_t mage::MemoryArena::GetMaximumBlockSize ( ) const
noexcept

Returns the maximum block size of this memory arena.

Returns
The maximum block size in bytes of this memory arena.

◆ GetTotalBlockSize()

std::size_t mage::MemoryArena::GetTotalBlockSize ( ) const
noexcept

Returns the block size (in bytes) of all blocks of this memory arena.

Returns
The block size (in bytes) of all blocks of this memory arena.

◆ operator=() [1/2]

MemoryArena& mage::MemoryArena::operator= ( const MemoryArena arena)
delete

Copies the given memory arena to this memory arena.

Parameters
[in]arenaA reference to the memory arena to copy.
Returns
A reference to the copy of the given memory arena (i.e. this memory arena).

◆ operator=() [2/2]

MemoryArena& mage::MemoryArena::operator= ( MemoryArena &&  arena)
delete

Moves the given memory arena to this memory arena.

Parameters
[in]arenaA reference to the memory arena to move.
Returns
A reference to the moved memory arena (i.e. this memory arena).

◆ Reset()

void mage::MemoryArena::Reset ( )

Resets this memory arena.

Member Data Documentation

◆ m_alignment

const std::size_t mage::MemoryArena::m_alignment
private

The alignment in bytes of this memory arena.

◆ m_available_blocks

std::list< MemoryBlock > mage::MemoryArena::m_available_blocks
private

A collection containing the available blocks of this memory arena.

◆ m_current_block

MemoryBlock mage::MemoryArena::m_current_block
private

The current block of this memory arena.

◆ m_current_block_pos

std::size_t mage::MemoryArena::m_current_block_pos
private

The current block position of this memory arena.

◆ m_maximum_block_size

const std::size_t mage::MemoryArena::m_maximum_block_size
private

The maximum block size in bytes of this memory arena.

◆ m_used_blocks

std::list< MemoryBlock > mage::MemoryArena::m_used_blocks
private

A collection containing the used blocks of this memory arena.