MAGE  v0.171.0
Matthias Advanced Game Engine
mage::MemoryArena::Allocator< T > Class Template Reference

#include <memory_arena.hpp>

Public Types

using value_type = T
 
using size_type = std::size_t
 
using difference_type = std::ptrdiff_t
 
using propagate_on_container_move_assignment = std::true_type
 
using is_always_equal = std::false_type
 

Public Member Functions

 Allocator (const Allocator &allocator) noexcept=default
 
 Allocator (Allocator &&allocator) noexcept=default
 
template<typename U >
 Allocator (const Allocator< U > &allocator) noexcept
 
 ~Allocator ()=default
 
Allocatoroperator= (const Allocator &allocator)=delete
 
Allocatoroperator= (Allocator &&allocator) noexcept=default
 
T * allocate (std::size_t count)
 
T * allocate (std::size_t count, [[maybe_unused]] const void *hint)
 
void deallocate ([[maybe_unused]] T *data, [[maybe_unused]] std::size_t count) const noexcept
 
template<typename U >
bool operator== (const Allocator< U > &rhs) const noexcept
 
template<typename U >
bool operator!= (const Allocator< U > &rhs) const noexcept
 

Private Member Functions

 Allocator (NotNull< MemoryArena * > memory_arena) noexcept
 

Private Attributes

NotNull< MemoryArena *> m_memory_arena
 

Friends

class MemoryArena
 

Detailed Description

template<typename T>
class mage::MemoryArena::Allocator< T >

A class of allocators for memory arenas.

Template Parameters
TThe data type.

Member Typedef Documentation

◆ difference_type

template<typename T >
using mage::MemoryArena::Allocator< T >::difference_type = std::ptrdiff_t

◆ is_always_equal

template<typename T >
using mage::MemoryArena::Allocator< T >::is_always_equal = std::false_type

◆ propagate_on_container_move_assignment

template<typename T >
using mage::MemoryArena::Allocator< T >::propagate_on_container_move_assignment = std::true_type

◆ size_type

template<typename T >
using mage::MemoryArena::Allocator< T >::size_type = std::size_t

◆ value_type

template<typename T >
using mage::MemoryArena::Allocator< T >::value_type = T

Constructor & Destructor Documentation

◆ Allocator() [1/4]

template<typename T >
mage::MemoryArena::Allocator< T >::Allocator ( const Allocator< T > &  allocator)
defaultnoexcept

Constructs an allocator from the given allocator.

Parameters
[in]allocatorA reference to the allocator to copy.

◆ Allocator() [2/4]

template<typename T >
mage::MemoryArena::Allocator< T >::Allocator ( Allocator< T > &&  allocator)
defaultnoexcept

Constructs an allocator by moving the given allocator.

Parameters
[in]allocatorA reference to the allocator to move.

◆ Allocator() [3/4]

template<typename T >
template<typename U >
mage::MemoryArena::Allocator< T >::Allocator ( const Allocator< U > &  allocator)
noexcept

Constructs an allocator from the given allocator.

Template Parameters
UThe data type.
Parameters
[in]allocatorA reference to the allocator to copy.

◆ ~Allocator()

template<typename T >
mage::MemoryArena::Allocator< T >::~Allocator ( )
default

Destructs this allocator.

◆ Allocator() [4/4]

template<typename T >
mage::MemoryArena::Allocator< T >::Allocator ( NotNull< MemoryArena * >  memory_arena)
explicitprivatenoexcept

Constructs an allocator.

Parameters
[in]memory_arenaA pointer to the memory arena.

Member Function Documentation

◆ allocate() [1/2]

template<typename T >
T* mage::MemoryArena::Allocator< T >::allocate ( std::size_t  count)

Allocates a block of storage with a size large enough to contain count elements of type T, and returns a pointer to the first element.

Parameters
[in]countThe number of objects of type T to allocate in memory.
Returns
A pointer to the memory block that was allocated. The pointer is a multiple of the alignment.
Exceptions
std::bad_allocFailed to allocate the memory block.

◆ allocate() [2/2]

template<typename T >
T* mage::MemoryArena::Allocator< T >::allocate ( std::size_t  count,
[[maybe_unused] ] const void *  hint 
)

Allocates a block of storage with a size large enough to contain count elements of type T, and returns a pointer to the first element.

Parameters
[in]countThe number of objects of type T to allocate in memory.
[in]hintEither nullptr or a value previously obtained by another call to mage::MemoryArena::Allocator<T>::allocate(std::size_t) and not yet freed with mage::MemoryArena::Allocator<T>::deallocate(T*, std::size_t). When not equal to nullptr, this value may be used as a hint to improve performance by allocating the new block near the one specified. The address of an adjacent element is often a good choice.
Returns
A pointer to the memory block that was allocated. The pointer is a multiple of the alignment.
Exceptions
std::bad_allocFailed to allocate the memory block.

◆ deallocate()

template<typename T >
void mage::MemoryArena::Allocator< T >::deallocate ( [[maybe_unused] ] T *  data,
[[maybe_unused] ] std::size_t  count 
) const
noexcept

Releases a block of storage previously allocated with mage::MemoryArena::Allocator<T>::allocate(std::size_t) and not yet released.

Parameters
[in]dataA pointer to the memory block that needs to be released.
[in]countThe number of objects of type T allocated on the call to allocate this block of storage.
Note
The elements in the array are not destroyed.

◆ operator!=()

template<typename T >
template<typename U >
bool mage::MemoryArena::Allocator< T >::operator!= ( const Allocator< U > &  rhs) const
noexcept

Compares this allocator to the given allocator for non-equality.

Template Parameters
UThe data type.
Parameters
[in]rhsA reference to the allocator to compare with.
Returns
true if and only if storage allocated from this allocator cannot be deallocated from the given allocator, and vice versa. false otherwise.

◆ operator=() [1/2]

template<typename T >
Allocator& mage::MemoryArena::Allocator< T >::operator= ( const Allocator< T > &  allocator)
delete

Copies the given allocator to this allocator.

Parameters
[in]allocatorA reference to the allocator to copy.
Returns
A reference to the copy of the given allocator (i.e. this allocator).

◆ operator=() [2/2]

template<typename T >
Allocator& mage::MemoryArena::Allocator< T >::operator= ( Allocator< T > &&  allocator)
defaultnoexcept

Moves the given allocator to this allocator.

Parameters
[in]allocatorA reference to the allocator to move.
Returns
A reference to the moved allocator (i.e. this allocator).

◆ operator==()

template<typename T >
template<typename U >
bool mage::MemoryArena::Allocator< T >::operator== ( const Allocator< U > &  rhs) const
noexcept

Compares this allocator to the given allocator for equality.

Template Parameters
UThe data type.
Parameters
[in]rhsA reference to the allocator to compare with.
Returns
true if and only if storage allocated from this allocator can be deallocated from the given allocator, and vice versa. false otherwise.

Friends And Related Function Documentation

◆ MemoryArena

template<typename T >
friend class MemoryArena
friend

Member Data Documentation

◆ m_memory_arena

template<typename T >
NotNull< MemoryArena* > mage::MemoryArena::Allocator< T >::m_memory_arena
private

A pointer to the memory arena of this allocator.