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>
50 #include <blaze/util/Null.h>
51 #include <blaze/util/Types.h>
52 
53 
54 namespace blaze {
55 
56 //=================================================================================================
57 //
58 // ALLOCATION FUNCTIONS
59 //
60 //=================================================================================================
61 
62 //*************************************************************************************************
83 template< typename T >
84 T* allocate( size_t size )
85 {
86  void* tmp( NULL );
87  const size_t alignment( AlignmentTrait<T>::value );
88 
89  if( alignment >= 8UL ) {
90 #if defined(_MSC_VER)
91  tmp = _aligned_malloc( size*sizeof(T), alignment );
92  if( tmp != NULL )
93 #else
94  if( !posix_memalign( &tmp, alignment, size*sizeof(T) ) )
95 #endif
96  return reinterpret_cast<T*>( tmp );
97  else throw std::bad_alloc();
98  }
99  else return ::new T[size];
100 }
101 //*************************************************************************************************
102 
103 
104 //*************************************************************************************************
114 template< typename T >
115 void deallocate( T* address )
116 {
117  const size_t alignment( AlignmentTrait<T>::value );
118 
119  if( alignment >= 8UL ) {
120 #if defined(_MSC_VER)
121  _aligned_free( address );
122 #else
123  free( address );
124 #endif
125  }
126  else delete[] address;
127 }
128 //*************************************************************************************************
129 
130 } // namespace blaze
131 
132 #endif
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.
void deallocate(T *address)
Deallocation of memory.
Definition: Memory.h:115
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
T * allocate(size_t size)
Aligned array allocation.
Definition: Memory.h:84
Header file for basic type definitions.