Gromacs
2018.8
|
#include <gromacs/utility/allocator.h>
Inherits AllocationPolicy.
Policy-based memory allocator.
T | Type of objects to allocate |
AllocationPolicy | Policy 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.
std::bad_alloc | Instead 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... | |
|
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.
|
default |
Constructor.
No constructor can be auto-generated in the presence of any user-defined constructor, but we want the default constructor.
|
inline |
Constructor to accept an AllocationPolicy.
This is useful for AllocationPolicies with state.
|
inline |
Return address of an object.
r | Reference to object of type T |
|
inline |
Return address of a const object.
r | Const reference to object of type T |
|
inline |
Do the actual memory allocation.
n | Number 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. |
hint | Optional value returned from previous call to allocate. For now this is not used. |
std::bad_alloc | if the allocation fails. |
|
inline |
Construct an object without allocating memory.
Args | Variable-length list of types for constructor args |
p | Adress of memory where to construct object |
args | Variable-length list of arguments to constructor |
|
inline |
Release memory.
p | Pointer to previously allocated memory returned from allocate() |
n | number of objects previously passed to allocate() |
|
inline |
Call the destructor of object without releasing memory.
p | Address of memory where to destroy object |
|
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.
|
inline |
Return true if two allocators are different.
rhs | Other allocator. |
This is a member function of the left-hand-side allocator.
|
inline |
Return true if two allocators are identical.
This is a member function of the left-hand-side allocator.