Gromacs  2018.8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
List of all members | Classes | Public Types | Public Member Functions
gmx::Allocator< T, AllocationPolicy > Class Template Reference

#include <gromacs/utility/allocator.h>

Inherits AllocationPolicy.

Description

template<class T, typename AllocationPolicy>
class gmx::Allocator< T, AllocationPolicy >

Policy-based memory allocator.

Template Parameters
TType of objects to allocate
AllocationPolicyPolicy of (matching) allocation and deallocation functions.

This class can be used for the optional allocator template parameter in standard library containers. It must be configured with both the type of object to allocate, and an AllocationPolicy which effectively wraps a matching pair of malloc and free functions. This permits implementing a family of related allocators e.g. with SIMD alignment, GPU host-side page locking, or perhaps both, in a way that preserves a common programming interface and duplicates minimal code.

AllocationPolicy is used as a base class, so that if AllocationPolicy is stateless, then the empty base optimization will ensure that Allocation is also stateless, and objects made with the Allocator will incur no size penalty. (Embedding an AllocationPolicy object incurs a size penalty always, even if the object is empty.) Normally a stateless allocator will be used.

However, an AllocationPolicy with state might be desirable for simplifying writing code that needs to allocate suitably for a transfer to a GPU. That code needs to specify an Allocator that can do the right job, which can be stateless. However, if we have code that will not know until run time whether a GPU transfer will occur, then the allocator needs to be aware of the state. That will increase the size of a container that uses the stateful allocator.

Exceptions
std::bad_allocInstead of a GROMACS exception object, we throw the standard one on allocation failures to make it as compatible as possible with the errors expected by code using the standard library containers.

Classes

struct  rebind
 Standard-required typedef to use allocator with different class. More...
 

Public Types

typedef T value_type
 Type of allocated elements.
 
typedef T & reference
 Reference to allocated elements.
 
typedef const T & const_reference
 Constant reference to allocated elements.
 
typedef T * pointer
 Pointer to allocated elements.
 
typedef const T * const_pointer
 Constant pointer to allocated elements.
 
typedef std::size_t size_type
 Integer type to use for size of objects.
 
typedef std::ptrdiff_t difference_type
 Type to hold differences between pointers.
 
typedef AllocationPolicy allocation_policy
 Type of the AllocationPolicy.
 

Public Member Functions

template<class U >
 Allocator (const Allocator< U, AllocationPolicy > &)
 Templated copy constructor. More...
 
 Allocator ()=default
 Constructor. More...
 
 Allocator (const AllocationPolicy &p)
 Constructor to accept an AllocationPolicy. More...
 
pointer address (reference r) const
 Return address of an object. More...
 
const_pointer address (const_reference r) const
 Return address of a const object. More...
 
pointer allocate (std::size_t n, typename std::allocator< void >::const_pointer hint=nullptr)
 Do the actual memory allocation. More...
 
void deallocate (pointer p, std::size_t n)
 Release memory. More...
 
AllocationPolicy getPolicy () const
 Return the policy object for this allocator.
 
template<class... Args>
void construct (pointer p, Args &&...args)
 Construct an object without allocating memory. More...
 
void destroy (pointer p)
 Call the destructor of object without releasing memory. More...
 
std::size_t max_size () const
 Return largest number of objects that can be allocated. More...
 
template<class T2 >
bool operator== (const Allocator< T2, AllocationPolicy > &) const
 Return true if two allocators are identical. More...
 
bool operator!= (const Allocator &rhs) const
 Return true if two allocators are different. More...
 

Constructor & Destructor Documentation

template<class T , typename AllocationPolicy >
template<class U >
gmx::Allocator< T, AllocationPolicy >::Allocator ( const Allocator< U, AllocationPolicy > &  )
inlineexplicit

Templated copy constructor.

This template constructor cannot be auto-generated, and is normally unused, except e.g. MSVC2015 standard library uses it in debug mode, presumably to implement some checks.

template<class T , typename AllocationPolicy >
gmx::Allocator< T, AllocationPolicy >::Allocator ( )
default

Constructor.

No constructor can be auto-generated in the presence of any user-defined constructor, but we want the default constructor.

template<class T , typename AllocationPolicy >
gmx::Allocator< T, AllocationPolicy >::Allocator ( const AllocationPolicy &  p)
inline

Constructor to accept an AllocationPolicy.

This is useful for AllocationPolicies with state.

Member Function Documentation

template<class T , typename AllocationPolicy >
pointer gmx::Allocator< T, AllocationPolicy >::address ( reference  r) const
inline

Return address of an object.

Parameters
rReference to object of type T
Returns
Pointer to T memory
template<class T , typename AllocationPolicy >
const_pointer gmx::Allocator< T, AllocationPolicy >::address ( const_reference  r) const
inline

Return address of a const object.

Parameters
rConst reference to object of type T
Returns
Pointer to T memory
template<class T , typename AllocationPolicy >
pointer gmx::Allocator< T, AllocationPolicy >::allocate ( std::size_t  n,
typename std::allocator< void >::const_pointer  hint = nullptr 
)
inline

Do the actual memory allocation.

Parameters
nNumber of elements of type T to allocate. n can be 0 bytes, which will return a non-null properly aligned and padded pointer that should not be used.
hintOptional value returned from previous call to allocate. For now this is not used.
Returns
Pointer to allocated memory
Exceptions
std::bad_allocif the allocation fails.
template<class T , typename AllocationPolicy >
template<class... Args>
void gmx::Allocator< T, AllocationPolicy >::construct ( pointer  p,
Args &&...  args 
)
inline

Construct an object without allocating memory.

Template Parameters
ArgsVariable-length list of types for constructor args
Parameters
pAdress of memory where to construct object
argsVariable-length list of arguments to constructor
template<class T , typename AllocationPolicy >
void gmx::Allocator< T, AllocationPolicy >::deallocate ( pointer  p,
std::size_t  n 
)
inline

Release memory.

Parameters
pPointer to previously allocated memory returned from allocate()
nnumber of objects previously passed to allocate()
template<class T , typename AllocationPolicy >
void gmx::Allocator< T, AllocationPolicy >::destroy ( pointer  p)
inline

Call the destructor of object without releasing memory.

Parameters
pAddress of memory where to destroy object
template<class T , typename AllocationPolicy >
std::size_t gmx::Allocator< T, AllocationPolicy >::max_size ( ) const
inline

Return largest number of objects that can be allocated.

This will be set such that the number of objects T multiplied by the size of each object is the largest value that can be represented by size_type.

template<class T , typename AllocationPolicy >
bool gmx::Allocator< T, AllocationPolicy >::operator!= ( const Allocator< T, AllocationPolicy > &  rhs) const
inline

Return true if two allocators are different.

Parameters
rhsOther allocator.

This is a member function of the left-hand-side allocator.

template<class T , typename AllocationPolicy >
template<class T2 >
bool gmx::Allocator< T, AllocationPolicy >::operator== ( const Allocator< T2, AllocationPolicy > &  ) const
inline

Return true if two allocators are identical.

This is a member function of the left-hand-side allocator.


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