AlignedAllocator.h
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>
44 #include <blaze/util/Null.h>
46 #include <blaze/util/Unused.h>
47 
48 
49 namespace blaze {
50 
51 //=================================================================================================
52 //
53 // CLASS DEFINITION
54 //
55 //=================================================================================================
56 
57 //*************************************************************************************************
69 template< typename Type >
71 {
72  public:
73  //**Type definitions****************************************************************************
74  typedef Type ValueType;
75  typedef Type* Pointer;
76  typedef const Type* ConstPointer;
77  typedef Type& Reference;
78  typedef const Type& ConstReference;
79  typedef std::size_t SizeType;
80  typedef std::ptrdiff_t DifferenceType;
81 
82  // STL allocator requirements
83  typedef ValueType value_type;
84  typedef Pointer pointer;
85  typedef ConstPointer const_pointer;
86  typedef Reference reference;
87  typedef ConstReference const_reference;
88  typedef SizeType size_type;
89  typedef DifferenceType difference_type;
90  //**********************************************************************************************
91 
92  //**rebind class definition*********************************************************************
95  template< typename Type2 >
96  struct rebind
97  {
99  };
100  //**********************************************************************************************
101 
102  //**Constructors********************************************************************************
105  explicit inline AlignedAllocator();
106 
107  template< typename Type2 >
108  inline AlignedAllocator( const AlignedAllocator<Type2>& );
110  //**********************************************************************************************
111 
112  //**Utility functions***************************************************************************
115  inline size_t max_size() const;
116  inline Pointer address( Reference x ) const;
117  inline ConstPointer address( ConstReference x ) const;
119  //**********************************************************************************************
120 
121  //**Allocation functions************************************************************************
124  inline Pointer allocate ( size_t numObjects, const void* localityHint = NULL );
125  inline void deallocate( Pointer ptr, size_t numObjects );
127  //**********************************************************************************************
128 
129  //**Construction functions**********************************************************************
132  inline void construct( Pointer ptr, const Type& value );
133  inline void destroy ( Pointer ptr );
135  //**********************************************************************************************
136 };
137 //*************************************************************************************************
138 
139 
140 
141 
142 //=================================================================================================
143 //
144 // CONSTRUCTOR
145 //
146 //=================================================================================================
147 
148 //*************************************************************************************************
151 template< typename Type >
153 {}
154 //*************************************************************************************************
155 
156 
157 //*************************************************************************************************
162 template< typename Type >
163 template< typename Type2 >
165 {
166  UNUSED_PARAMETER( allocator );
167 }
168 //*************************************************************************************************
169 
170 
171 
172 
173 //=================================================================================================
174 //
175 // UTILITY FUNCTIONS
176 //
177 //=================================================================================================
178 
179 //*************************************************************************************************
184 template< typename Type >
185 inline size_t AlignedAllocator<Type>::max_size() const
186 {
187  return size_t(-1) / sizeof( Type );
188 }
189 //*************************************************************************************************
190 
191 
192 //*************************************************************************************************
197 template< typename Type >
198 inline typename AlignedAllocator<Type>::Pointer
200 {
201  return &x;
202 }
203 //*************************************************************************************************
204 
205 
206 //*************************************************************************************************
211 template< typename Type >
214 {
215  return &x;
216 }
217 //*************************************************************************************************
218 
219 
220 
221 
222 //=================================================================================================
223 //
224 // ALLOCATION FUNCTIONS
225 //
226 //=================================================================================================
227 
228 //*************************************************************************************************
241 template< typename Type >
242 inline typename AlignedAllocator<Type>::Pointer
243  AlignedAllocator<Type>::allocate( size_t numObjects, const void* localityHint )
244 {
245  UNUSED_PARAMETER( localityHint );
246 
247  const size_t alignment( AlignmentOf<Type>::value );
248 
249  if( alignment >= 8UL ) {
250  return reinterpret_cast<Type*>( allocate_backend( numObjects*sizeof(Type), alignment ) );
251  }
252  else {
253  return static_cast<Pointer>( operator new[]( numObjects * sizeof( Type ) ) );
254  }
255 }
256 //*************************************************************************************************
257 
258 
259 //*************************************************************************************************
270 template< typename Type >
271 inline void AlignedAllocator<Type>::deallocate( Pointer ptr, size_t numObjects )
272 {
273  UNUSED_PARAMETER( numObjects );
274 
275  if( ptr == NULL )
276  return;
277 
278  const size_t alignment( AlignmentOf<Type>::value );
279 
280  if( alignment >= 8UL ) {
281  deallocate_backend( ptr );
282  }
283  else {
284  operator delete[]( ptr );
285  }
286 }
287 //*************************************************************************************************
288 
289 
290 
291 
292 //=================================================================================================
293 //
294 // CONSTRUCTION FUNCTIONS
295 //
296 //=================================================================================================
297 
298 //*************************************************************************************************
308 template< typename Type >
310 {
311  ::new( ptr ) Type( value );
312 }
313 //*************************************************************************************************
314 
315 
316 //*************************************************************************************************
325 template< typename Type >
327 {
328  ptr->~Type();
329 }
330 //*************************************************************************************************
331 
332 
333 
334 
335 //=================================================================================================
336 //
337 // GLOBAL OPERATORS
338 //
339 //=================================================================================================
340 
341 //*************************************************************************************************
344 template< typename T1, typename T2 >
345 inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs );
346 
347 template< typename T1, typename T2 >
348 inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs );
350 //*************************************************************************************************
351 
352 
353 //*************************************************************************************************
360 template< typename T1 // Type of the left-hand side aligned allocator
361  , typename T2 > // Type of the right-hand side aligned allocator
362 inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs )
363 {
364  UNUSED_PARAMETER( lhs, rhs );
365  return true;
366 }
367 //*************************************************************************************************
368 
369 
370 //*************************************************************************************************
377 template< typename T1 // Type of the left-hand side aligned allocator
378  , typename T2 > // Type of the right-hand side aligned allocator
379 inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs )
380 {
381  UNUSED_PARAMETER( lhs, rhs );
382  return false;
383 }
384 //*************************************************************************************************
385 
386 } // namespace blaze
387 
388 #endif
Type ValueType
Type of the allocated values.
Definition: AlignedAllocator.h:74
SizeType size_type
Size type of the aligned allocator.
Definition: AlignedAllocator.h:88
Header file for the AlignmentOf type trait.
Header file for the UNUSED_PARAMETER function template.
void UNUSED_PARAMETER(const T1 &)
Suppression of unused parameter warnings.
Definition: Unused.h:81
Type * Pointer
Type of a pointer to the allocated values.
Definition: AlignedAllocator.h:75
AlignedAllocator< Type2 > other
Type of the other allocator.
Definition: AlignedAllocator.h:98
Header file for memory allocation and deallocation functionality.
const Type & ConstReference
Type of a reference-to-const to the allocated values.
Definition: AlignedAllocator.h:78
std::ptrdiff_t DifferenceType
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:80
const Type * ConstPointer
Type of a pointer-to-const to the allocated values.
Definition: AlignedAllocator.h:76
Pointer allocate(size_t numObjects, const void *localityHint=NULL)
Allocates aligned memory for the specified number of objects.
Definition: AlignedAllocator.h:243
Namespace of the Blaze C++ math library.
Definition: Blaze.h:57
size_t max_size() const
Returns the maximum possible number of elements that can be allocated together.
Definition: AlignedAllocator.h:185
ValueType value_type
Type of the allocated values.
Definition: AlignedAllocator.h:83
void construct(Pointer ptr, const Type &value)
Constructs an object of type Type at the specified memory location.
Definition: AlignedAllocator.h:309
DifferenceType difference_type
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:89
const Type & ConstReference
Reference to a constant matrix value.
Definition: CompressedMatrix.h:2590
Allocator for type-specific aligned memory.The AlignedAllocator class template represents an implemen...
Definition: AlignedAllocator.h:70
ConstReference const_reference
Type of a reference-to-const to the allocated values.
Definition: AlignedAllocator.h:87
std::size_t SizeType
Size type of the aligned allocator.
Definition: AlignedAllocator.h:79
Pointer pointer
Type of a pointer to the allocated values.
Definition: AlignedAllocator.h:84
void deallocate(Pointer ptr, size_t numObjects)
Deallocation of memory.
Definition: AlignedAllocator.h:271
ConstPointer const_pointer
Type of a pointer-to-const to the allocated values.
Definition: AlignedAllocator.h:85
Pointer address(Reference x) const
Returns the address of the given element.
Definition: AlignedAllocator.h:199
AlignedAllocator()
The default constructor for AlignedAllocator.
Definition: AlignedAllocator.h:152
bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value. ...
Definition: Accuracy.h:249
bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:289
Reference reference
Type of a reference to the allocated values.
Definition: AlignedAllocator.h:86
void destroy(Pointer ptr)
Destroys the object of type Type at the specified memory location.
Definition: AlignedAllocator.h:326
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:77
Type & Reference
Type of a reference to the allocated values.
Definition: AlignedAllocator.h:77
Implementation of the AlignedAllocator rebind mechanism.
Definition: AlignedAllocator.h:96
Header file for a safe C++ NULL pointer implementation.