21 #ifndef __TBB_memory_pool_H 22 #define __TBB_memory_pool_H 24 #if !TBB_PREVIEW_MEMORY_POOL 25 #error Set TBB_PREVIEW_MEMORY_POOL to include memory_pool.h 34 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC 39 #define __TBBMALLOC_ASSERT ASSERT 41 #define __TBBMALLOC_ASSERT(a,b) ((void)0) 45 namespace interface6 {
50 class pool_base : tbb::internal::no_copy {
55 void recycle() { rml::pool_reset(my_pool); }
58 void *malloc(
size_t size) {
return rml::pool_malloc(my_pool, size); }
61 void free(
void* ptr) { rml::pool_free(my_pool, ptr); }
65 void *realloc(
void* ptr,
size_t size) {
66 return rml::pool_realloc(my_pool, ptr, size);
71 void destroy() { rml::pool_destroy(my_pool); }
73 rml::MemoryPool *my_pool;
79 #if _MSC_VER && !defined(__INTEL_COMPILER) 81 #pragma warning (push) 82 #pragma warning (disable: 4100) 87 template<
typename T,
typename P =
internal::pool_base>
92 template<
typename U,
typename R>
94 template<
typename V,
typename U,
typename R>
96 template<
typename V,
typename U,
typename R>
99 typedef typename tbb::internal::allocator_type<T>::value_type value_type;
100 typedef value_type* pointer;
101 typedef const value_type* const_pointer;
102 typedef value_type& reference;
103 typedef const value_type& const_reference;
104 typedef size_t size_type;
105 typedef ptrdiff_t difference_type;
115 pointer address(reference x)
const {
return &x; }
116 const_pointer address(const_reference x)
const {
return &x; }
120 pointer p =
static_cast<pointer
>( my_pool->malloc( n*
sizeof(value_type) ) );
122 tbb::internal::throw_exception(std::bad_alloc());
131 size_type max =
static_cast<size_type
>(-1) /
sizeof (value_type);
132 return (max > 0 ? max : 1);
135 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC 136 template<
typename U,
typename... Args>
137 void construct(U *p, Args&&... args)
138 { ::new((
void *)p) U(std::forward<Args>(args)...); }
139 #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC 140 #if __TBB_CPP11_RVALUE_REF_PRESENT 141 void construct( pointer p, value_type&& value ) {::new((
void*)(p)) value_type(std::move(value));}
143 void construct( pointer p,
const value_type& value ) { ::new((
void*)(p)) value_type(value); }
144 #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC 147 void destroy( pointer p ) { p->~value_type(); }
151 #if _MSC_VER && !defined(__INTEL_COMPILER) 152 #pragma warning (pop) 153 #endif // warning 4100 is back 161 typedef void* pointer;
162 typedef const void* const_pointer;
163 typedef void value_type;
164 template<
typename U>
struct rebind {
175 template<
typename U,
typename R>
177 template<
typename V,
typename U,
typename R>
179 template<
typename V,
typename U,
typename R>
183 template<
typename T,
typename U,
typename P>
186 template<
typename T,
typename U,
typename P>
191 template <
typename Alloc>
194 static void *allocate_request(intptr_t pool_id,
size_t & bytes);
195 static int deallocate_request(intptr_t pool_id,
void*,
size_t raw_bytes);
209 inline static void *allocate_request(intptr_t pool_id,
size_t & bytes);
220 template <
typename Alloc>
222 rml::MemPoolPolicy args(allocate_request, deallocate_request,
223 sizeof(
typename Alloc::value_type));
224 rml::MemPoolError res = rml::pool_create_v1(intptr_t(
this), &args, &my_pool);
225 if (res!=rml::POOL_OK)
226 tbb::internal::throw_exception(std::runtime_error(
"Can't create pool"));
228 template <
typename Alloc>
231 const size_t unit_size =
sizeof(
typename Alloc::value_type);
232 __TBBMALLOC_ASSERT( 0 == bytes%unit_size, NULL);
234 __TBB_TRY { ptr =
self.my_alloc.allocate( bytes/unit_size ); }
235 __TBB_CATCH(...) {
return 0; }
238 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED 241 #pragma warning (push) 242 #pragma warning (disable: 4702) 244 template <
typename Alloc>
247 const size_t unit_size =
sizeof(
typename Alloc::value_type);
248 __TBBMALLOC_ASSERT( 0 == raw_bytes%unit_size, NULL);
249 self.my_alloc.deallocate( static_cast<typename Alloc::value_type*>(raw_ptr), raw_bytes/unit_size );
252 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED 253 #pragma warning (pop) 258 tbb::internal::throw_exception(std::invalid_argument(
"Zero in parameter is invalid"));
259 rml::MemPoolPolicy args(allocate_request, 0, size,
true);
260 rml::MemPoolError res = rml::pool_create_v1(intptr_t(
this), &args, &my_pool);
261 if (res!=rml::POOL_OK)
262 tbb::internal::throw_exception(std::runtime_error(
"Can't create pool"));
264 inline void *fixed_pool::allocate_request(intptr_t pool_id,
size_t & bytes) {
266 __TBBMALLOC_ASSERT(0 !=
self.my_size,
"The buffer must not be used twice.");
267 bytes =
self.my_size;
269 return self.my_buffer;
278 #undef __TBBMALLOC_ASSERT 279 #endif// __TBB_memory_pool_H ~fixed_pool()
destroy pool
Definition: memory_pool.h:215
~memory_pool()
destroy pool
Definition: memory_pool.h:202
fixed_pool(void *buf, size_t size)
construct pool with underlying allocator
Definition: memory_pool.h:255
void construct(pointer p, const value_type &value)
Copy-construct value at location pointed to by p.
Definition: memory_pool.h:143
memory_pool(const Alloc &src=Alloc())
construct pool with underlying allocator
Definition: memory_pool.h:221
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: memory_pool.h:88
size_type max_size() const
Largest value for which method allocate might succeed.
Definition: memory_pool.h:130
void deallocate(pointer p, size_type)
Free previously allocated block of memory.
Definition: memory_pool.h:126
Definition: _flow_graph_async_msg_impl.h:32
Definition: memory_pool.h:206
The namespace tbb contains all components of the library.
Definition: parallel_for.h:44
Definition: memory_pool.h:106
pointer allocate(size_type n, const void *=0)
Allocate space for n objects.
Definition: memory_pool.h:119
Thread-safe growable pool allocator for variable-size requests.
Definition: memory_pool.h:192
void destroy(pointer p)
Destroy value at location pointed to by p.
Definition: memory_pool.h:147