CookMem 1.4.0
A C++ memory context / allocator
Loading...
Searching...
No Matches
cookmem::Allocator< T, ContextProvider > Class Template Reference

#include <allocator.h>

Classes

struct  rebind
 

Public Types

typedef ContextProvider context_provider_type
 The type of the context provider.
 
typedef ContextProvider::context_type context_type
 The type of the memory context.
 
typedef T value_type
 The type of the allocated objects.
 
typedef T * pointer
 Pointer to an allocated object.
 
typedef const T * const_pointer
 Const pointer to an allocated object.
 
typedef T & reference
 Reference to an allocated object.
 
typedef const T & const_reference
 Const reference to an allocated object.
 
typedef std::size_t size_type
 The type of object counts.
 
typedef std::ptrdiff_t difference_type
 The type of pointer differences.
 
typedef ContextProvider::propagate_on_container_copy_assignment propagate_on_container_copy_assignment
 
typedef ContextProvider::propagate_on_container_move_assignment propagate_on_container_move_assignment
 
typedef ContextProvider::propagate_on_container_swap propagate_on_container_swap
 
typedef ContextProvider::is_always_equal is_always_equal
 

Public Member Functions

template<class Provider = ContextProvider>
 Allocator (typename std::enable_if< std::is_default_constructible< Provider >::value, int >::type *=nullptr)
 
 Allocator (context_type &context)
 
 Allocator (const ContextProvider &provider)
 
 Allocator (const Allocator &other)
 
template<class U >
 Allocator (const Allocator< U, ContextProvider > &other)
 
pointer allocate (size_type size, const void *=nullptr)
 
void deallocate (pointer ptr, size_type)
 
size_type max_size () const
 
template<class U , class... Args>
void construct (U *ptr, Args &&... args)
 
template<class U >
void destroy (U *ptr)
 
context_type & getContext () const
 
template<class U >
bool operator== (const Allocator< U, ContextProvider > &other) const
 
template<class U >
bool operator!= (const Allocator< U, ContextProvider > &other) const
 

Detailed Description

template<class T, class ContextProvider = MemContextProvider<CachedMemContext<> >>
class cookmem::Allocator< T, ContextProvider >

An STL allocator that allocates through its context provider.

Allocator copies and rebound allocators retain the same provider.

Types that require an alignment larger than ALLOCATION_ALIGNMENT are allocated with the allocateAligned () function of the context. Other types only need the context to provide allocate ().

Member Typedef Documentation

◆ is_always_equal

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
typedef ContextProvider::is_always_equal cookmem::Allocator< T, ContextProvider >::is_always_equal

Same as the context provider.

◆ propagate_on_container_copy_assignment

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
typedef ContextProvider::propagate_on_container_copy_assignment cookmem::Allocator< T, ContextProvider >::propagate_on_container_copy_assignment

Same as the context provider.

◆ propagate_on_container_move_assignment

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
typedef ContextProvider::propagate_on_container_move_assignment cookmem::Allocator< T, ContextProvider >::propagate_on_container_move_assignment

Same as the context provider.

◆ propagate_on_container_swap

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
typedef ContextProvider::propagate_on_container_swap cookmem::Allocator< T, ContextProvider >::propagate_on_container_swap

Same as the context provider.

Constructor & Destructor Documentation

◆ Allocator() [1/5]

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class Provider = ContextProvider>
cookmem::Allocator< T, ContextProvider >::Allocator ( typename std::enable_if< std::is_default_constructible< Provider >::value, int >::type *  = nullptr)
inline

Default constructor. It is available only when the context provider is default constructible, such as TLSContextProvider.

◆ Allocator() [2/5]

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
cookmem::Allocator< T, ContextProvider >::Allocator ( context_type &  context)
inlineexplicit

Constructor.

Parameters
contextthe memory context to allocate from. It must outlive every container that uses this allocator.

◆ Allocator() [3/5]

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
cookmem::Allocator< T, ContextProvider >::Allocator ( const ContextProvider &  provider)
inlineexplicit

Constructor.

Parameters
providerthe context provider to copy.

◆ Allocator() [4/5]

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
cookmem::Allocator< T, ContextProvider >::Allocator ( const Allocator< T, ContextProvider > &  other)
inline

Copy constructor. The copy uses the same memory context.

Parameters
otherthe allocator to copy.

◆ Allocator() [5/5]

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class U >
cookmem::Allocator< T, ContextProvider >::Allocator ( const Allocator< U, ContextProvider > &  other)
inline

Converting constructor from an allocator for a different object type. The new allocator uses the same memory context.

Parameters
otherthe allocator to copy.

Member Function Documentation

◆ allocate()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
pointer cookmem::Allocator< T, ContextProvider >::allocate ( size_type  size,
const void *  = nullptr 
)
inline

Allocate memory for a number of objects. The objects are not constructed.

Parameters
sizethe number of objects.
Returns
the allocated memory, or nullptr if size is 0.
Exceptions
std::bad_allocif the memory cannot be allocated.

◆ construct()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class U , class... Args>
void cookmem::Allocator< T, ContextProvider >::construct ( U *  ptr,
Args &&...  args 
)
inline

Construct an object in allocated memory.

Parameters
ptrwhere to construct the object.
argsthe constructor arguments.

◆ deallocate()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
void cookmem::Allocator< T, ContextProvider >::deallocate ( pointer  ptr,
size_type   
)
inline

Deallocate memory obtained from allocate (). The objects must have been destroyed already.

Parameters
ptrthe memory to deallocate.

◆ destroy()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class U >
void cookmem::Allocator< T, ContextProvider >::destroy ( U *  ptr)
inline

Destroy an object without deallocating its memory.

Parameters
ptrthe object to destroy.

◆ getContext()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
context_type & cookmem::Allocator< T, ContextProvider >::getContext ( ) const
inline

Get the memory context.

Returns
the memory context this allocator allocates from.

◆ max_size()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
size_type cookmem::Allocator< T, ContextProvider >::max_size ( ) const
inline

Get the largest number of objects that could be requested.

Returns
the largest number of objects that allocate () accepts.

◆ operator!=()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class U >
bool cookmem::Allocator< T, ContextProvider >::operator!= ( const Allocator< U, ContextProvider > &  other) const
inline

Check if two allocators use different memory contexts.

Parameters
otherthe allocator to compare with.
Returns
true if the allocators use different memory contexts.

◆ operator==()

template<class T , class ContextProvider = MemContextProvider<CachedMemContext<> >>
template<class U >
bool cookmem::Allocator< T, ContextProvider >::operator== ( const Allocator< U, ContextProvider > &  other) const
inline

Check if memory allocated by one allocator can be deallocated by the other.

Parameters
otherthe allocator to compare with.
Returns
true if both allocators use the same memory context.

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