00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029 #ifndef __TBB_tbb_allocator_H
00030 #define __TBB_tbb_allocator_H
00031
00032 #include "tbb_stddef.h"
00033 #include <new>
00034 #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
00035 #include <utility>
00036 #endif
00037
00038 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00039
00040 #pragma warning (push)
00041 #pragma warning (disable: 4530)
00042 #endif
00043
00044 #include <cstring>
00045
00046 #if !TBB_USE_EXCEPTIONS && _MSC_VER
00047 #pragma warning (pop)
00048 #endif
00049
00050 namespace tbb {
00051
00053 namespace internal {
00054
00056
00057 void __TBB_EXPORTED_FUNC deallocate_via_handler_v3( void *p );
00058
00060
00061 void* __TBB_EXPORTED_FUNC allocate_via_handler_v3( size_t n );
00062
00064 bool __TBB_EXPORTED_FUNC is_malloc_used_v3();
00065 }
00067
00068 #if _MSC_VER && !defined(__INTEL_COMPILER)
00069
00070 #pragma warning (push)
00071 #pragma warning (disable: 4100)
00072 #endif
00073
00075
00080 template<typename T>
00081 class tbb_allocator {
00082 public:
00083 typedef typename internal::allocator_type<T>::value_type value_type;
00084 typedef value_type* pointer;
00085 typedef const value_type* const_pointer;
00086 typedef value_type& reference;
00087 typedef const value_type& const_reference;
00088 typedef size_t size_type;
00089 typedef ptrdiff_t difference_type;
00090 template<typename U> struct rebind {
00091 typedef tbb_allocator<U> other;
00092 };
00093
00095 enum malloc_type {
00096 scalable,
00097 standard
00098 };
00099
00100 tbb_allocator() throw() {}
00101 tbb_allocator( const tbb_allocator& ) throw() {}
00102 template<typename U> tbb_allocator(const tbb_allocator<U>&) throw() {}
00103
00104 pointer address(reference x) const {return &x;}
00105 const_pointer address(const_reference x) const {return &x;}
00106
00108 pointer allocate( size_type n, const void* = 0) {
00109 return pointer(internal::allocate_via_handler_v3( n * sizeof(value_type) ));
00110 }
00111
00113 void deallocate( pointer p, size_type ) {
00114 internal::deallocate_via_handler_v3(p);
00115 }
00116
00118 size_type max_size() const throw() {
00119 size_type max = static_cast<size_type>(-1) / sizeof (value_type);
00120 return (max > 0 ? max : 1);
00121 }
00122
00124 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00125 template<typename... Args>
00126 void construct(pointer p, Args&&... args)
00127 #if __TBB_CPP11_STD_FORWARD_BROKEN
00128 { ::new((void *)p) T((args)...); }
00129 #else
00130 { ::new((void *)p) T(std::forward<Args>(args)...); }
00131 #endif
00132 #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00133 void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
00134 #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00135
00137 void destroy( pointer p ) {p->~value_type();}
00138
00140 static malloc_type allocator_type() {
00141 return internal::is_malloc_used_v3() ? standard : scalable;
00142 }
00143 };
00144
00145 #if _MSC_VER && !defined(__INTEL_COMPILER)
00146 #pragma warning (pop)
00147 #endif // warning 4100 is back
00148
00150
00151 template<>
00152 class tbb_allocator<void> {
00153 public:
00154 typedef void* pointer;
00155 typedef const void* const_pointer;
00156 typedef void value_type;
00157 template<typename U> struct rebind {
00158 typedef tbb_allocator<U> other;
00159 };
00160 };
00161
00162 template<typename T, typename U>
00163 inline bool operator==( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return true;}
00164
00165 template<typename T, typename U>
00166 inline bool operator!=( const tbb_allocator<T>&, const tbb_allocator<U>& ) {return false;}
00167
00169
00174 template <typename T, template<typename X> class Allocator = tbb_allocator>
00175 class zero_allocator : public Allocator<T>
00176 {
00177 public:
00178 typedef Allocator<T> base_allocator_type;
00179 typedef typename base_allocator_type::value_type value_type;
00180 typedef typename base_allocator_type::pointer pointer;
00181 typedef typename base_allocator_type::const_pointer const_pointer;
00182 typedef typename base_allocator_type::reference reference;
00183 typedef typename base_allocator_type::const_reference const_reference;
00184 typedef typename base_allocator_type::size_type size_type;
00185 typedef typename base_allocator_type::difference_type difference_type;
00186 template<typename U> struct rebind {
00187 typedef zero_allocator<U, Allocator> other;
00188 };
00189
00190 zero_allocator() throw() { }
00191 zero_allocator(const zero_allocator &a) throw() : base_allocator_type( a ) { }
00192 template<typename U>
00193 zero_allocator(const zero_allocator<U> &a) throw() : base_allocator_type( Allocator<U>( a ) ) { }
00194
00195 pointer allocate(const size_type n, const void *hint = 0 ) {
00196 pointer ptr = base_allocator_type::allocate( n, hint );
00197 std::memset( ptr, 0, n * sizeof(value_type) );
00198 return ptr;
00199 }
00200 };
00201
00203
00204 template<template<typename T> class Allocator>
00205 class zero_allocator<void, Allocator> : public Allocator<void> {
00206 public:
00207 typedef Allocator<void> base_allocator_type;
00208 typedef typename base_allocator_type::value_type value_type;
00209 typedef typename base_allocator_type::pointer pointer;
00210 typedef typename base_allocator_type::const_pointer const_pointer;
00211 template<typename U> struct rebind {
00212 typedef zero_allocator<U, Allocator> other;
00213 };
00214 };
00215
00216 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00217 inline bool operator==( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00218 return static_cast< B1<T1> >(a) == static_cast< B2<T2> >(b);
00219 }
00220 template<typename T1, template<typename X1> class B1, typename T2, template<typename X2> class B2>
00221 inline bool operator!=( const zero_allocator<T1,B1> &a, const zero_allocator<T2,B2> &b) {
00222 return static_cast< B1<T1> >(a) != static_cast< B2<T2> >(b);
00223 }
00224
00225 }
00226
00227 #endif