Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator > Class Template Reference

STL-compatible allocator adapter for custom allocators. More...

#include <stl_allocator_adapter.hpp>

Classes

struct  rebind
 Rebind support for allocating different types. More...
 

Public Types

using value_type = T
 
using size_type = 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

constexpr STLAllocatorAdapter (UnderlyingAllocator &allocator) noexcept
 Constructs adapter with reference to underlying allocator.
 
template<typename U >
constexpr STLAllocatorAdapter (const STLAllocatorAdapter< U, UnderlyingAllocator > &other) noexcept
 Rebind copy constructor.
 
constexpr STLAllocatorAdapter (const STLAllocatorAdapter &) noexcept=default
 
constexpr STLAllocatorAdapter (STLAllocatorAdapter &&) noexcept=default
 
constexpr ~STLAllocatorAdapter () noexcept=default
 
template<typename U >
constexpr STLAllocatorAdapteroperator= (const STLAllocatorAdapter< U, UnderlyingAllocator > &other) noexcept
 
constexpr STLAllocatorAdapteroperator= (const STLAllocatorAdapter &) noexcept=default
 
constexpr STLAllocatorAdapteroperator= (STLAllocatorAdapter &&) noexcept=default
 
T * allocate (size_type count)
 Allocates memory for n objects of type T.
 
void deallocate (T *ptr, size_type count) noexcept
 Deallocates memory for n objects.
 
template<typename U >
constexpr bool operator== (const STLAllocatorAdapter< U, UnderlyingAllocator > &other) const noexcept
 Equality comparison.
 
template<typename U >
constexpr bool operator!= (const STLAllocatorAdapter< U, UnderlyingAllocator > &other) const noexcept
 Inequality comparison.
 
constexpr size_type max_size () const noexcept
 Returns maximum number of objects that can be allocated.
 
constexpr UnderlyingAllocator * get_allocator () const noexcept
 Gets pointer to underlying allocator.
 
template<typename U >
constexpr auto operator= (const STLAllocatorAdapter< U, UnderlyingAllocator > &other) noexcept -> STLAllocatorAdapter &
 

Friends

template<typename U , typename A >
class STLAllocatorAdapter
 

Detailed Description

template<typename T, typename UnderlyingAllocator>
class helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >

STL-compatible allocator adapter for custom allocators.

Wraps custom allocators to work with STL containers. The underlying allocator must remain alive for the lifetime of any containers using this adapter.

Note
Thread-safety depends on the underlying allocator.
The underlying allocator is held by reference - ensure it outlives all containers.
Template Parameters
TType of objects to allocate
UnderlyingAllocatorThe custom allocator type

Definition at line 31 of file stl_allocator_adapter.hpp.

Member Typedef Documentation

◆ difference_type

template<typename T , typename UnderlyingAllocator >
using helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::difference_type = std::ptrdiff_t

◆ is_always_equal

template<typename T , typename UnderlyingAllocator >
using helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::is_always_equal = std::false_type

◆ propagate_on_container_move_assignment

template<typename T , typename UnderlyingAllocator >
using helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::propagate_on_container_move_assignment = std::true_type

◆ size_type

template<typename T , typename UnderlyingAllocator >
using helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::size_type = size_t

◆ value_type

template<typename T , typename UnderlyingAllocator >
using helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::value_type = T

Constructor & Destructor Documentation

◆ STLAllocatorAdapter() [1/4]

template<typename T , typename UnderlyingAllocator >
constexpr helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::STLAllocatorAdapter ( UnderlyingAllocator &  allocator)
inlineexplicitconstexprnoexcept

Constructs adapter with reference to underlying allocator.

Parameters
allocatorReference to the custom allocator to use

Definition at line 53 of file stl_allocator_adapter.hpp.

53: allocator_(&allocator) {}

◆ STLAllocatorAdapter() [2/4]

template<typename T , typename UnderlyingAllocator >
template<typename U >
constexpr helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::STLAllocatorAdapter ( const STLAllocatorAdapter< U, UnderlyingAllocator > &  other)
inlineexplicitconstexprnoexcept

Rebind copy constructor.

Allows converting between allocators of different types.

Definition at line 60 of file stl_allocator_adapter.hpp.

61 : allocator_(other.allocator_) {}

◆ STLAllocatorAdapter() [3/4]

template<typename T , typename UnderlyingAllocator >
constexpr helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::STLAllocatorAdapter ( const STLAllocatorAdapter< T, UnderlyingAllocator > &  )
constexprdefaultnoexcept

◆ STLAllocatorAdapter() [4/4]

template<typename T , typename UnderlyingAllocator >
constexpr helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::STLAllocatorAdapter ( STLAllocatorAdapter< T, UnderlyingAllocator > &&  )
constexprdefaultnoexcept

◆ ~STLAllocatorAdapter()

template<typename T , typename UnderlyingAllocator >
constexpr helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::~STLAllocatorAdapter ( )
constexprdefaultnoexcept

Member Function Documentation

◆ allocate()

template<typename T , typename UnderlyingAllocator >
T * helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::allocate ( size_type  count)
inline

Allocates memory for n objects of type T.

Exceptions
std::bad_allocif allocation fails
Parameters
countCount of objects to allocate space for
Returns
Pointer to allocated memory
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 135 of file stl_allocator_adapter.hpp.

135 {
136 if (count > max_size()) {
137 throw std::bad_alloc();
138 }
139
140 const size_t size = count * sizeof(T);
141 constexpr size_t alignment = std::max(alignof(T), kMinAlignment);
142
143 auto result = allocator_->Allocate(size, alignment);
144
145 if (result.ptr == nullptr) {
146 throw std::bad_alloc();
147 }
148
149 return static_cast<T*>(result.ptr);
150}
constexpr size_type max_size() const noexcept
Returns maximum number of objects that can be allocated.
constexpr size_t kMinAlignment
Minimum alignment for any allocation.

◆ deallocate()

template<typename T , typename UnderlyingAllocator >
void helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::deallocate ( T *  ptr,
size_type  count 
)
inlinenoexcept

Deallocates memory for n objects.

Parameters
ptrPointer to deallocate
countCount of objects (may be ignored by some allocators)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 153 of file stl_allocator_adapter.hpp.

153 {
154 if (ptr == nullptr) [[unlikely]] {
155 return;
156 }
157
158 // FrameAllocator, DoubleFrameAllocator, and NFrameAllocator don't support individual deallocation
159 if constexpr (std::same_as<UnderlyingAllocator, FrameAllocator> ||
160 std::same_as<UnderlyingAllocator, DoubleFrameAllocator>) {
161 // No-op: Frame allocators don't support individual deallocation
162 }
163 // PoolAllocator and FreeListAllocator only need ptr
164 else if constexpr (std::same_as<UnderlyingAllocator, PoolAllocator> ||
165 std::same_as<UnderlyingAllocator, FreeListAllocator>) {
166 allocator_->Deallocate(ptr);
167 }
168 // StackAllocator needs ptr and size
169 else {
170 allocator_->Deallocate(ptr, count * sizeof(T));
171 }
172}

◆ get_allocator()

template<typename T , typename UnderlyingAllocator >
constexpr UnderlyingAllocator * helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::get_allocator ( ) const
inlineconstexprnoexcept

Gets pointer to underlying allocator.

Returns
Pointer to underlying allocator
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 117 of file stl_allocator_adapter.hpp.

117{ return allocator_; }

◆ max_size()

template<typename T , typename UnderlyingAllocator >
constexpr size_type helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::max_size ( ) const
inlineconstexprnoexcept

Returns maximum number of objects that can be allocated.

Returns
Maximum size
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 109 of file stl_allocator_adapter.hpp.

109 {
110 return std::numeric_limits<size_type>::max() / sizeof(T);
111 }

◆ operator!=()

template<typename T , typename UnderlyingAllocator >
template<typename U >
constexpr bool helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator!= ( const STLAllocatorAdapter< U, UnderlyingAllocator > &  other) const
inlineconstexprnoexcept

Inequality comparison.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 101 of file stl_allocator_adapter.hpp.

101 {
102 return !(*this == other);
103 }

◆ operator=() [1/4]

template<typename T , typename UnderlyingAllocator >
constexpr STLAllocatorAdapter & helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator= ( const STLAllocatorAdapter< T, UnderlyingAllocator > &  )
constexprdefaultnoexcept

◆ operator=() [2/4]

template<typename T , typename UnderlyingAllocator >
template<typename U >
constexpr STLAllocatorAdapter & helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator= ( const STLAllocatorAdapter< U, UnderlyingAllocator > &  other)
constexprnoexcept

◆ operator=() [3/4]

template<typename T , typename UnderlyingAllocator >
template<typename U >
constexpr auto helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator= ( const STLAllocatorAdapter< U, UnderlyingAllocator > &  other) -> STLAllocatorAdapter&
constexprnoexcept

Definition at line 128 of file stl_allocator_adapter.hpp.

129 {
130 allocator_ = other.allocator_;
131 return *this;
132}

◆ operator=() [4/4]

template<typename T , typename UnderlyingAllocator >
constexpr STLAllocatorAdapter & helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator= ( STLAllocatorAdapter< T, UnderlyingAllocator > &&  )
constexprdefaultnoexcept

◆ operator==()

template<typename T , typename UnderlyingAllocator >
template<typename U >
constexpr bool helios::memory::STLAllocatorAdapter< T, UnderlyingAllocator >::operator== ( const STLAllocatorAdapter< U, UnderlyingAllocator > &  other) const
inlineconstexprnoexcept

Equality comparison.

Two adapters are equal if they reference the same underlying allocator.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/memory/stl_allocator_adapter.hpp.

Definition at line 93 of file stl_allocator_adapter.hpp.

93 {
94 return allocator_ == other.allocator_;
95 }

Friends And Related Symbol Documentation

◆ STLAllocatorAdapter

template<typename T , typename UnderlyingAllocator >
template<typename U , typename A >
friend class STLAllocatorAdapter
friend