My Project
Loading...
Searching...
No Matches
alignedallocator.hh
Go to the documentation of this file.
1// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2// vi: set et ts=4 sw=4 sts=4:
21#ifndef EWOMS_ALIGNED_ALLOCATOR_HH
22#define EWOMS_ALIGNED_ALLOCATOR_HH
23
24#include <utility>
25#include <memory>
26#include <type_traits>
27#include <cassert>
28
29namespace Opm {
30
31namespace detail {
32constexpr inline bool is_alignment(std::size_t value) noexcept
33{
34 return (value > 0) && ((value & (value - 1)) == 0);
35}
36
37template<std::size_t N>
39 : std::integral_constant<bool, (N > 0) && ((N & (N - 1)) == 0)>
40{};
41
42template<std::size_t A, std::size_t B>
44 : std::integral_constant<std::size_t, (A < B) ? A : B>
45{ };
46
47template<class T>
48struct offset_object
49{
50 char offset;
51 T object;
52};
53
54template<class T>
55struct alignment_of
56 : min_size<sizeof(T), sizeof(offset_object<T>) - sizeof(T)>::type
57{};
58
59template<std::size_t A, std::size_t B>
60struct max_align
61 : std::integral_constant<std::size_t,(A > B) ? A : B>
62{};
63
64template<class T>
66 : std::integral_constant<std::size_t, ~static_cast<std::size_t>(0) / sizeof(T)>
67{};
68
69using std::addressof;
70}
71
72inline void* aligned_alloc(std::size_t alignment,
73 std::size_t size) noexcept
74{
75 assert(detail::is_alignment(alignment));
76 if (alignment < sizeof(void*)) {
77 alignment = sizeof(void*);
78 }
79 void* p;
80 if (::posix_memalign(&p, alignment, size) != 0) {
81 p = 0;
82 }
83 return p;
84}
85
86inline void aligned_free(void* ptr)
87 noexcept
88{
89 ::free(ptr);
90}
91
92
93template<class T, std::size_t Alignment>
95 static_assert(detail::is_alignment_constant<Alignment>::value, "Alignment must be powers of two!");
96
97public:
98 using value_type = T;
99 using pointer = T*;
100 using const_pointer = const T*;
101 using void_pointer = void*;
102 using const_void_pointer = const void*;
103 using size_type = std::size_t;
104 using difference_type = std::ptrdiff_t;
105 using reference = T&;
106 using const_reference = const T&;
107
108private:
110
111public:
112 template<class U>
113 struct rebind {
115 };
116
118 noexcept = default;
119
120 template<class U>
122 Alignment>&) noexcept {
123 }
124
125 pointer address(reference value) const
126 noexcept {
127 return detail::addressof(value);
128 }
129
130 const_pointer address(const_reference value) const
131 noexcept {
132 return detail::addressof(value);
133 }
134
135 pointer allocate(size_type size,
136 const_void_pointer = 0) {
137 void* p = aligned_alloc(MaxAlign::value,
138 sizeof(T) * size);
139 if (!p && size > 0) {
140 throw std::bad_alloc();
141 }
142 return static_cast<T*>(p);
143 }
144
145 void deallocate(pointer ptr, size_type) {
146 aligned_free(ptr);
147 }
148
149 constexpr size_type max_size() const
150 noexcept {
151 return detail::max_count_of<T>::value;
152 }
153
154 template<class U, class... Args>
155 void construct(U* ptr, Args&&... args) {
156 void* p = ptr;
157 ::new(p) U(std::forward<Args>(args)...);
158 }
159
160 template<class U>
161 void construct(U* ptr) {
162 void* p = ptr;
163 ::new(p) U();
164 }
165
166 template<class U>
167 void destroy(U* ptr) {
168 (void)ptr;
169 ptr->~U();
170 }
171};
172
173template<std::size_t Alignment>
174class aligned_allocator<void, Alignment> {
176 "The specified alignment is not a power of two!");
177
178public:
179 using value_type = void;
180 using pointer = void*;
181 using const_pointer = const void*;
182
183 template<class U>
184 struct rebind {
186 };
187};
188
189template<class T1, class T2, std::size_t Alignment>
190inline bool operator==(const aligned_allocator<T1,
191 Alignment>&, const aligned_allocator<T2,
192 Alignment>&) noexcept
193{
194 return true;
195}
196
197template<class T1, class T2, std::size_t Alignment>
198inline bool operator!=(const aligned_allocator<T1,
199 Alignment>&, const aligned_allocator<T2,
200 Alignment>&) noexcept
201{
202 return false;
203}
204}
205
206#endif
Definition alignedallocator.hh:94
This file contains a set of helper functions used by VFPProd / VFPInj.
Definition blackoilboundaryratevector.hh:37
Definition alignedallocator.hh:113
Definition alignedallocator.hh:184
Definition alignedallocator.hh:40
Definition alignedallocator.hh:62
Definition alignedallocator.hh:67
Definition alignedallocator.hh:45