Intel(R) Threading Building Blocks Doxygen Documentation  version 4.2.3
memory_pool.h
Go to the documentation of this file.
1 /*
2  Copyright (c) 2005-2020 Intel Corporation
3 
4  Licensed under the Apache License, Version 2.0 (the "License");
5  you may not use this file except in compliance with the License.
6  You may obtain a copy of the License at
7 
8  http://www.apache.org/licenses/LICENSE-2.0
9 
10  Unless required by applicable law or agreed to in writing, software
11  distributed under the License is distributed on an "AS IS" BASIS,
12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  See the License for the specific language governing permissions and
14  limitations under the License.
15 */
16 
17 #ifndef __TBB_memory_pool_H
18 #define __TBB_memory_pool_H
19 
20 #if !TBB_PREVIEW_MEMORY_POOL
21 #error Set TBB_PREVIEW_MEMORY_POOL to include memory_pool.h
22 #endif
23 
25 #include "scalable_allocator.h"
26 #include <new> // std::bad_alloc
27 #include <stdexcept> // std::runtime_error, std::invalid_argument
28 // required in C++03 to construct std::runtime_error and std::invalid_argument
29 #include <string>
30 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
31 #include <utility> // std::forward
32 #endif
33 
34 #if __TBB_EXTRA_DEBUG
35 #define __TBBMALLOC_ASSERT ASSERT
36 #else
37 #define __TBBMALLOC_ASSERT(a,b) ((void)0)
38 #endif
39 
40 namespace tbb {
41 namespace interface6 {
43 namespace internal {
44 
47  // Pool interface is separate from standard allocator classes because it has
48  // to maintain internal state, no copy or assignment. Move and swap are possible.
49 public:
51  void recycle() { rml::pool_reset(my_pool); }
52 
54  void *malloc(size_t size) { return rml::pool_malloc(my_pool, size); }
55 
57  void free(void* ptr) { rml::pool_free(my_pool, ptr); }
58 
60  // Enables some low-level optimization possibilities
61  void *realloc(void* ptr, size_t size) {
62  return rml::pool_realloc(my_pool, ptr, size);
63  }
64 
65 protected:
67  void destroy() { rml::pool_destroy(my_pool); }
68 
69  rml::MemoryPool *my_pool;
70 };
71 
72 } // namespace internal
74 
75 #if _MSC_VER && !defined(__INTEL_COMPILER)
76  // Workaround for erroneous "unreferenced parameter" warning in method destroy.
77  #pragma warning (push)
78  #pragma warning (disable: 4100)
79 #endif
80 
82 
83 template<typename T, typename P = internal::pool_base>
85 protected:
86  typedef P pool_type;
88  template<typename U, typename R>
89  friend class memory_pool_allocator;
90  template<typename V, typename U, typename R>
91  friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
92  template<typename V, typename U, typename R>
93  friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
94 public:
96  typedef value_type* pointer;
97  typedef const value_type* const_pointer;
99  typedef const value_type& const_reference;
100  typedef size_t size_type;
101  typedef ptrdiff_t difference_type;
102  template<typename U> struct rebind {
104  };
105 
106  explicit memory_pool_allocator(pool_type &pool) throw() : my_pool(&pool) {}
107  memory_pool_allocator(const memory_pool_allocator& src) throw() : my_pool(src.my_pool) {}
108  template<typename U>
109  memory_pool_allocator(const memory_pool_allocator<U,P>& src) throw() : my_pool(src.my_pool) {}
110 
111  pointer address(reference x) const { return &x; }
112  const_pointer address(const_reference x) const { return &x; }
113 
115  pointer allocate( size_type n, const void* /*hint*/ = 0) {
116  pointer p = static_cast<pointer>( my_pool->malloc( n*sizeof(value_type) ) );
117  if (!p)
118  tbb::internal::throw_exception(std::bad_alloc());
119  return p;
120  }
123  my_pool->free(p);
124  }
126  size_type max_size() const throw() {
127  size_type max = static_cast<size_type>(-1) / sizeof (value_type);
128  return (max > 0 ? max : 1);
129  }
131 #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
132  template<typename U, typename... Args>
133  void construct(U *p, Args&&... args)
134  { ::new((void *)p) U(std::forward<Args>(args)...); }
135 #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
136 #if __TBB_CPP11_RVALUE_REF_PRESENT
137  void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
138 #endif
139  void construct( pointer p, const value_type& value ) { ::new((void*)(p)) value_type(value); }
140 #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
141 
143  void destroy( pointer p ) { p->~value_type(); }
144 
145 };
146 
147 #if _MSC_VER && !defined(__INTEL_COMPILER)
148  #pragma warning (pop)
149 #endif // warning 4100 is back
150 
152 
153 template<typename P>
155 public:
156  typedef P pool_type;
157  typedef void* pointer;
158  typedef const void* const_pointer;
159  typedef void value_type;
160  template<typename U> struct rebind {
162  };
163 
164  explicit memory_pool_allocator( pool_type &pool) throw() : my_pool(&pool) {}
165  memory_pool_allocator( const memory_pool_allocator& src) throw() : my_pool(src.my_pool) {}
166  template<typename U>
167  memory_pool_allocator(const memory_pool_allocator<U,P>& src) throw() : my_pool(src.my_pool) {}
168 
169 protected:
171  template<typename U, typename R>
172  friend class memory_pool_allocator;
173  template<typename V, typename U, typename R>
174  friend bool operator==( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
175  template<typename V, typename U, typename R>
176  friend bool operator!=( const memory_pool_allocator<V,R>& a, const memory_pool_allocator<U,R>& b);
177 };
178 
179 template<typename T, typename U, typename P>
180 inline bool operator==( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool==b.my_pool;}
181 
182 template<typename T, typename U, typename P>
183 inline bool operator!=( const memory_pool_allocator<T,P>& a, const memory_pool_allocator<U,P>& b) {return a.my_pool!=b.my_pool;}
184 
185 
187 template <typename Alloc>
189  Alloc my_alloc; // TODO: base-class optimization
190  static void *allocate_request(intptr_t pool_id, size_t & bytes);
191  static int deallocate_request(intptr_t pool_id, void*, size_t raw_bytes);
192 
193 public:
195  explicit memory_pool(const Alloc &src = Alloc());
196 
198  ~memory_pool() { destroy(); } // call the callbacks first and destroy my_alloc latter
199 
200 };
201 
203  void *my_buffer;
204  size_t my_size;
205  inline static void *allocate_request(intptr_t pool_id, size_t & bytes);
206 
207 public:
209  inline fixed_pool(void *buf, size_t size);
212 };
213 
215 
216 template <typename Alloc>
217 memory_pool<Alloc>::memory_pool(const Alloc &src) : my_alloc(src) {
218  rml::MemPoolPolicy args(allocate_request, deallocate_request,
219  sizeof(typename Alloc::value_type));
220  rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
221  if (res!=rml::POOL_OK)
222  tbb::internal::throw_exception(std::runtime_error("Can't create pool"));
223 }
224 template <typename Alloc>
225 void *memory_pool<Alloc>::allocate_request(intptr_t pool_id, size_t & bytes) {
226  memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
227  const size_t unit_size = sizeof(typename Alloc::value_type);
228  __TBBMALLOC_ASSERT( 0 == bytes%unit_size, NULL);
229  void *ptr;
230  __TBB_TRY { ptr = self.my_alloc.allocate( bytes/unit_size ); }
231  __TBB_CATCH(...) { return 0; }
232  return ptr;
233 }
234 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
235  // Workaround for erroneous "unreachable code" warning in the template below.
236  // Specific for VC++ 17-18 compiler
237  #pragma warning (push)
238  #pragma warning (disable: 4702)
239 #endif
240 template <typename Alloc>
241 int memory_pool<Alloc>::deallocate_request(intptr_t pool_id, void* raw_ptr, size_t raw_bytes) {
242  memory_pool<Alloc> &self = *reinterpret_cast<memory_pool<Alloc>*>(pool_id);
243  const size_t unit_size = sizeof(typename Alloc::value_type);
244  __TBBMALLOC_ASSERT( 0 == raw_bytes%unit_size, NULL);
245  self.my_alloc.deallocate( static_cast<typename Alloc::value_type*>(raw_ptr), raw_bytes/unit_size );
246  return 0;
247 }
248 #if __TBB_MSVC_UNREACHABLE_CODE_IGNORED
249  #pragma warning (pop)
250 #endif
251 inline fixed_pool::fixed_pool(void *buf, size_t size) : my_buffer(buf), my_size(size) {
252  if (!buf || !size)
253  // TODO: improve support for mode with exceptions disabled
254  tbb::internal::throw_exception(std::invalid_argument("Zero in parameter is invalid"));
255  rml::MemPoolPolicy args(allocate_request, 0, size, /*fixedPool=*/true);
256  rml::MemPoolError res = rml::pool_create_v1(intptr_t(this), &args, &my_pool);
257  if (res!=rml::POOL_OK)
258  tbb::internal::throw_exception(std::runtime_error("Can't create pool"));
259 }
260 inline void *fixed_pool::allocate_request(intptr_t pool_id, size_t & bytes) {
261  fixed_pool &self = *reinterpret_cast<fixed_pool*>(pool_id);
262  __TBBMALLOC_ASSERT(0 != self.my_size, "The buffer must not be used twice.");
263  bytes = self.my_size;
264  self.my_size = 0; // remember that buffer has been used
265  return self.my_buffer;
266 }
267 
268 } //namespace interface6
272 } //namespace tbb
273 
274 #undef __TBBMALLOC_ASSERT
275 #endif// __TBB_memory_pool_H
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(pool_type &pool)
Definition: memory_pool.h:164
tbb::interface6::memory_pool_allocator::pointer
value_type * pointer
Definition: memory_pool.h:96
tbb::interface6::memory_pool_allocator::size_type
size_t size_type
Definition: memory_pool.h:100
internal
Definition: _flow_graph_async_msg_impl.h:24
void
void
Definition: ittnotify_static.h:91
tbb::interface6::memory_pool_allocator::max_size
size_type max_size() const
Largest value for which method allocate might succeed.
Definition: memory_pool.h:126
tbb::interface6::memory_pool_allocator::rebind
Definition: memory_pool.h:102
tbb::interface6::memory_pool_allocator::address
const_pointer address(const_reference x) const
Definition: memory_pool.h:112
tbb::interface6::operator!=
bool operator!=(const memory_pool_allocator< T, P > &a, const memory_pool_allocator< U, P > &b)
Definition: memory_pool.h:183
tbb::interface6::fixed_pool
Definition: memory_pool.h:202
tbb::interface6::fixed_pool::my_buffer
void * my_buffer
Definition: memory_pool.h:203
tbb::internal::throw_exception
void throw_exception(exception_id eid)
Versionless convenience wrapper for throw_exception_v4()
Definition: tbb_exception.h:105
tbb::interface6::memory_pool::my_alloc
Alloc my_alloc
Definition: memory_pool.h:189
tbb::interface6::memory_pool::deallocate_request
static int deallocate_request(intptr_t pool_id, void *, size_t raw_bytes)
Definition: memory_pool.h:241
tbb::interface6::internal::pool_base::realloc
void * realloc(void *ptr, size_t size)
The "realloc" analogue complementing pool_malloc.
Definition: memory_pool.h:61
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator< U, P > &src)
Definition: memory_pool.h:109
tbb::interface6::memory_pool_allocator< void, P >::rebind::other
memory_pool_allocator< U, P > other
Definition: memory_pool.h:161
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::interface6::memory_pool_allocator::allocate
pointer allocate(size_type n, const void *=0)
Allocate space for n objects.
Definition: memory_pool.h:115
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator &src)
Definition: memory_pool.h:107
tbb::interface6::memory_pool_allocator
Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5.
Definition: memory_pool.h:84
tbb::interface6::memory_pool_allocator::value_type
tbb::internal::allocator_type< T >::value_type value_type
Definition: memory_pool.h:95
tbb::interface6::internal::pool_base::recycle
void recycle()
Reset pool to reuse its memory (free all objects at once)
Definition: memory_pool.h:51
__TBBMALLOC_ASSERT
#define __TBBMALLOC_ASSERT(a, b)
Definition: memory_pool.h:37
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator< U, P > &src)
Definition: memory_pool.h:167
size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
Definition: ittnotify_static.h:109
tbb::interface6::internal::pool_base::free
void free(void *ptr)
The "free" analogue to discard a previously allocated piece of memory.
Definition: memory_pool.h:57
tbb::interface6::internal::pool_base::destroy
void destroy()
destroy pool - must be called in a child class
Definition: memory_pool.h:67
tbb::interface6::fixed_pool::my_size
size_t my_size
Definition: memory_pool.h:204
tbb::interface6::memory_pool_allocator< void, P >::memory_pool_allocator
memory_pool_allocator(const memory_pool_allocator &src)
Definition: memory_pool.h:165
scalable_allocator.h
tbb::interface6::memory_pool_allocator::const_pointer
const typedef value_type * const_pointer
Definition: memory_pool.h:97
tbb::interface6::memory_pool_allocator::deallocate
void deallocate(pointer p, size_type)
Free previously allocated block of memory.
Definition: memory_pool.h:122
tbb::interface6::memory_pool_allocator::destroy
void destroy(pointer p)
Destroy value at location pointed to by p.
Definition: memory_pool.h:143
tbb::interface6::memory_pool_allocator::my_pool
pool_type * my_pool
Definition: memory_pool.h:87
tbb::interface6::memory_pool_allocator::construct
void construct(pointer p, const value_type &value)
Definition: memory_pool.h:139
tbb::internal::allocator_type::value_type
T value_type
Definition: tbb_stddef.h:472
tbb::interface6::memory_pool_allocator::const_reference
const typedef value_type & const_reference
Definition: memory_pool.h:99
tbb::interface6::memory_pool_allocator::operator==
friend bool operator==(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::operator==
bool operator==(const memory_pool_allocator< T, P > &a, const memory_pool_allocator< U, P > &b)
Definition: memory_pool.h:180
tbb::move
void move(tbb_thread &t1, tbb_thread &t2)
Definition: tbb_thread.h:319
tbb::interface6::memory_pool_allocator::difference_type
ptrdiff_t difference_type
Definition: memory_pool.h:101
tbb::interface6::memory_pool_allocator< void, P >::pool_type
P pool_type
Definition: memory_pool.h:156
tbb::interface6::internal::pool_base::my_pool
rml::MemoryPool * my_pool
Definition: memory_pool.h:69
tbb::interface6::memory_pool_allocator::rebind::other
memory_pool_allocator< U, P > other
Definition: memory_pool.h:103
tbb::interface6::memory_pool_allocator::construct
void construct(pointer p, value_type &&value)
Copy-construct value at location pointed to by p.
Definition: memory_pool.h:137
tbb::interface6::memory_pool_allocator::operator!=
friend bool operator!=(const memory_pool_allocator< V, R > &a, const memory_pool_allocator< U, R > &b)
tbb::interface6::memory_pool_allocator< void, P >::const_pointer
const typedef void * const_pointer
Definition: memory_pool.h:158
tbb::interface6::fixed_pool::fixed_pool
fixed_pool(void *buf, size_t size)
construct pool with underlying allocator
Definition: memory_pool.h:251
tbb::interface6::memory_pool_allocator::address
pointer address(reference x) const
Definition: memory_pool.h:111
tbb::internal::no_copy
Base class for types that should not be copied or assigned.
Definition: tbb_stddef.h:330
tbb::interface6::memory_pool_allocator::memory_pool_allocator
memory_pool_allocator(pool_type &pool)
Definition: memory_pool.h:106
__TBB_TRY
#define __TBB_TRY
Definition: tbb_stddef.h:283
tbb::interface6::memory_pool_allocator::pool_type
P pool_type
Definition: memory_pool.h:86
tbb::interface6::memory_pool_allocator< void, P >::pointer
void * pointer
Definition: memory_pool.h:157
tbb::interface6::memory_pool
Thread-safe growable pool allocator for variable-size requests.
Definition: memory_pool.h:188
tbb::internal::max
T max(const T &val1, const T &val2)
Utility template function returning greater of the two values.
Definition: tbb_misc.h:119
tbb::interface6::memory_pool::~memory_pool
~memory_pool()
destroy pool
Definition: memory_pool.h:198
value
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain __itt_id ITT_FORMAT p const __itt_domain __itt_id __itt_timestamp __itt_timestamp ITT_FORMAT lu const __itt_domain __itt_id __itt_id __itt_string_handle ITT_FORMAT p const __itt_domain ITT_FORMAT p const __itt_domain __itt_string_handle unsigned long long value
Definition: ittnotify_static.h:192
tbb::interface6::memory_pool::memory_pool
memory_pool(const Alloc &src=Alloc())
construct pool with underlying allocator
Definition: memory_pool.h:217
tbb::interface6::memory_pool_allocator< void, P >::value_type
void value_type
Definition: memory_pool.h:159
tbb::interface6::fixed_pool::~fixed_pool
~fixed_pool()
destroy pool
Definition: memory_pool.h:211
tbb::interface6::internal::pool_base
Base of thread-safe pool allocator for variable-size requests.
Definition: memory_pool.h:46
__TBB_CATCH
#define __TBB_CATCH(e)
Definition: tbb_stddef.h:284
p
void const char const char int ITT_FORMAT __itt_group_sync p
Definition: ittnotify_static.h:91
tbb::interface6::internal::pool_base::malloc
void * malloc(size_t size)
The "malloc" analogue to allocate block of memory of size bytes.
Definition: memory_pool.h:54
tbb::interface6::memory_pool::allocate_request
static void * allocate_request(intptr_t pool_id, size_t &bytes)
Definition: memory_pool.h:225
tbb::interface6::memory_pool_allocator::reference
value_type & reference
Definition: memory_pool.h:98
tbb::interface6::memory_pool_allocator< void, P >::my_pool
pool_type * my_pool
Definition: memory_pool.h:170
tbb::interface6::fixed_pool::allocate_request
static void * allocate_request(intptr_t pool_id, size_t &bytes)
Definition: memory_pool.h:260

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.