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_scalable_allocator_H
00030 #define __TBB_scalable_allocator_H
00031
00033 #include <stddef.h>
00034 #if !_MSC_VER
00035 #include <stdint.h>
00036 #endif
00037
00038 #if !defined(__cplusplus) && __ICC==1100
00039 #pragma warning (push)
00040 #pragma warning (disable: 991)
00041 #endif
00042
00043 #ifdef __cplusplus
00044 extern "C" {
00045 #endif
00046
00047 #if _MSC_VER >= 1400
00048 #define __TBB_EXPORTED_FUNC __cdecl
00049 #else
00050 #define __TBB_EXPORTED_FUNC
00051 #endif
00052
00055 void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
00056
00059 void __TBB_EXPORTED_FUNC scalable_free (void* ptr);
00060
00063 void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
00064
00067 void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
00068
00071 int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
00072
00075 void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
00076
00079 void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
00080
00083 void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
00084
00089 size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
00090
00091 #ifdef __cplusplus
00092 }
00093 #endif
00094
00095 #ifdef __cplusplus
00096
00097 namespace rml {
00098 class MemoryPool;
00099
00100 typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
00101 typedef int (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113 struct MemPoolPolicy {
00114 enum {
00115 VERSION = 1
00116 };
00117
00118 rawAllocType pAlloc;
00119 rawFreeType pFree;
00120
00121 size_t granularity;
00122 int version;
00123
00124
00125 unsigned fixedPool : 1,
00126
00127 keepAllMemory : 1,
00128 reserved : 30;
00129
00130 MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
00131 size_t granularity_ = 0, bool fixedPool_ = false,
00132 bool keepAllMemory_ = false) :
00133 pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(VERSION),
00134 fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
00135 reserved(0) {}
00136 };
00137
00138 enum MemPoolError {
00139 POOL_OK,
00140 INVALID_POLICY,
00141 UNSUPPORTED_POLICY,
00142 NO_MEMORY
00143 };
00144
00145 MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
00146 rml::MemoryPool **pool);
00147
00148 bool pool_destroy(MemoryPool* memPool);
00149 void *pool_malloc(MemoryPool* memPool, size_t size);
00150 void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
00151 void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
00152 void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
00153 bool pool_reset(MemoryPool* memPool);
00154 bool pool_free(MemoryPool *memPool, void *object);
00155 }
00156
00157 #include <new>
00158
00159
00160 #ifndef __TBB_NO_IMPLICIT_LINKAGE
00161 #define __TBB_NO_IMPLICIT_LINKAGE 1
00162 #include "tbb_stddef.h"
00163 #undef __TBB_NO_IMPLICIT_LINKAGE
00164 #else
00165 #include "tbb_stddef.h"
00166 #endif
00167
00168 #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
00169 #include <utility>
00170 #endif
00171
00172 namespace tbb {
00173
00174 #if _MSC_VER && !defined(__INTEL_COMPILER)
00175
00176 #pragma warning (push)
00177 #pragma warning (disable: 4100)
00178 #endif
00179
00181
00184 template<typename T>
00185 class scalable_allocator {
00186 public:
00187 typedef typename internal::allocator_type<T>::value_type value_type;
00188 typedef value_type* pointer;
00189 typedef const value_type* const_pointer;
00190 typedef value_type& reference;
00191 typedef const value_type& const_reference;
00192 typedef size_t size_type;
00193 typedef ptrdiff_t difference_type;
00194 template<class U> struct rebind {
00195 typedef scalable_allocator<U> other;
00196 };
00197
00198 scalable_allocator() throw() {}
00199 scalable_allocator( const scalable_allocator& ) throw() {}
00200 template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
00201
00202 pointer address(reference x) const {return &x;}
00203 const_pointer address(const_reference x) const {return &x;}
00204
00206 pointer allocate( size_type n, const void* =0 ) {
00207 return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
00208 }
00209
00211 void deallocate( pointer p, size_type ) {
00212 scalable_free( p );
00213 }
00214
00216 size_type max_size() const throw() {
00217 size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
00218 return (absolutemax > 0 ? absolutemax : 1);
00219 }
00220 #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00221 template<typename... Args>
00222 void construct(pointer p, Args&&... args)
00223 #if __TBB_CPP11_STD_FORWARD_BROKEN
00224 { ::new((void *)p) T((args)...); }
00225 #else
00226 { ::new((void *)p) T(std::forward<Args>(args)...); }
00227 #endif
00228 #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00229 void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
00230 #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
00231 void destroy( pointer p ) {p->~value_type();}
00232 };
00233
00234 #if _MSC_VER && !defined(__INTEL_COMPILER)
00235 #pragma warning (pop)
00236 #endif // warning 4100 is back
00237
00239
00240 template<>
00241 class scalable_allocator<void> {
00242 public:
00243 typedef void* pointer;
00244 typedef const void* const_pointer;
00245 typedef void value_type;
00246 template<class U> struct rebind {
00247 typedef scalable_allocator<U> other;
00248 };
00249 };
00250
00251 template<typename T, typename U>
00252 inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
00253
00254 template<typename T, typename U>
00255 inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
00256
00257 }
00258
00259 #if _MSC_VER
00260 #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
00261 #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
00262 #endif
00263
00264 #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
00265 #ifdef _DEBUG
00266 #pragma comment(lib, "tbbmalloc_debug.lib")
00267 #else
00268 #pragma comment(lib, "tbbmalloc.lib")
00269 #endif
00270 #endif
00271
00272
00273 #endif
00274
00275 #endif
00276
00277 #if !defined(__cplusplus) && __ICC==1100
00278 #pragma warning (pop)
00279 #endif // ICC 11.0 warning 991 is back
00280
00281 #endif