00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __TBB_cache_aligned_allocator_H
00022 #define __TBB_cache_aligned_allocator_H
00023
00024 #include <new>
00025 #include "tbb_stddef.h"
00026
00027 namespace tbb {
00028
00030 namespace internal {
00032
00035 const size_t NFS_MaxLineSize = 128;
00036
00038
00039 size_t NFS_GetLineSize();
00040
00042
00043 void* NFS_Allocate( size_t n_element, size_t element_size, void* hint );
00044
00046
00048 void NFS_Free( void* );
00049 }
00051
00053
00056 template<typename T>
00057 class cache_aligned_allocator {
00058 public:
00059 typedef T* pointer;
00060 typedef const T* const_pointer;
00061 typedef T& reference;
00062 typedef const T& const_reference;
00063 typedef T value_type;
00064 typedef size_t size_type;
00065 typedef ptrdiff_t difference_type;
00066 template<typename U> struct rebind {
00067 typedef cache_aligned_allocator<U> other;
00068 };
00069
00070 #if _WIN64
00071
00072
00074 char* _Charalloc( size_type size ) {
00075 return (char*)internal::NFS_Allocate( size, sizeof(T), 0 );
00076 }
00077 #endif
00078
00079 cache_aligned_allocator() throw() {}
00080 cache_aligned_allocator( const cache_aligned_allocator& ) throw() {}
00081 template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
00082
00083 pointer address(reference x) const {return &x;}
00084 const_pointer address(const_reference x) const {return &x;}
00085
00087 pointer allocate( size_type n, const void* hint=0 ) {
00088
00089 return pointer(internal::NFS_Allocate( n, sizeof(T), const_cast<void*>(hint) ));
00090 }
00091
00093 void deallocate( pointer p, size_type ) {
00094 internal::NFS_Free(p);
00095 }
00096
00098 size_type max_size() const throw() {
00099 return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(T);
00100 }
00101
00103 void construct( pointer p, const T& value ) {new(static_cast<void*>(p)) T(value);}
00104
00106 void destroy( pointer p ) {p->~T();}
00107 };
00108
00110
00111 template<>
00112 class cache_aligned_allocator<void> {
00113 public:
00114 typedef void* pointer;
00115 typedef const void* const_pointer;
00116 typedef void value_type;
00117 template<typename U> struct rebind {
00118 typedef cache_aligned_allocator<U> other;
00119 };
00120 };
00121
00122 template<typename T, typename U>
00123 inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
00124
00125 template<typename T, typename U>
00126 inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
00127
00128 }
00129
00130 #endif