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 <new>
35 #include <stdexcept>
36 #include <boost/checked_delete.hpp>
38 #include <blaze/util/Null.h>
39 #include <blaze/util/Types.h>
41 
42 
43 namespace blaze {
44 
45 //=================================================================================================
46 //
47 // ALLOCATION FUNCTIONS
48 //
49 //=================================================================================================
50 
51 //*************************************************************************************************
71 template< typename T >
72 T* allocate( size_t size )
73 {
75  {
76  void* tmp( NULL );
77  const size_t alignment( AlignmentTrait<T>::value );
78 
79 #if defined(_MSC_VER)
80  tmp = _aligned_malloc( size*sizeof(T), alignment );
81  if( tmp != NULL )
82 #else
83  if( !posix_memalign( &tmp, alignment, size*sizeof(T) ) )
84 #endif
85  return reinterpret_cast<T*>( tmp );
86  else throw std::bad_alloc();
87  }
88  else return new T[size];
89 }
90 //*************************************************************************************************
91 
92 
93 //*************************************************************************************************
99 template< typename T >
100 void deallocate( T* address )
101 {
103 #if defined(_MSC_VER)
104  _aligned_free( address );
105 #else
106  free( address );
107 #endif
108  }
109  else {
110  boost::checked_array_delete( address );
111  }
112 }
113 //*************************************************************************************************
114 
115 } // namespace blaze
116 
117 #endif