Vc  1.3.2-dev
SIMD Vector Classes for C++
Allocator
1 /* This file is part of the Vc library. {{{
2 Copyright © 2014 Matthias Kretz <kretz@kde.org>
3 
4 Redistribution and use in source and binary forms, with or without
5 modification, 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 
15 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
19 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 ON 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
24 SOFTWARE, 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 
39 /**
40  * \ingroup Utilities
41  *
42  * Convenience macro to set the default allocator for a given \p Type to
43  * Vc::Allocator.
44  *
45  * \param Type Your type that you want to use with STL containers.
46  *
47  * \note You have to use this macro in the global namespace.
48  */
49 #ifdef Vc_MSVC
50 #define Vc_DECLARE_ALLOCATOR(Type) \
51 namespace std \
52 { \
53 template <> class allocator<Type> : public ::Vc::Allocator<Type> \
54 { \
55 public: \
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) \
66 namespace std \
67 { \
68 template <> class allocator<Type> : public ::Vc::Allocator<Type> \
69 { \
70 public: \
71  template <typename U> struct rebind { \
72  typedef ::std::allocator<U> other; \
73  }; \
74 }; \
75 }
76 #endif
77 
78 namespace Vc_VERSIONED_NAMESPACE
79 {
80  using std::size_t;
81  using std::ptrdiff_t;
82 
83  /**
84  * \headerfile Allocator <Vc/Allocator>
85  * An allocator that uses global new and supports over-aligned types, as per [C++11 20.6.9].
86  *
87  * Meant as a simple replacement for the allocator defined in the C++ Standard.
88  * Allocation is done using the global new/delete operators. But if the alignment property of \p
89  * T is larger than the size of a pointer, the allocate function allocates slightly more memory
90  * to adjust the pointer for correct alignment.
91  *
92  * If the \p T does not require over-alignment no additional memory will be allocated.
93  *
94  * \tparam T The type of objects to allocate.
95  *
96  * Example:
97  * \code
98  * struct Data {
99  * Vc::float_v x, y, z;
100  * };
101  *
102  * void fun()
103  * {
104  * std::vector<Data> dat0; // this will use std::allocator<Data>, which probably ignores the
105  * // alignment requirements for Data. Thus any access to dat0 may
106  * // crash your program.
107  *
108  * std::vector<Data, Vc::Allocator<Data> > dat1; // now std::vector will get correctly aligned
109  * // memory. Accesses to dat1 are safe.
110  * ...
111  * \endcode
112  *
113  * %Vc ships a macro to conveniently tell STL to use Vc::Allocator per default for a given type:
114  * \code
115  * struct Data {
116  * Vc::float_v x, y, z;
117  * };
118  * Vc_DECLARE_ALLOCATOR(Data)
119  *
120  * void fun()
121  * {
122  * std::vector<Data> dat0; // good now
123  * ...
124  * \endcode
125  *
126  * \ingroup Utilities
127  */
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 #ifdef Vc_IMPL_MIC
141  SimdAlignment = 64,
142 #elif defined Vc_IMPL_AVX
143  SimdAlignment = 32,
144 #elif defined Vc_IMPL_SSE
145  SimdAlignment = 16,
146 #else
147  SimdAlignment = 1,
148 #endif
149  Alignment = alignof(T) > SimdAlignment ? alignof(T) : SimdAlignment,
150  /* The number of extra bytes allocated must be large enough to put a pointer right
151  * before the adjusted address. This pointer stores the original address, which is
152  * required to call ::operator delete in deallocate.
153  *
154  * The address we get from ::operator new is a multiple of NaturalAlignment:
155  * p = N * NaturalAlignment
156  *
157  * Since all alignments are powers of two, Alignment is a multiple of NaturalAlignment:
158  * Alignment = k * NaturalAlignment
159  *
160  * two cases:
161  * 1. If p is already aligned to Alignment then allocate will return p + Alignment. In
162  * this case there are Alignment Bytes available to store a pointer.
163  * 2. If p is not aligned then p + (k - (N modulo k)) * NaturalAlignment will be
164  * returned. Since NaturalAlignment >= sizeof(void*) the pointer fits.
165  */
166  ExtraBytes = Alignment > NaturalAlignment ? Alignment : 0,
167  AlignmentMask = Alignment - 1
168  };
169  public:
170  typedef size_t size_type;
171  typedef ptrdiff_t difference_type;
172  typedef T* pointer;
173  typedef const T* const_pointer;
174  typedef T& reference;
175  typedef const T& const_reference;
176  typedef T value_type;
177 
178  template<typename U> struct rebind { typedef Allocator<U> other; };
179 
180  Allocator() throw() { }
181  Allocator(const Allocator&) throw() { }
182  template<typename U> Allocator(const Allocator<U>&) throw() { }
183 
184  pointer address(reference x) const { return &x; }
185  const_pointer address(const_reference x) const { return &x; }
186 
187  pointer allocate(size_type n, const void* = 0)
188  {
189  if (n > this->max_size()) {
190  throw std::bad_alloc();
191  }
192 
193  char *p = static_cast<char *>(::operator new(n * sizeof(T) + ExtraBytes));
194  if (ExtraBytes > 0) {
195  char *const pp = p;
196  p += ExtraBytes;
197  const char *null = 0;
198  p -= ((p - null) & AlignmentMask); // equivalent to p &= ~AlignmentMask;
199  reinterpret_cast<char **>(p)[-1] = pp;
200  }
201  return reinterpret_cast<pointer>(p);
202  }
203 
204  void deallocate(pointer p, size_type)
205  {
206  if (ExtraBytes > 0) {
207  p = reinterpret_cast<pointer *>(p)[-1];
208  }
209  ::operator delete(p);
210  }
211 
212  size_type max_size() const throw() { return size_t(-1) / sizeof(T); }
213 
214 #ifdef Vc_MSVC
215  // MSVC brokenness: the following function is optional - just doesn't compile without it
216  const Allocator &select_on_container_copy_construction() const { return *this; }
217 
218  // MSVC also requires a function that neither C++98 nor C++11 mention
219  // but it doesn't support variadic templates... otherwise the Vc_CXX11 clause would be nice
220  void construct(pointer p) { ::new(p) T(); }
221 
222  // we still need the C++98 version:
223  void construct(pointer p, const T& val) { ::new(p) T(val); }
224  void destroy(pointer p) { p->~T(); }
225 #else
226  template<typename U, typename... Args> void construct(U* p, Args&&... args)
227  {
228  ::new(p) U(std::forward<Args>(args)...);
229  }
230  template<typename U> void destroy(U* p) { p->~U(); }
231 #endif
232  };
233 
234  template<typename T> inline bool operator==(const Allocator<T>&, const Allocator<T>&) { return true; }
235  template<typename T> inline bool operator!=(const Allocator<T>&, const Allocator<T>&) { return false; }
236 
237 }
238 
239 #include "vector.h"
240 namespace std
241 {
242  template<typename T> class allocator<Vc::Vector<T> > : public ::Vc::Allocator<Vc::Vector<T> >
243  {
244  public:
245  template<typename U> struct rebind { typedef ::std::allocator<U> other; };
246 #ifdef Vc_MSVC
247  // MSVC brokenness: the following function is optional - just doesn't compile without it
248  const allocator &select_on_container_copy_construction() const { return *this; }
249 #endif
250  };
251  template <typename T>
252  class allocator<Vc::Mask<T>> : public ::Vc::Allocator<Vc::Mask<T>>
253  {
254  public:
255  template<typename U> struct rebind { typedef ::std::allocator<U> other; };
256 #ifdef Vc_MSVC
257  // MSVC brokenness: the following function is optional - just doesn't compile without it
258  const allocator &select_on_container_copy_construction() const { return *this; }
259 #endif
260  };
261  template <typename T, std::size_t N, typename V, std::size_t M>
262  class allocator<Vc::SimdArray<T, N, V, M>> : public ::Vc::Allocator<Vc::SimdArray<T, N, V, M>>
263  {
264  public:
265  template<typename U> struct rebind { typedef ::std::allocator<U> other; };
266 #ifdef Vc_MSVC
267  // MSVC brokenness: the following function is optional - just doesn't compile without it
268  const allocator &select_on_container_copy_construction() const { return *this; }
269 #endif
270  };
271  template <typename T, std::size_t N, typename V, std::size_t M>
272  class allocator<Vc::SimdMaskArray<T, N, V, M>> : public ::Vc::Allocator<Vc::SimdMaskArray<T, N, V, M>>
273  {
274  public:
275  template<typename U> struct rebind { typedef ::std::allocator<U> other; };
276 #ifdef Vc_MSVC
277  // MSVC brokenness: the following function is optional - just doesn't compile without it
278  const allocator &select_on_container_copy_construction() const { return *this; }
279 #endif
280  };
281 }
282 
283 #endif // VC_ALLOCATOR_H_
284 
285 // vim: ft=cpp et sw=4 sts=4