Blaze 3.9
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
44#include <blaze/util/Memory.h>
45#include <blaze/util/Types.h>
47
48
49namespace blaze {
50
51//=================================================================================================
52//
53// CLASS DEFINITION
54//
55//=================================================================================================
56
57//*************************************************************************************************
70template< typename T >
72{
73 public:
74 //**Type definitions****************************************************************************
75 using ValueType = T;
76 using SizeType = size_t;
77 using DifferenceType = ptrdiff_t;
78
79 // STL allocator requirements
83 //**********************************************************************************************
84
85 //**rebind class definition*********************************************************************
88 template< typename U >
89 struct rebind
90 {
92 };
93 //**********************************************************************************************
94
95 //**Constructors********************************************************************************
98 AlignedAllocator() = default;
99
100 template< typename U >
101 inline AlignedAllocator( const AlignedAllocator<U>& );
103 //**********************************************************************************************
104
105 //**Allocation functions************************************************************************
108 inline T* allocate ( size_t numObjects );
109 inline void deallocate( T* ptr, size_t numObjects ) noexcept;
111 //**********************************************************************************************
112};
113//*************************************************************************************************
114
115
116
117
118//=================================================================================================
119//
120// CONSTRUCTOR
121//
122//=================================================================================================
123
124//*************************************************************************************************
129template< typename T >
130template< typename U >
132{
133 MAYBE_UNUSED( allocator );
134}
135//*************************************************************************************************
136
137
138
139
140//=================================================================================================
141//
142// ALLOCATION FUNCTIONS
143//
144//=================================================================================================
145
146//*************************************************************************************************
158template< typename T >
159inline T* AlignedAllocator<T>::allocate( size_t numObjects )
160{
161 const size_t alignment( AlignmentOf_v<T> );
162
163 if( alignment >= 8UL ) {
164 return reinterpret_cast<T*>( alignedAllocate( numObjects*sizeof(T), alignment ) );
165 }
166 else {
167 return static_cast<T*>( operator new[]( numObjects * sizeof( T ) ) );
168 }
169}
170//*************************************************************************************************
171
172
173//*************************************************************************************************
184template< typename T >
185inline void AlignedAllocator<T>::deallocate( T* ptr, size_t numObjects ) noexcept
186{
187 MAYBE_UNUSED( numObjects );
188
189 if( ptr == nullptr )
190 return;
191
192 const size_t alignment( AlignmentOf_v<T> );
193
194 if( alignment >= 8UL ) {
195 alignedDeallocate( ptr );
196 }
197 else {
198 operator delete[]( ptr );
199 }
200}
201//*************************************************************************************************
202
203
204
205
206//=================================================================================================
207//
208// GLOBAL OPERATORS
209//
210//=================================================================================================
211
212//*************************************************************************************************
215template< typename T1, typename T2 >
216inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept;
217
218template< typename T1, typename T2 >
219inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept;
221//*************************************************************************************************
222
223
224//*************************************************************************************************
231template< typename T1 // Type of the left-hand side aligned allocator
232 , typename T2 > // Type of the right-hand side aligned allocator
233inline bool operator==( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept
234{
235 MAYBE_UNUSED( lhs, rhs );
236 return true;
237}
238//*************************************************************************************************
239
240
241//*************************************************************************************************
248template< typename T1 // Type of the left-hand side aligned allocator
249 , typename T2 > // Type of the right-hand side aligned allocator
250inline bool operator!=( const AlignedAllocator<T1>& lhs, const AlignedAllocator<T2>& rhs ) noexcept
251{
252 MAYBE_UNUSED( lhs, rhs );
253 return false;
254}
255//*************************************************************************************************
256
257} // namespace blaze
258
259#endif
Header file for the AlignmentOf type trait.
Header file for the MAYBE_UNUSED function template.
Header file for memory allocation and deallocation functionality.
Allocator for type-specific aligned memory.
Definition: AlignedAllocator.h:72
size_t SizeType
Size type of the aligned allocator.
Definition: AlignedAllocator.h:76
ValueType value_type
Type of the allocated values.
Definition: AlignedAllocator.h:80
T * allocate(size_t numObjects)
Allocates aligned memory for the specified number of objects.
Definition: AlignedAllocator.h:159
T ValueType
Type of the allocated values.
Definition: AlignedAllocator.h:75
void deallocate(T *ptr, size_t numObjects) noexcept
Deallocation of memory.
Definition: AlignedAllocator.h:185
ptrdiff_t DifferenceType
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:77
SizeType size_type
Size type of the aligned allocator.
Definition: AlignedAllocator.h:81
DifferenceType difference_type
Difference type of the aligned allocator.
Definition: AlignedAllocator.h:82
constexpr bool operator==(const NegativeAccuracy< A > &lhs, const T &rhs)
Equality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:253
constexpr bool operator!=(const NegativeAccuracy< A > &lhs, const T &rhs)
Inequality comparison between a NegativeAccuracy object and a floating point value.
Definition: Accuracy.h:293
void alignedDeallocate(const void *address) noexcept
Deallocation of aligned memory.
Definition: Memory.h:115
constexpr void MAYBE_UNUSED(const Args &...)
Suppression of unused parameter warnings.
Definition: MaybeUnused.h:81
byte_t * alignedAllocate(size_t size, size_t alignment)
Aligned array allocation.
Definition: Memory.h:82
Implementation of the AlignedAllocator rebind mechanism.
Definition: AlignedAllocator.h:90
Header file for basic type definitions.