Vc 1.4.5
SIMD Vector Classes for C++
 
Loading...
Searching...
No Matches
Allocator
1/* This file is part of the Vc library. {{{
2Copyright © 2014 Matthias Kretz <kretz@kde.org>
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are met:
6 * Redistributions of source code must retain the above copyright
7 notice, this list of conditions and the following disclaimer.
8 * Redistributions in binary form must reproduce the above copyright
9 notice, this list of conditions and the following disclaimer in the
10 documentation and/or other materials provided with the distribution.
11 * Neither the names of contributing organizations nor the
12 names of its contributors may be used to endorse or promote products
13 derived from this software without specific prior written permission.
14
15THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26}}}*/
27
28#ifndef VC_ALLOCATOR_H_
29#define VC_ALLOCATOR_H_
30
31#include <new>
32#include <cstddef>
33#include <cstdlib>
34#include <utility>
35
36#include "global.h"
37#include "common/macros.h"
38
49#ifdef Vc_MSVC
50#define Vc_DECLARE_ALLOCATOR(Type) \
51namespace std \
52{ \
53template <> class allocator<Type> : public ::Vc::Allocator<Type> \
54{ \
55public: \
56 template <typename U> struct rebind { \
57 typedef ::std::allocator<U> other; \
58 }; \
59 /* MSVC brokenness: the following function is optional - just doesn't compile \
60 * without it */ \
61 const allocator &select_on_container_copy_construction() const { return *this; } \
62}; \
63}
64#else
65#define Vc_DECLARE_ALLOCATOR(Type) \
66namespace std \
67{ \
68template <> class allocator<Type> : public ::Vc::Allocator<Type> \
69{ \
70public: \
71 template <typename U> struct rebind { \
72 typedef ::std::allocator<U> other; \
73 }; \
74}; \
75}
76#endif
77
78namespace Vc_VERSIONED_NAMESPACE
79{
80 using std::size_t;
81 using std::ptrdiff_t;
82
128 template<typename T> class Allocator
129 {
130 private:
131 enum Constants {
132#ifdef Vc_HAVE_STD_MAX_ALIGN_T
133 NaturalAlignment = alignof(std::max_align_t),
134#elif defined(Vc_HAVE_MAX_ALIGN_T)
135 NaturalAlignment = alignof(::max_align_t),
136#else
137 NaturalAlignment = sizeof(void *) > alignof(long double) ? sizeof(void *) :
138 (alignof(long double) > alignof(long long) ? alignof(long double) : alignof(long long)),
139#endif
140#if defined Vc_IMPL_AVX
141 SimdAlignment = 32,
142#elif defined Vc_IMPL_SSE
143 SimdAlignment = 16,
144#else
145 SimdAlignment = 1,
146#endif
147 Alignment = alignof(T) > SimdAlignment ? alignof(T) : SimdAlignment,
148 /* The number of extra bytes allocated must be large enough to put a pointer right
149 * before the adjusted address. This pointer stores the original address, which is
150 * required to call ::operator delete in deallocate.
151 *
152 * The address we get from ::operator new is a multiple of NaturalAlignment:
153 * p = N * NaturalAlignment
154 *
155 * Since all alignments are powers of two, Alignment is a multiple of NaturalAlignment:
156 * Alignment = k * NaturalAlignment
157 *
158 * two cases:
159 * 1. If p is already aligned to Alignment then allocate will return p + Alignment. In
160 * this case there are Alignment Bytes available to store a pointer.
161 * 2. If p is not aligned then p + (k - (N modulo k)) * NaturalAlignment will be
162 * returned. Since NaturalAlignment >= sizeof(void*) the pointer fits.
163 */
164 ExtraBytes = Alignment > NaturalAlignment ? Alignment : 0,
165 AlignmentMask = Alignment - 1
166 };
167 public:
168 typedef size_t size_type;
169 typedef ptrdiff_t difference_type;
170 typedef T* pointer;
171 typedef const T* const_pointer;
172 typedef T& reference;
173 typedef const T& const_reference;
174 typedef T value_type;
175
176 template<typename U> struct rebind { typedef Allocator<U> other; };
177
178 Allocator() throw() { }
179 Allocator(const Allocator&) throw() { }
180 template<typename U> Allocator(const Allocator<U>&) throw() { }
181
182 pointer address(reference x) const { return &x; }
183 const_pointer address(const_reference x) const { return &x; }
184
185 pointer allocate(size_type n, const void* = 0)
186 {
187 if (n > this->max_size()) {
188 throw std::bad_alloc();
189 }
190
191 char *p = static_cast<char *>(::operator new(n * sizeof(T) + ExtraBytes));
192 if (ExtraBytes > 0) {
193 char *const pp = p;
194 p += ExtraBytes;
195 const char *null = 0;
196 p -= ((p - null) & AlignmentMask); // equivalent to p &= ~AlignmentMask;
197 reinterpret_cast<char **>(p)[-1] = pp;
198 }
199 return reinterpret_cast<pointer>(p);
200 }
201
202 void deallocate(pointer p, size_type)
203 {
204 if (ExtraBytes > 0) {
205 p = reinterpret_cast<pointer *>(p)[-1];
206 }
207 ::operator delete(p);
208 }
209
210 size_type max_size() const throw() { return size_t(-1) / sizeof(T); }
211
212#ifdef Vc_MSVC
213 // MSVC brokenness: the following function is optional - just doesn't compile without it
214 const Allocator &select_on_container_copy_construction() const { return *this; }
215
216 // MSVC also requires a function that neither C++98 nor C++11 mention
217 // but it doesn't support variadic templates... otherwise the Vc_CXX11 clause would be nice
218 void construct(pointer p) { ::new(p) T(); }
219
220 // we still need the C++98 version:
221 void construct(pointer p, const T& val) { ::new(p) T(val); }
222 void destroy(pointer p) { p->~T(); }
223#else
224 template<typename U, typename... Args> void construct(U* p, Args&&... args)
225 {
226 ::new(p) U(std::forward<Args>(args)...);
227 }
228 template<typename U> void destroy(U* p) { p->~U(); }
229#endif
230 };
231
232 template<typename T> inline bool operator==(const Allocator<T>&, const Allocator<T>&) { return true; }
233 template<typename T> inline bool operator!=(const Allocator<T>&, const Allocator<T>&) { return false; }
234
235}
236
237#include "vector.h"
238namespace std
239{
240 template<typename T, typename Abi>
241 class allocator<Vc::Vector<T, Abi> > : public ::Vc::Allocator<Vc::Vector<T, Abi> >
242 {
243 public:
244 template<typename U> struct rebind { typedef ::std::allocator<U> other; };
245#ifdef Vc_MSVC
246 // MSVC brokenness: the following function is optional - just doesn't compile without it
247 const allocator &select_on_container_copy_construction() const { return *this; }
248#endif
249 };
250 template <typename T, typename Abi>
251 class allocator<Vc::Mask<T, Abi>> : public ::Vc::Allocator<Vc::Mask<T, Abi>>
252 {
253 public:
254 template<typename U> struct rebind { typedef ::std::allocator<U> other; };
255#ifdef Vc_MSVC
256 // MSVC brokenness: the following function is optional - just doesn't compile without it
257 const allocator &select_on_container_copy_construction() const { return *this; }
258#endif
259 };
260 template <typename T, std::size_t N, typename V, std::size_t M>
261 class allocator<Vc::SimdArray<T, N, V, M>> : public ::Vc::Allocator<Vc::SimdArray<T, N, V, M>>
262 {
263 public:
264 template<typename U> struct rebind { typedef ::std::allocator<U> other; };
265#ifdef Vc_MSVC
266 // MSVC brokenness: the following function is optional - just doesn't compile without it
267 const allocator &select_on_container_copy_construction() const { return *this; }
268#endif
269 };
270 template <typename T, std::size_t N, typename V, std::size_t M>
271 class allocator<Vc::SimdMaskArray<T, N, V, M>> : public ::Vc::Allocator<Vc::SimdMaskArray<T, N, V, M>>
272 {
273 public:
274 template<typename U> struct rebind { typedef ::std::allocator<U> other; };
275#ifdef Vc_MSVC
276 // MSVC brokenness: the following function is optional - just doesn't compile without it
277 const allocator &select_on_container_copy_construction() const { return *this; }
278#endif
279 };
280}
281
282#endif // VC_ALLOCATOR_H_
283
284// vim: ft=cpp et sw=4 sts=4
An allocator that uses global new and supports over-aligned types, as per [C++11 20....
Definition Allocator:129
Vector Classes Namespace.
Definition dox.h:585