QMCPACK
Mallocator< T, ALIGN > Struct Template Reference
+ Inheritance diagram for Mallocator< T, ALIGN >:
+ Collaboration diagram for Mallocator< T, ALIGN >:

Classes

struct  rebind
 

Public Types

using value_type = T
 
using size_type = size_t
 
using pointer = T *
 
using const_pointer = const T *
 

Public Member Functions

 Mallocator ()=default
 
template<class U >
 Mallocator (const Mallocator< U, ALIGN > &)
 
T * allocate (std::size_t n)
 
void deallocate (T *p, std::size_t n)
 

Static Public Attributes

static constexpr size_t alignment = ALIGN
 

Detailed Description

template<typename T, size_t ALIGN>
struct qmcplusplus::Mallocator< T, ALIGN >

Definition at line 25 of file Mallocator.hpp.


Class Documentation

◆ qmcplusplus::Mallocator::rebind

struct qmcplusplus::Mallocator::rebind

template<typename T, size_t ALIGN>
template<class U>
struct qmcplusplus::Mallocator< T, ALIGN >::rebind< U >

Definition at line 40 of file Mallocator.hpp.

+ Collaboration diagram for Mallocator< T, ALIGN >::rebind< U >:
Class Members
typedef Mallocator< U, ALIGN > other

Member Typedef Documentation

◆ const_pointer

using const_pointer = const T*

Definition at line 30 of file Mallocator.hpp.

◆ pointer

using pointer = T*

Definition at line 29 of file Mallocator.hpp.

◆ size_type

using size_type = size_t

Definition at line 28 of file Mallocator.hpp.

◆ value_type

using value_type = T

Definition at line 27 of file Mallocator.hpp.

Constructor & Destructor Documentation

◆ Mallocator() [1/2]

Mallocator ( )
default

◆ Mallocator() [2/2]

Mallocator ( const Mallocator< U, ALIGN > &  )
inline

Definition at line 36 of file Mallocator.hpp.

37  {}

Member Function Documentation

◆ allocate()

T* allocate ( std::size_t  n)
inline

Definition at line 45 of file Mallocator.hpp.

46  {
47  if (n == 0)
48  throw std::runtime_error("Mallocator::allocate does not accept size 0 allocations.");
49  void* pt(nullptr);
50  std::size_t asize = n * sizeof(T);
51  std::size_t amod = asize % ALIGN;
52  if (amod != 0)
53  asize += ALIGN - amod;
54 
55 #if __STDC_VERSION__ >= 201112L || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)
56  // as per C11 standard asize must be an integral multiple of ALIGN
57  // or behavior is undefined. Some implementations support all positive
58  // values of asize but the standard has not been amended
59  // This is also not guaranteed threadsafe until it appeared in
60  // the C++17 standard.
61  pt = aligned_alloc(ALIGN, asize);
62 #else
63  // While posix memalign can deal with asize violating the C11 standard
64  // assumptions made later by our simd code namely copyn require allocation
65  // of the entire aligned block to avoid heap buffer read overflows later
66  posix_memalign(&pt, ALIGN, asize);
67 #endif
68  if (pt == nullptr)
69  throw std::runtime_error("Allocation failed in Mallocator, requested size in bytes = " +
70  std::to_string(n * sizeof(T)));
71  return static_cast<T*>(pt);
72  }

◆ deallocate()

void deallocate ( T *  p,
std::size_t  n 
)
inline

Definition at line 74 of file Mallocator.hpp.

75  {
76  if (n == 0)
77  throw std::runtime_error("Mallocator::deallocate does not accept size 0 allocations.");
78  free(p);
79  }

Member Data Documentation

◆ alignment

constexpr size_t alignment = ALIGN
static

Definition at line 32 of file Mallocator.hpp.


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