AutoPas  3.0.0
Loading...
Searching...
No Matches
AlignedAllocator.h
Go to the documentation of this file.
1
7#pragma once
8
9#include <cstdlib>
10#include <limits>
11#include <new>
12#include <utility>
13
14namespace autopas {
15
21constexpr unsigned int DEFAULT_CACHE_LINE_SIZE{64};
22
28template <class T, size_t Alignment = DEFAULT_CACHE_LINE_SIZE>
30 public:
31 // needed for compatibility with stl::allocator
33 using value_type = T;
35 using pointer = T *;
37 using const_pointer = const T *;
39 using reference = T &;
41 using const_reference = const T &;
43 using size_type = size_t;
44
51 template <class U>
52 struct rebind {
55 };
56
60 AlignedAllocator() = default;
61
65 template <class U>
67
71 ~AlignedAllocator() = default;
72
78 size_t max_size() const noexcept { return (std::numeric_limits<size_t>::max() - size_t(Alignment)) / sizeof(T); }
79
85 T *allocate(std::size_t n) {
86 if (n <= max_size()) {
87 size_t neededSize = sizeof(T) * n;
88 // sizeToRequest has to be a multiple of Alignment!
89 static_assert(Alignment > 0, "Alignment must be bigger than 0!");
90 // Rounds up to next multiple of Alignment.
91 size_t sizeToRequest = ((neededSize + Alignment - 1) / Alignment) * Alignment;
92 T *ptr = static_cast<T *>(aligned_alloc(Alignment, sizeToRequest));
93 if (ptr == nullptr) {
94 throw std::bad_alloc();
95 }
96 return ptr;
97 }
98 throw std::bad_alloc();
99 }
100
105 void deallocate(T *ptr, std::size_t /*n*/) { free(ptr); }
106
113 template <class U, class... Args>
114 void construct(U *p, Args &&...args) {
115 ::new ((void *)p) U(std::forward<Args>(args)...);
116 }
117
122 template <class U>
123 void destroy(U *p) {
124 p->~U();
125 }
126};
127
136template <typename T, size_t TAlignment, typename U, size_t UAlignment>
138 return TAlignment == UAlignment;
139}
140
151template <typename T, size_t TAlignment, typename U, size_t UAlignment>
153 return !(a == b);
154}
155
156} // namespace autopas
AlignedAllocator class.
Definition: AlignedAllocator.h:29
T * pointer
pointer type
Definition: AlignedAllocator.h:35
T * allocate(std::size_t n)
Allocate aligned memory for n objects of type T.
Definition: AlignedAllocator.h:85
T & reference
reference type
Definition: AlignedAllocator.h:39
size_t size_type
size type
Definition: AlignedAllocator.h:43
~AlignedAllocator()=default
Default destructor.
T value_type
value type
Definition: AlignedAllocator.h:33
const T & const_reference
const reference type
Definition: AlignedAllocator.h:41
void construct(U *p, Args &&...args)
Construct object of type U at already allocated memory, pointed to by p.
Definition: AlignedAllocator.h:114
const T * const_pointer
const pointer type
Definition: AlignedAllocator.h:37
size_t max_size() const noexcept
Returns maximum possible value of n, with which we can call allocate(n)
Definition: AlignedAllocator.h:78
void destroy(U *p)
Destroy object pointed to by p, but does not deallocate the memory.
Definition: AlignedAllocator.h:123
void deallocate(T *ptr, std::size_t)
Deallocate memory pointed to by ptr.
Definition: AlignedAllocator.h:105
AlignedAllocator(const AlignedAllocator< U, Alignment > &)
Copy constructor.
Definition: AlignedAllocator.h:66
AlignedAllocator()=default
Default empty constructor.
This is the main namespace of AutoPas.
Definition: AutoPasDecl.h:32
bool operator!=(const Configuration &lhs, const Configuration &rhs)
Not-Equals operator for Configuration objects.
Definition: Configuration.cpp:112
bool operator==(const Configuration &lhs, const Configuration &rhs)
Equals operator for Configuration objects.
Definition: Configuration.cpp:108
constexpr unsigned int DEFAULT_CACHE_LINE_SIZE
Default size for a cache line.
Definition: AlignedAllocator.h:21
Equivalent allocator for other types Class whose member other is an alias of allocator for type U.
Definition: AlignedAllocator.h:52