scalable_allocator.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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_scalable_allocator_H
  14. #define __TBB_scalable_allocator_H
  15. /** @file */
  16. #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
  17. #if !_MSC_VER
  18. #include <stdint.h> /* Need intptr_t from here. */
  19. #endif
  20. #if !defined(__cplusplus) && __ICC==1100
  21. #pragma warning (push)
  22. #pragma warning (disable: 991)
  23. #endif
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */
  27. #if _MSC_VER >= 1400
  28. #define __TBB_EXPORTED_FUNC __cdecl
  29. #else
  30. #define __TBB_EXPORTED_FUNC
  31. #endif
  32. /** The "malloc" analogue to allocate block of memory of size bytes.
  33. * @ingroup memory_allocation */
  34. void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
  35. /** The "free" analogue to discard a previously allocated piece of memory.
  36. @ingroup memory_allocation */
  37. void __TBB_EXPORTED_FUNC scalable_free (void* ptr);
  38. /** The "realloc" analogue complementing scalable_malloc.
  39. @ingroup memory_allocation */
  40. void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
  41. /** The "calloc" analogue complementing scalable_malloc.
  42. @ingroup memory_allocation */
  43. void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
  44. /** The "posix_memalign" analogue.
  45. @ingroup memory_allocation */
  46. int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
  47. /** The "_aligned_malloc" analogue.
  48. @ingroup memory_allocation */
  49. void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
  50. /** The "_aligned_realloc" analogue.
  51. @ingroup memory_allocation */
  52. void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
  53. /** The "_aligned_free" analogue.
  54. @ingroup memory_allocation */
  55. void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
  56. /** The analogue of _msize/malloc_size/malloc_usable_size.
  57. Returns the usable size of a memory block previously allocated by scalable_*,
  58. or 0 (zero) if ptr does not point to such a block.
  59. @ingroup memory_allocation */
  60. size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
  61. /* Results for scalable_allocation_* functions */
  62. typedef enum {
  63. TBBMALLOC_OK,
  64. TBBMALLOC_INVALID_PARAM,
  65. TBBMALLOC_UNSUPPORTED,
  66. TBBMALLOC_NO_MEMORY,
  67. TBBMALLOC_NO_EFFECT
  68. } ScalableAllocationResult;
  69. /* Setting TBB_MALLOC_USE_HUGE_PAGES environment variable to 1 enables huge pages.
  70. scalable_allocation_mode call has priority over environment variable. */
  71. typedef enum {
  72. TBBMALLOC_USE_HUGE_PAGES, /* value turns using huge pages on and off */
  73. /* deprecated, kept for backward compatibility only */
  74. USE_HUGE_PAGES = TBBMALLOC_USE_HUGE_PAGES,
  75. /* try to limit memory consumption value (Bytes), clean internal buffers
  76. if limit is exceeded, but not prevents from requesting memory from OS */
  77. TBBMALLOC_SET_SOFT_HEAP_LIMIT,
  78. /* Lower bound for the size (Bytes), that is interpreted as huge
  79. * and not released during regular cleanup operations. */
  80. TBBMALLOC_SET_HUGE_SIZE_THRESHOLD
  81. } AllocationModeParam;
  82. /** Set TBB allocator-specific allocation modes.
  83. @ingroup memory_allocation */
  84. int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value);
  85. typedef enum {
  86. /* Clean internal allocator buffers for all threads.
  87. Returns TBBMALLOC_NO_EFFECT if no buffers cleaned,
  88. TBBMALLOC_OK if some memory released from buffers. */
  89. TBBMALLOC_CLEAN_ALL_BUFFERS,
  90. /* Clean internal allocator buffer for current thread only.
  91. Return values same as for TBBMALLOC_CLEAN_ALL_BUFFERS. */
  92. TBBMALLOC_CLEAN_THREAD_BUFFERS
  93. } ScalableAllocationCmd;
  94. /** Call TBB allocator-specific commands.
  95. @ingroup memory_allocation */
  96. int __TBB_EXPORTED_FUNC scalable_allocation_command(int cmd, void *param);
  97. #ifdef __cplusplus
  98. } /* extern "C" */
  99. #endif /* __cplusplus */
  100. #ifdef __cplusplus
  101. //! The namespace rml contains components of low-level memory pool interface.
  102. namespace rml {
  103. class MemoryPool;
  104. typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
  105. // returns non-zero in case of error
  106. typedef int (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
  107. /*
  108. MemPoolPolicy extension must be compatible with such structure fields layout
  109. struct MemPoolPolicy {
  110. rawAllocType pAlloc;
  111. rawFreeType pFree;
  112. size_t granularity; // granularity of pAlloc allocations
  113. };
  114. */
  115. struct MemPoolPolicy {
  116. enum {
  117. TBBMALLOC_POOL_VERSION = 1
  118. };
  119. rawAllocType pAlloc;
  120. rawFreeType pFree;
  121. // granularity of pAlloc allocations. 0 means default used.
  122. size_t granularity;
  123. int version;
  124. // all memory consumed at 1st pAlloc call and never returned,
  125. // no more pAlloc calls after 1st
  126. unsigned fixedPool : 1,
  127. // memory consumed but returned only at pool termination
  128. keepAllMemory : 1,
  129. reserved : 30;
  130. MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
  131. size_t granularity_ = 0, bool fixedPool_ = false,
  132. bool keepAllMemory_ = false) :
  133. pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(TBBMALLOC_POOL_VERSION),
  134. fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
  135. reserved(0) {}
  136. };
  137. // enums have same values as appropriate enums from ScalableAllocationResult
  138. // TODO: use ScalableAllocationResult in pool_create directly
  139. enum MemPoolError {
  140. // pool created successfully
  141. POOL_OK = TBBMALLOC_OK,
  142. // invalid policy parameters found
  143. INVALID_POLICY = TBBMALLOC_INVALID_PARAM,
  144. // requested pool policy is not supported by allocator library
  145. UNSUPPORTED_POLICY = TBBMALLOC_UNSUPPORTED,
  146. // lack of memory during pool creation
  147. NO_MEMORY = TBBMALLOC_NO_MEMORY,
  148. // action takes no effect
  149. NO_EFFECT = TBBMALLOC_NO_EFFECT
  150. };
  151. MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
  152. rml::MemoryPool **pool);
  153. bool pool_destroy(MemoryPool* memPool);
  154. void *pool_malloc(MemoryPool* memPool, size_t size);
  155. void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
  156. void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
  157. void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
  158. bool pool_reset(MemoryPool* memPool);
  159. bool pool_free(MemoryPool *memPool, void *object);
  160. MemoryPool *pool_identify(void *object);
  161. size_t pool_msize(MemoryPool *memPool, void *object);
  162. } // namespace rml
  163. #include <new> /* To use new with the placement argument */
  164. /* Ensure that including this header does not cause implicit linkage with TBB */
  165. #ifndef __TBB_NO_IMPLICIT_LINKAGE
  166. #define __TBB_NO_IMPLICIT_LINKAGE 1
  167. #include "tbb_stddef.h"
  168. #undef __TBB_NO_IMPLICIT_LINKAGE
  169. #else
  170. #include "tbb_stddef.h"
  171. #endif
  172. #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  173. #include <utility> // std::forward
  174. #endif
  175. #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
  176. #include <memory_resource>
  177. #endif
  178. namespace tbb {
  179. #if _MSC_VER && !defined(__INTEL_COMPILER)
  180. // Workaround for erroneous "unreferenced parameter" warning in method destroy.
  181. #pragma warning (push)
  182. #pragma warning (disable: 4100)
  183. #endif
  184. //! @cond INTERNAL
  185. namespace internal {
  186. #if TBB_USE_EXCEPTIONS
  187. // forward declaration is for inlining prevention
  188. template<typename E> __TBB_NOINLINE( void throw_exception(const E &e) );
  189. #endif
  190. // keep throw in a separate function to prevent code bloat
  191. template<typename E>
  192. void throw_exception(const E &e) {
  193. __TBB_THROW(e);
  194. }
  195. } // namespace internal
  196. //! @endcond
  197. //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
  198. /** The members are ordered the same way they are in section 20.4.1
  199. of the ISO C++ standard.
  200. @ingroup memory_allocation */
  201. template<typename T>
  202. class scalable_allocator {
  203. public:
  204. typedef typename internal::allocator_type<T>::value_type value_type;
  205. typedef value_type* pointer;
  206. typedef const value_type* const_pointer;
  207. typedef value_type& reference;
  208. typedef const value_type& const_reference;
  209. typedef size_t size_type;
  210. typedef ptrdiff_t difference_type;
  211. template<class U> struct rebind {
  212. typedef scalable_allocator<U> other;
  213. };
  214. scalable_allocator() throw() {}
  215. scalable_allocator( const scalable_allocator& ) throw() {}
  216. template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
  217. pointer address(reference x) const {return &x;}
  218. const_pointer address(const_reference x) const {return &x;}
  219. //! Allocate space for n objects.
  220. pointer allocate( size_type n, const void* /*hint*/ =0 ) {
  221. pointer p = static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
  222. if (!p)
  223. internal::throw_exception(std::bad_alloc());
  224. return p;
  225. }
  226. //! Free previously allocated block of memory
  227. void deallocate( pointer p, size_type ) {
  228. scalable_free( p );
  229. }
  230. //! Largest value for which method allocate might succeed.
  231. size_type max_size() const throw() {
  232. size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
  233. return (absolutemax > 0 ? absolutemax : 1);
  234. }
  235. #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  236. template<typename U, typename... Args>
  237. void construct(U *p, Args&&... args)
  238. { ::new((void *)p) U(std::forward<Args>(args)...); }
  239. #else /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
  240. #if __TBB_CPP11_RVALUE_REF_PRESENT
  241. void construct( pointer p, value_type&& value ) { ::new((void*)(p)) value_type( std::move( value ) ); }
  242. #endif
  243. void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
  244. #endif /* __TBB_ALLOCATOR_CONSTRUCT_VARIADIC */
  245. void destroy( pointer p ) {p->~value_type();}
  246. };
  247. #if _MSC_VER && !defined(__INTEL_COMPILER)
  248. #pragma warning (pop)
  249. #endif /* warning 4100 is back */
  250. //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
  251. /** @ingroup memory_allocation */
  252. template<>
  253. class scalable_allocator<void> {
  254. public:
  255. typedef void* pointer;
  256. typedef const void* const_pointer;
  257. typedef void value_type;
  258. template<class U> struct rebind {
  259. typedef scalable_allocator<U> other;
  260. };
  261. };
  262. template<typename T, typename U>
  263. inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
  264. template<typename T, typename U>
  265. inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
  266. #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
  267. namespace internal {
  268. //! C++17 memory resource implementation for scalable allocator
  269. //! ISO C++ Section 23.12.2
  270. class scalable_resource_impl : public std::pmr::memory_resource {
  271. private:
  272. void* do_allocate(size_t bytes, size_t alignment) override {
  273. void* ptr = scalable_aligned_malloc( bytes, alignment );
  274. if (!ptr) {
  275. throw_exception(std::bad_alloc());
  276. }
  277. return ptr;
  278. }
  279. void do_deallocate(void* ptr, size_t /*bytes*/, size_t /*alignment*/) override {
  280. scalable_free(ptr);
  281. }
  282. //! Memory allocated by one instance of scalable_resource_impl could be deallocated by any
  283. //! other instance of this class
  284. bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
  285. return this == &other ||
  286. #if __TBB_USE_OPTIONAL_RTTI
  287. dynamic_cast<const scalable_resource_impl*>(&other) != NULL;
  288. #else
  289. false;
  290. #endif
  291. }
  292. };
  293. } // namespace internal
  294. //! Global scalable allocator memory resource provider
  295. inline std::pmr::memory_resource* scalable_memory_resource() noexcept {
  296. static tbb::internal::scalable_resource_impl scalable_res;
  297. return &scalable_res;
  298. }
  299. #endif /* __TBB_CPP17_MEMORY_RESOURCE_PRESENT */
  300. } // namespace tbb
  301. #if _MSC_VER
  302. #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
  303. #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
  304. #endif
  305. #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
  306. #ifdef _DEBUG
  307. #pragma comment(lib, "tbbmalloc_debug.lib")
  308. #else
  309. #pragma comment(lib, "tbbmalloc.lib")
  310. #endif
  311. #endif
  312. #endif
  313. #endif /* __cplusplus */
  314. #if !defined(__cplusplus) && __ICC==1100
  315. #pragma warning (pop)
  316. #endif /* ICC 11.0 warning 991 is back */
  317. #endif /* __TBB_scalable_allocator_H */