10#ifndef ALIGNEDALLOCATOR_H
11#define ALIGNEDALLOCATOR_H
21#define CACHE_LINE_SIZE 64
30template<
typename T,
size_t Alignment = CACHE_LINE_SIZE>
35 typedef const T* const_pointer;
37 typedef const T& const_reference;
38 typedef size_t size_type;
63 return (std::numeric_limits < size_t > ::max() -
size_t(Alignment)) /
sizeof(T);
73 T* ptr =
static_cast<T*
>(malloc(
sizeof(T) * n));
74#elif defined(__SSE3__) && !defined(__PGI)
75 T* ptr =
static_cast<T*
>(_mm_malloc(
sizeof(T) * n, Alignment));
77 T* ptr =
static_cast<T*
>(memalign(Alignment,
sizeof(T) * n));
82 throw std::bad_alloc();
86 throw std::bad_alloc();
93#if defined(__SSE3__) && !defined(__PGI)
103 template<
class U,
class ...Args>
105 ::new ((
void*) p) U(std::forward<Args>(args)...);
117template<
typename T,
size_t TAlignment,
typename U,
size_t UAlignment>
120 return TAlignment == UAlignment;
123template<
typename T,
size_t TAlignment,
typename U,
size_t UAlignment>
Definition: AlignedAllocator.h:43
A custom allocator to get aligned memory.
Definition: AlignedAllocator.h:31
AlignedAllocator()=default
Default constructor.
size_t max_size() const noexcept
Returns maximum possible value of n, with which we can call allocate(n)
Definition: AlignedAllocator.h:62
AlignedAllocator(const AlignedAllocator< U, Alignment > &)
Copy constructor.
Definition: AlignedAllocator.h:56
void construct(U *p, Args &&... args)
Construct object of type U at already allocated memory, pointed to by p.
Definition: AlignedAllocator.h:104
void destroy(U *p)
Destroy object pointed to by p, but does not deallocate the memory.
Definition: AlignedAllocator.h:112
T * allocate(std::size_t n)
Allocate aligned memory for n objects of type T.
Definition: AlignedAllocator.h:70
void deallocate(T *ptr, std::size_t)
Deallocate memory pointed to by ptr.
Definition: AlignedAllocator.h:92