All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Memory.h
Go to the documentation of this file.
1 //=================================================================================================
33 //=================================================================================================
34 
35 #ifndef _BLAZE_UTIL_MEMORY_H_
36 #define _BLAZE_UTIL_MEMORY_H_
37 
38 
39 //*************************************************************************************************
40 // Includes
41 //*************************************************************************************************
42 
43 #if defined(_MSC_VER)
44 # include <malloc.h>
45 #endif
46 #include <cstdlib>
47 #include <new>
48 #include <stdexcept>
49 #include <boost/checked_delete.hpp>
51 #include <blaze/util/Null.h>
52 #include <blaze/util/Types.h>
54 
55 
56 namespace blaze {
57 
58 //=================================================================================================
59 //
60 // ALLOCATION FUNCTIONS
61 //
62 //=================================================================================================
63 
64 //*************************************************************************************************
84 template< typename T >
85 T* allocate( size_t size )
86 {
88  {
89  void* tmp( NULL );
90  const size_t alignment( AlignmentTrait<T>::value );
91 
92 #if defined(_MSC_VER)
93  tmp = _aligned_malloc( size*sizeof(T), alignment );
94  if( tmp != NULL )
95 #else
96  if( !posix_memalign( &tmp, alignment, size*sizeof(T) ) )
97 #endif
98  return reinterpret_cast<T*>( tmp );
99  else throw std::bad_alloc();
100  }
101  else return new T[size];
102 }
103 //*************************************************************************************************
104 
105 
106 //*************************************************************************************************
112 template< typename T >
113 void deallocate( T* address )
114 {
115  if( IsVectorizable<T>::value && address != NULL ) {
116 #if defined(_MSC_VER)
117  _aligned_free( address );
118 #else
119  free( address );
120 #endif
121  }
122  else {
123  boost::checked_array_delete( address );
124  }
125 }
126 //*************************************************************************************************
127 
128 } // namespace blaze
129 
130 #endif
Compile time check for vectorizable types.Depending on the available instruction set (SSE...
Definition: IsVectorizable.h:107
void deallocate(T *address)
Deallocation of memory.
Definition: Memory.h:113
const blaze::Null NULL
Global NULL pointer.This instance of the Null class replaces the NULL macro to ensure a type-safe NUL...
Definition: Null.h:300
Header file for a safe C++ NULL pointer implementation.
T * allocate(size_t size)
Aligned array allocation.
Definition: Memory.h:85
Header file for the IsVectorizable type trait.
Header file for the alignment trait.
Evaluation of the required alignment of the given data type.The AlignmentTrait class template evaluat...
Definition: AlignmentTrait.h:79
Header file for basic type definitions.