memory_pool.h 10 KB

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