#include <stdcontainerma.hpp>
Implementation of std::allocator
to be used with container types provided by the C++ standard library.
This allocator is suitable to use cases of strict or weak monotonic allocation. An alternative version that recycles nodes (and for example bucket arrays in case of std::unordered_set
and std::unordered_map
), is provided with sibling type StdContMARecycling.
A further alternative is given with type StdContMAOptional, which accepts a pointer instead of a reference to an allocator and allows this pointer to be nullptr
, which switches to dynamic allocation.
While the dedicated container types provided with this ALib Module offer a method named Reset (see for example HashTable::Reset), the C++ std containers of-course do not. This is a challenge, because their internal memory will be invalid with a reset of the monotonic allocator used, and for example with class std::vector
there is no interface method that makes them "forget" their internal data array. Its method shrink_to_fit
by its specification is not forced to shrink anything or even dispose the data if the size was 0
when called. This is implementation dependent.
The way out is as simple as radical: The container is just to be reconstructed without prior destruction. This can be done using a C++ placement new. The following code snippet demonstrates this:
T | The type of objects to be allocated. |
Definition at line 161 of file stdcontainerma.hpp.
Inner Classes | |
struct | rebind |
Public Fields | |
MonoAllocator & | allocator |
bool | dbgDeallocationWarning |
Public Methods | |
StdContMA () | |
constexpr | StdContMA (const StdContMA &) noexcept=default |
constexpr | StdContMA (MonoAllocator &pAllocator, bool dbgDeallocationWarning=true) |
constexpr | StdContMA (StdContMA &&) noexcept=default |
template<typename TSibling > | |
StdContMA (TSibling &origin) | |
ALIB_NODISCARD T * | allocate (size_t n, const void *=nullptr) |
void | deallocate (T *p, std::size_t n) |
template<typename U > | |
bool | operator!= (const StdContMA< U > &rhs) const noexcept |
template<typename U > | |
bool | operator== (const StdContMA< U > &rhs) const noexcept |
![]() | |
const_pointer | address (const_reference x) const noexcept |
pointer | address (reference x) const noexcept |
template<typename U , typename... Args> | |
void | construct (U *p, Args &&... args) |
template<typename U > | |
void | destroy (U *p) |
size_t | max_size () const noexcept |
Additional Inherited Members | |
![]() | |
using | const_pointer = const T * |
using | const_reference = const T & |
using | difference_type = ptrdiff_t |
Type definition as required by C++ library standards. | |
using | is_always_equal = std::false_type |
Type definition as required by C++ library standards. | |
using | pointer = T * |
using | reference = T & |
using | size_type = size_t |
Type definition as required by C++ library standards. | |
using | value_type = T |
Type definition as required by C++ library standards. | |
|
inline |
Default constructor declared, but not defined.
std::allocator
) that this object implements, the default constructor has to be declared. Due to the fact that field allocator is of reference type, no declaration can be made.
|
inline |
Copy constructor using an instance of different template type.
TSibling | The originating allocator's type (StdContMA<X>). |
origin | The originating allocator of type TSibling . |
Definition at line 222 of file stdcontainerma.hpp.
|
constexpr |
Constructor for the initial allocator instantiation.
Parameter dbgDisableDeallocationWarning, which defaults to true
might be given after a code is tested to be stict in respect to allocation. (Note: unfortunately, due to the ^design of std::allocator
and its use, this flag can not be changed once a container is constructed. This is why it has to be decided upfront if a warning is to be raised or not). ^
pAllocator | The recycler for allocations and de-allocations. |
dbgDeallocationWarning | As described with this method. Available only in debug builds. |
|
inline |
Passes the allocation request to field allocator.
n | The number of requested objects to allocate storage for. |
Definition at line 289 of file stdcontainerma.hpp.
|
inline |
If allocator is not set, invokes std::free(p)
. Otherwise does nothing.
p | Pointer to the previously allocated memory. |
n | The number of objects allocated. |
Definition at line 300 of file stdcontainerma.hpp.
|
inlinenoexcept |
Comparison operator.
U | The allocation type of the other allocator. |
rhs | The right hand side allocator. |
false
if this and rhs use the same allocator, true
otherwise. Definition at line 272 of file stdcontainerma.hpp.
|
inlinenoexcept |
Comparison operator.
U | The allocation type of the other allocator. |
rhs | The right hand side allocator. |
true
if this and rhs use the same allocator, false
otherwise. Definition at line 260 of file stdcontainerma.hpp.
MonoAllocator& allocator |
The allocator to use.
Definition at line 184 of file stdcontainerma.hpp.
bool dbgDeallocationWarning |
If true
warnings about de-allocation of objects are suppressed. this should be set prior to destructing a container that uses this allocator.
Available only in debug builds.
Definition at line 190 of file stdcontainerma.hpp.