41 #ifndef EWOMS_ALIGNED_ALLOCATOR_HH
42 #define EWOMS_ALIGNED_ALLOCATOR_HH
46 #include <type_traits>
52 constexpr
inline bool is_alignment(std::size_t value) noexcept
54 return (value > 0) && ((value & (value - 1)) == 0);
57 template<std::
size_t N>
59 : std::integral_constant<bool, (N > 0) && ((N & (N - 1)) == 0)>
62 template<std::
size_t A, std::
size_t B>
64 : std::integral_constant<std::size_t, (A < B) ? A : B>
76 : min_size<sizeof(T), sizeof(offset_object<T>) - sizeof(T)>::type
79 template<std::size_t A, std::size_t B>
81 : std::integral_constant<std::size_t,(A > B) ? A : B>
86 : std::integral_constant<std::size_t, ~static_cast<std::size_t>(0) / sizeof(T)>
92 inline void* aligned_alloc(std::size_t alignment,
93 std::size_t size) noexcept
95 assert(detail::is_alignment(alignment));
96 if (alignment <
sizeof(
void*)) {
97 alignment =
sizeof(
void*);
100 if (::posix_memalign(&p, alignment, size) != 0) {
106 inline void aligned_free(
void* ptr)
113 template<
class T, std::
size_t Alignment>
118 typedef T value_type;
120 typedef const T* const_pointer;
121 typedef void* void_pointer;
122 typedef const void* const_void_pointer;
123 typedef std::size_t size_type;
124 typedef std::ptrdiff_t difference_type;
125 typedef T& reference;
126 typedef const T& const_reference;
142 Alignment>&) noexcept {
145 pointer address(reference value)
const
147 return detail::addressof(value);
150 const_pointer address(const_reference value)
const
152 return detail::addressof(value);
155 pointer allocate(size_type size,
156 const_void_pointer = 0) {
157 void* p = aligned_alloc(MaxAlign::value,
159 if (!p && size > 0) {
160 throw std::bad_alloc();
162 return static_cast<T*
>(p);
165 void deallocate(pointer ptr, size_type) {
169 constexpr size_type max_size() const
171 return detail::max_count_of<T>::value;
174 template<
class U,
class... Args>
175 void construct(U* ptr, Args&&... args) {
177 ::new(p) U(std::forward<Args>(args)...);
181 void construct(U* ptr) {
187 void destroy(U* ptr) {
193 template<std::
size_t Alignment>
196 "The specified alignment is not a power of two!");
199 typedef void value_type;
200 typedef void* pointer;
201 typedef const void* const_pointer;
209 template<
class T1,
class T2, std::
size_t Alignment>
212 Alignment>&) noexcept
217 template<
class T1,
class T2, std::
size_t Alignment>
218 inline bool operator!=(
const aligned_allocator<T1,
219 Alignment>&,
const aligned_allocator<T2,
220 Alignment>&) noexcept
Definition: alignedallocator.hh:58
Definition: alignedallocator.hh:63
Definition: alignedallocator.hh:133
Definition: alignedallocator.hh:114
Definition: alignedallocator.hh:80
Definition: alignedallocator.hh:85