00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef __TBB_tbb_allocator_H
00022 #define __TBB_tbb_allocator_H
00023
00024 #include <new>
00025 #include <cstring>
00026 #include "tbb_stddef.h"
00027
00028 namespace tbb {
00029
00031 namespace internal {
00032
00034
00035 void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
00036
00038
00039 void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
00040
00042 bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
00043 }
00045
00046 #if _MSC_VER && !defined(__INTEL_COMPILER)
00047
00048 #pragma warning (push)
00049 #pragma warning (disable: 4100)
00050 #endif
00051
00053
00058 template<typename T>
00059 class tbb_allocator {
00060 public:
00061 typedef typename internal::allocator_type<T>::value_type value_type;
00062 typedef value_type* pointer;
00063 typedef const value_type* const_pointer;
00064 typedef value_type& reference;
00065 typedef const value_type& const_reference;
00066 typedef size_t size_type;
00067 typedef ptrdiff_t difference_type;
00068 template<typename U> struct rebind {
00069 typedef tbb_allocator<U> other;
00070 };
00071
00073 enum malloc_type {
00074 scalable,
00075 standard
00076 };
00077
00078 tbb_allocator() throw() {}
00079 tbb_allocator( const tbb_allocator& ) throw() {}
00080 template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
00081
00082 pointer address(reference x) const {return &x;}
00083 const_pointer address(const_reference x) const {return &x;}
00084
00086 pointer allocate( size_type n, const void* = 0) {
00087 return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
00088 }
00089
00091 void deallocate( pointer p, size_type ) {
00092 internal::deallocate_via_handler_v3(p);
00093 }
00094
00096 size_type max_size() const throw() {
00097 size_type max = static_cast<size_type>(-1) / sizeof (value_type);
00098 return (max > 0 ? max : 1);
00099 }
00100
00102 void construct( pointer p, const value_type& value ) {new(static_cast<void*>(p)) value_type(value);}
00103
00105 void destroy( pointer p ) {p->~value_type();}
00106
00108 static malloc_type allocator_type() {
00109 return internal::is_malloc_used_v3() ? standard : scalable;
00110 }
00111 };
00112
00113 #if _MSC_VER && !defined(__INTEL_COMPILER)
00114 #pragma warning (pop)
00115 #endif // warning 4100 is back
00116
00118
00119 template<>
00120 class tbb_allocator<void> {
00121 public:
00122 typedef void* pointer;
00123 typedef const void* const_pointer;
00124 typedef void value_type;
00125 template<typename U> struct rebind {
00126 typedef tbb_allocator<U> other;
00127 };
00128 };
00129
00130 template<typename T, typename U>
00131 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
00132
00133 template<typename T, typename U>
00134 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
00135
00137
00142 template <typename T, template<typename X> class Allocator = tbb_allocator>
00143 class zero_allocator : public Allocator<T>
00144 {
00145 public:
00146 typedef Allocator<T> base_allocator_type;
00147 typedef typename base_allocator_type::value_type value_type;
00148 typedef typename base_allocator_type::pointer pointer;
00149 typedef typename base_allocator_type::const_pointer const_pointer;
00150 typedef typename base_allocator_type::reference reference;
00151 typedef typename base_allocator_type::const_reference const_reference;
00152 typedef typename base_allocator_type::size_type size_type;
00153 typedef typename base_allocator_type::difference_type difference_type;
00154 template<typename U> struct rebind {
00155 typedef zero_allocator<U, Allocator> other;
00156 };
00157
00158 zero_allocator() throw() { }
00159 zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
00160 template<typename U>
00161 zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
00162
00163 pointer allocate(const size_type n, const void *hint = 0 ) {
00164 pointer ptr = base_allocator_type::allocate( n, hint );
00165 std::memset( ptr, 0, n * sizeof(value_type) );
00166 return ptr;
00167 }
00168 };
00169
00171
00172 template<template<typename T> class Allocator>
00173 class zero_allocator<void, Allocator> : public Allocator<void> {
00174 public:
00175 typedef Allocator<void> base_allocator_type;
00176 typedef typename base_allocator_type::value_type value_type;
00177 typedef typename base_allocator_type::pointer pointer;
00178 typedef typename base_allocator_type::const_pointer const_pointer;
00179 template<typename U> struct rebind {
00180 typedef zero_allocator<U, Allocator> other;
00181 };
00182 };
00183
00184 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00185 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00186 return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
00187 }
00188 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00189 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00190 return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
00191 }
00192
00193 }
00194
00195 #endif