ALib C++ Library
Library Version: 2511 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alib::lang::StdAllocator< T, TAllocator > Struct Template Reference

Description:

template<typename T, typename TAllocator>
struct alib::lang::StdAllocator< T, TAllocator >

This struct is an implementation of C++ standard library type std::allocator. It is to be used with container types provided by the C++ standard library in combination with ALib Allocators.

With the inclusion of module ALib Monomem in the ALib Build, this allocator is suitable to use cases of strict or weak monotonic allocation.

Shortcuts for Specific ALib Allocators

The following alias type definitions exist for this type, each addressing a specific allocator:

Along with this, shortcuts to the container types are likewise provided with:

Resetting A Container

While the dedicated container types provided with module ALib Containers offer a method named Reset (see, for example, HashTable::Reset), the C++ standard containers do not. This is a challenge because their internal memory will be invalid with a reset. For example, if allocator type MonoAllocator was used with class std::vector, there is no interface method that makes the vector "forget" its 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 destructed and reconstructed "in place". This can be done using a C++ placement-new. The following code snippet demonstrates this:

// field members or global objects
alib::MonoAllocator allocator( ALIB_DBG("MyAllocator",) 4);
std::vector<MyData, StdMA<MyData>> transactionObjects( allocator );
// method using the allocator and the vector
void processTransaction( /* transaction data */)
{
// perform transaction, collecting stuff in vector and/or allocator
// ...
// ...
// ...
// before we leave we reset the vector and the allocator:
// 1. Destruct the container and contained objects.
// (In case contained objects are trivially destructible, this is not
// needed and usually optimized out by the compiler.)
allocator().Delete(&transactionObjects);
// 2. Reset allocator
allocator.Reset();
// 3. Placement new on the vector object
new( &transactionObjects ) std::vector<MyData, StdMA<MyData>>( allocator );
}
See also
Template Parameters
TThe type of objects to be allocated.
TAllocatorThe allocator type, as prototyped with Allocator.

Definition at line 758 of file allocation.inl.

Inheritance diagram for alib::lang::StdAllocator< T, TAllocator >:
[legend]
Collaboration diagram for alib::lang::StdAllocator< T, TAllocator >:
[legend]

Public Type Index:

using AllocatorType = TAllocator
 Exposes template parameter TAllocator.
 
using allocBase = AllocatorMember<TAllocator>
 The type of the base class that stores the allocator.
 
using difference_type = std::ptrdiff_t
 Type definition as required by C++ library standards.
 
using propagate_on_container_move_assignment = std::false_type
 Type definition as required by C++ library standards.
 
using size_type = std::size_t
 Type definition as required by C++ library standards.
 
using value_type = T
 Type definition as required by C++ library standards.
 
- Public Type Index: inherited from alib::lang::AllocatorMember< TAllocator >
using AllocatorType = TAllocator
 Exposes the allocator type.
 

Public Method Index:

constexpr StdAllocator () noexcept=default
 Parameterless constructor used with heap allocation.
 
constexpr StdAllocator (const StdAllocator &) noexcept=default
 Defaulted copy constructor.
 
template<typename TSibling>
 StdAllocator (const StdAllocator< TSibling, TAllocator > &origin)
 
template<typename TSibling>
requires alib::lang::IsAllocator<typename TSibling::allocator>
constexpr StdAllocator (const TSibling &origin) noexcept
 
constexpr StdAllocator (StdAllocator &&) noexcept=default
 Defaulted move constructor.
 
constexpr StdAllocator (TAllocator &pAllocator) noexcept
 
constexpr ~StdAllocator () noexcept=default
 Parameterless destructor used with heap allocation.
 
constexpr T * allocate (size_t n)
 
constexpr std::allocation_result< T *, std::size_t > allocate_at_least (std::size_t n)
 
constexpr void deallocate (T *p, std::size_t n)
 
- Public Method Index: inherited from alib::lang::AllocatorMember< TAllocator >
template<typename TIf = TAllocator>
requires std::is_default_constructible_v<TIf>
 AllocatorMember ()
 
 AllocatorMember (TAllocator &pAllocator) noexcept
 
AllocatorInterface< TAllocator > AI () const noexcept
 
TAllocator & GetAllocator () const noexcept
 

Additional Inherited Members

- Protected Field Index: inherited from alib::lang::AllocatorMember< TAllocator >
TAllocator * allocator
 The allocator stored.
 

Type Definition Details:

◆ AllocatorType

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::AllocatorType = TAllocator

Exposes template parameter TAllocator.

Definition at line 766 of file allocation.inl.

◆ allocBase

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::allocBase = AllocatorMember<TAllocator>

The type of the base class that stores the allocator.

Definition at line 769 of file allocation.inl.

◆ difference_type

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::difference_type = std::ptrdiff_t

Type definition as required by C++ library standards.

Definition at line 762 of file allocation.inl.

◆ propagate_on_container_move_assignment

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::propagate_on_container_move_assignment = std::false_type

Type definition as required by C++ library standards.

Definition at line 763 of file allocation.inl.

◆ size_type

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::size_type = std::size_t

Type definition as required by C++ library standards.

Definition at line 761 of file allocation.inl.

◆ value_type

template<typename T, typename TAllocator>
using alib::lang::StdAllocator< T, TAllocator >::value_type = T

Type definition as required by C++ library standards.

Definition at line 760 of file allocation.inl.

Constructor(s) / Destructor Details:

◆ StdAllocator() [1/3]

template<typename T, typename TAllocator>
alib::lang::StdAllocator< T, TAllocator >::StdAllocator ( TAllocator & pAllocator)
inlineconstexprnoexcept

Constructor for the initial allocator instantiation.

Parameters
pAllocatorThe allocator.

Definition at line 777 of file allocation.inl.

◆ StdAllocator() [2/3]

template<typename T, typename TAllocator>
template<typename TSibling>
requires alib::lang::IsAllocator<typename TSibling::allocator>
alib::lang::StdAllocator< T, TAllocator >::StdAllocator ( const TSibling & origin)
inlineconstexprnoexcept

Copy constructor using an instance of a different template type.

Template Parameters
TSiblingThe originating allocator's type (StdAllocator<X>).
Parameters
originThe originating allocator of type TSibling .

Definition at line 792 of file allocation.inl.

◆ StdAllocator() [3/3]

template<typename T, typename TAllocator>
template<typename TSibling>
alib::lang::StdAllocator< T, TAllocator >::StdAllocator ( const StdAllocator< TSibling, TAllocator > & origin)
inline

Copy constructor using an instance of a different template type.

Template Parameters
TSiblingThe originating allocator's type (StdAllocator<X>).
Parameters
originThe originating allocator of type TSibling .

Definition at line 799 of file allocation.inl.

Method Details:

◆ allocate()

template<typename T, typename TAllocator>
T * alib::lang::StdAllocator< T, TAllocator >::allocate ( size_t n)
inlinenodiscardconstexpr

Passes the allocation request to the AllocatorMember.

Parameters
nThe number of requested objects to allocate storage for.
Returns
Pointer to the start of the array of n objects of type T.

Definition at line 813 of file allocation.inl.

◆ allocate_at_least()

template<typename T, typename TAllocator>
std::allocation_result< T *, std::size_t > alib::lang::StdAllocator< T, TAllocator >::allocate_at_least ( std::size_t n)
inlinenodiscardconstexpr

The C++23 extension of std::allocator finally supports ALib mechanics of using oversized allocation objects.

Parameters
nThe number of requested objects to allocate storage for.
Returns
The Pointer to the start of the allocated objects of type T as well as the number of objects allocated, which will be equal or higher than n.

Definition at line 823 of file allocation.inl.

◆ deallocate()

template<typename T, typename TAllocator>
void alib::lang::StdAllocator< T, TAllocator >::deallocate ( T * p,
std::size_t n )
inlineconstexpr

Frees the given array of objects.

Parameters
pPointer to the previously allocated memory.
nThe number of objects allocated.

Definition at line 837 of file allocation.inl.


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