All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Memory.h
Go to the documentation of this file.
1 //=================================================================================================
20 //=================================================================================================
21 
22 #ifndef _BLAZE_UTIL_MEMORY_H_
23 #define _BLAZE_UTIL_MEMORY_H_
24 
25 
26 //*************************************************************************************************
27 // Includes
28 //*************************************************************************************************
29 
30 #if defined(_MSC_VER)
31 # include <malloc.h>
32 #endif
33 #include <cstdlib>
34 #include <stdexcept>
36 #include <blaze/util/Null.h>
37 #include <blaze/util/Types.h>
39 
40 
41 namespace blaze {
42 
43 //=================================================================================================
44 //
45 // ALLOCATION FUNCTIONS
46 //
47 //=================================================================================================
48 
49 //*************************************************************************************************
69 template< typename T >
70 T* allocate( size_t size )
71 {
73  {
74  void* tmp( NULL );
75  const size_t alignment( AlignmentTrait<T>::value );
76 
77 #if defined(_MSC_VER)
78  tmp = _aligned_malloc( size*sizeof(T), alignment );
79  if( tmp != NULL )
80 #else
81  if( !posix_memalign( &tmp, alignment, size*sizeof(T) ) )
82 #endif
83  return reinterpret_cast<T*>( tmp );
84  else throw std::bad_alloc();
85  }
86  else return new T[size];
87 }
88 //*************************************************************************************************
89 
90 
91 //*************************************************************************************************
97 template< typename T >
98 void deallocate( T* address )
99 {
100  if( IsBuiltin<T>::value ) {
101 #if defined(_MSC_VER)
102  _aligned_free( address );
103 #else
104  free( address );
105 #endif
106  }
107  else delete[] address;
108 }
109 //*************************************************************************************************
110 
111 } // namespace blaze
112 
113 #endif