AlignedAllocator.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_ALIGNEDALLOCATOR_H_
36 #define _BLAZE_UTIL_ALIGNEDALLOCATOR_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #include <blaze/util/Memory.h>
45 #include <blaze/util/Unused.h>
46 
47 
48 namespace blaze {
49 
50 //=================================================================================================
51 //
52 // CLASS DEFINITION
53 //
54 //=================================================================================================
55 
56 //*************************************************************************************************
68 template< typename Type >
70 {
71  public:
72  //**Type definitions****************************************************************************
73  using ValueType = Type;
74  using Pointer = Type*;
75  using ConstPointer = const Type*;
76  using Reference = Type&;
77  using ConstReference = const Type&;
78  using SizeType = std::size_t;
79  using DifferenceType = std::ptrdiff_t;
80 
81  // STL allocator requirements
83  using pointer = Pointer;
85  using reference = Reference;
87  using size_type = SizeType;
89  //**********************************************************************************************
90 
91  //**rebind class definition*********************************************************************
94  template< typename Type2 >
95  struct rebind
96  {
98  };
99  //**********************************************************************************************
100 
101  //**Constructors********************************************************************************
104  explicit inline AlignedAllocator();
105 
106  template< typename Type2 >
107  inline AlignedAllocator( const AlignedAllocator<Type2>& );
109  //**********************************************************************************************
110 
111  //**Utility functions***************************************************************************
114  inline constexpr size_t max_size() const noexcept;
115  inline Pointer address( Reference x ) const noexcept;
116  inline ConstPointer address( ConstReference x ) const noexcept;
118  //**********************************************************************************************
119 
120  //**Allocation functions************************************************************************
123  inline Pointer allocate ( size_t numObjects, const void* localityHint = nullptr );
124  inline void deallocate( Pointer ptr, size_t numObjects ) noexcept;
126  //**********************************************************************************************
127 
128  //**Construction functions**********************************************************************
131  template< typename... Args >
132  inline void construct( Pointer ptr, Args&&... args );
133 
134  inline void destroy( Pointer ptr ) noexcept;
136  //**********************************************************************************************
137 };
138 //*************************************************************************************************
139 
140 
141 
142 
143 //=================================================================================================
144 //
145 // CONSTRUCTOR
146 //
147 //=================================================================================================
148 
149 //*************************************************************************************************
152 template< typename Type >
154 {}
155 //*************************************************************************************************
156 
157 
158 //*************************************************************************************************
163 template< typename Type >
164 template< typename Type2 >
166 {
167  UNUSED_PARAMETER( allocator );
168 }
169 //*************************************************************************************************
170 
171 
172 
173 
174 //=================================================================================================
175 //
176 // UTILITY FUNCTIONS
177 //
178 //=================================================================================================
179 
180 //*************************************************************************************************
185 template< typename Type >
186 inline constexpr size_t AlignedAllocator<Type>::max_size() const noexcept
187 {
188  return size_t(-1) / sizeof( Type );
189 }
190 //*************************************************************************************************
191 
192 
193 //*************************************************************************************************
198 template< typename Type >
199 inline typename AlignedAllocator<Type>::Pointer
201 {
202  return &x;
203 }
204 //*************************************************************************************************
205 
206 
207 //*************************************************************************************************
212 template< typename Type >
215 {
216  return &x;
217 }
218 //*************************************************************************************************
219 
220 
221 
222 
223 //=================================================================================================
224 //
225 // ALLOCATION FUNCTIONS
226 //
227 //=================================================================================================
228 
229 //*************************************************************************************************
242 template< typename Type >
243 inline typename AlignedAllocator<Type>::Pointer
244  AlignedAllocator<Type>::allocate( size_t numObjects, const void* localityHint )
245 {
246  UNUSED_PARAMETER( localityHint );
247 
248  const size_t alignment( AlignmentOf<Type>::value );
249 
250  if( alignment >= 8UL ) {
251  return reinterpret_cast<Type*>( allocate_backend( numObjects*sizeof(Type), alignment ) );
252  }
253  else {
254  return static_cast<Pointer>( operator new[]( numObjects * sizeof( Type ) ) );
255  }
256 }
257 //*************************************************************************************************
258 
259 
260 //*************************************************************************************************
271 template< typename Type >
272 inline void AlignedAllocator<Type>::deallocate( Pointer ptr, size_t numObjects ) noexcept
273 {
274  UNUSED_PARAMETER( numObjects );
275 
276  if( ptr == nullptr )
277  return;
278 
279  const size_t alignment( AlignmentOf<Type>::value );
280 
281  if( alignment >= 8UL ) {
282  deallocate_backend( ptr );
283  }
284  else {
285  operator delete[]( ptr );
286  }
287 }
288 //*************************************************************************************************
289 
290 
291 
292 
293 //=================================================================================================
294 //
295 // CONSTRUCTION FUNCTIONS
296 //
297 //=================================================================================================
298 
299 //*************************************************************************************************
309 template< typename Type >
310 template< typename... Args >
311 inline void AlignedAllocator<Type>::construct( Pointer ptr, Args&&... args )
312 {
313  ::new( ptr ) Type( std::forward<Args>( args )... );
314 }
315 //*************************************************************************************************
316 
317 
318 //*************************************************************************************************
327 template< typename Type >
328 inline void AlignedAllocator<Type>::destroy( Pointer ptr ) noexcept
329 {
330  ptr->~Type();
331 }
332 //*************************************************************************************************
333 
334 
335 
336 
337 //=================================================================================================
338 //
339 // GLOBAL OPERATORS
340 //
341 //=================================================================================================
342 
343 //*************************************************************************************************
346 template< typename T1, typename T2 >
347 inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept;
348 
349 template< typename T1, typename T2 >
350 inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept;
352 //*************************************************************************************************
353 
354 
355 //*************************************************************************************************
362 template< typename T1 // Type of the left-hand side aligned allocator
363  , typename T2 > // Type of the right-hand side aligned allocator
364 inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept
365 {
366  UNUSED_PARAMETER( lhs, rhs );
367  return true;
368 }
369 //*************************************************************************************************
370 
371 
372 //*************************************************************************************************
379 template< typename T1 // Type of the left-hand side aligned allocator
380  , typename T2 > // Type of the right-hand side aligned allocator
381 inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept
382 {
383  UNUSED_PARAMETER( lhs, rhs );
384  return false;
385 }
386 //*************************************************************************************************
387 
388 } // namespace blaze
389 
390 #endif
constexpr size_t max_size() const noexcept
Returns the maximum possible number of elements that can be allocated together.
Definition: AlignedAllocator.h:186
Header file for the AlignmentOf type trait.
Header file for the UNUSED_PARAMETER function template.
ValueType value_type
Type of the allocated values.
Definition: AlignedAllocator.h:82
Header file for memory allocation and deallocation functionality.
void destroy(Pointer ptr) noexcept
Destroys the object of type Type at the specified memory location.
Definition: AlignedAllocator.h:328
Pointer address(Reference x) const noexcept
Returns the address of the given element.
Definition: AlignedAllocator.h:200
DifferenceType difference_type
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:88
ConstPointer const_pointer
Type of a pointer-to-const to the allocated values.
Definition: AlignedAllocator.h:84
Pointer pointer
Type of a pointer to the allocated values.
Definition: AlignedAllocator.h:83
Namespace of the Blaze C++ math library.
Definition: Blaze.h:58
Pointer allocate(size_t numObjects, const void *localityHint=nullptr)
Allocates aligned memory for the specified number of objects.
Definition: AlignedAllocator.h:244
SizeType size_type
Size type of the aligned allocator.
Definition: AlignedAllocator.h:87
Type * Pointer
Type of a pointer to the allocated values.
Definition: AlignedAllocator.h:74
constexpr bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:250
void construct(Pointer ptr, Args &&... args)
Constructs an object of type Type at the specified memory location.
Definition: AlignedAllocator.h:311
constexpr bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:290
Allocator for type-specific aligned memory.The AlignedAllocator class template represents an implemen...
Definition: AlignedAllocator.h:69
void deallocate(Pointer ptr, size_t numObjects) noexcept
Deallocation of memory.
Definition: AlignedAllocator.h:272
const Type * ConstPointer
Type of a pointer-to-const to the allocated values.
Definition: AlignedAllocator.h:75
Type ValueType
Type of the allocated values.
Definition: AlignedAllocator.h:73
AlignedAllocator()
The default constructor for AlignedAllocator.
Definition: AlignedAllocator.h:153
Reference reference
Type of a reference to the allocated values.
Definition: AlignedAllocator.h:85
Type & Reference
Type of a reference to the allocated values.
Definition: AlignedAllocator.h:76
std::ptrdiff_t DifferenceType
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:79
std::size_t SizeType
Size type of the aligned allocator.
Definition: AlignedAllocator.h:78
const Type & ConstReference
Type of a reference-to-const to the allocated values.
Definition: AlignedAllocator.h:77
void UNUSED_PARAMETER(const Args &...)
Suppression of unused parameter warnings.
Definition: Unused.h:81
ConstReference const_reference
Type of a reference-to-const to the allocated values.
Definition: AlignedAllocator.h:86
Size type of the Blaze library.
Evaluation of the required alignment of the given data type.The AlignmentOf type trait template evalu...
Definition: AlignmentOf.h:219
Implementation of the AlignedAllocator rebind mechanism.
Definition: AlignedAllocator.h:95