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 //*************************************************************************************************
82 template< typename T >
83 T* allocate( size_t size )
84 {
85  void* tmp( NULL );
86  const size_t alignment( AlignmentTrait<T>::value );
87 
88  if( alignment >= 8UL ) {
89 #if defined(_MSC_VER)
90  tmp = _aligned_malloc( size*sizeof(T), alignment );
91  if( tmp != NULL )
92 #else
93  if( !posix_memalign( &tmp, alignment, size*sizeof(T) ) )
94 #endif
95  return reinterpret_cast<T*>( tmp );
96  else throw std::bad_alloc();
97  }
98  else return ::new T[size];
99 }
100 //*************************************************************************************************
101 
102 
103 //*************************************************************************************************
113 template< typename T >
114 void deallocate( T* address )
115 {
116  const size_t alignment( AlignmentTrait<T>::value );
117 
118  if( alignment >= 8UL ) {
119 #if defined(_MSC_VER)
120  _aligned_free( address );
121 #else
122  free( address );
123 #endif
124  }
125  else delete[] address;
126 }
127 //*************************************************************************************************
128 
129 } // namespace blaze
130 
131 #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:114
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:83
Header file for basic type definitions.