scalable_allocator.h 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. Copyright 2005-2013 Intel Corporation. All Rights Reserved.
  3. This file is part of Threading Building Blocks.
  4. Threading Building Blocks is free software; you can redistribute it
  5. and/or modify it under the terms of the GNU General Public License
  6. version 2 as published by the Free Software Foundation.
  7. Threading Building Blocks is distributed in the hope that it will be
  8. useful, but WITHOUT ANY WARRANTY; without even the implied warranty
  9. of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with Threading Building Blocks; if not, write to the Free Software
  13. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  14. As a special exception, you may use this file as part of a free software
  15. library without restriction. Specifically, if other files instantiate
  16. templates or use macros or inline functions from this file, or you compile
  17. this file and link it with other files to produce an executable, this
  18. file does not by itself cause the resulting executable to be covered by
  19. the GNU General Public License. This exception does not however
  20. invalidate any other reasons why the executable file might be covered by
  21. the GNU General Public License.
  22. */
  23. #ifndef __TBB_scalable_allocator_H
  24. #define __TBB_scalable_allocator_H
  25. /** @file */
  26. #include <stddef.h> /* Need ptrdiff_t and size_t from here. */
  27. #if !_MSC_VER
  28. #include <stdint.h> /* Need intptr_t from here. */
  29. #endif
  30. #if !defined(__cplusplus) && __ICC==1100
  31. #pragma warning (push)
  32. #pragma warning (disable: 991)
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. #if _MSC_VER >= 1400
  38. #define __TBB_EXPORTED_FUNC __cdecl
  39. #else
  40. #define __TBB_EXPORTED_FUNC
  41. #endif
  42. /** The "malloc" analogue to allocate block of memory of size bytes.
  43. * @ingroup memory_allocation */
  44. void * __TBB_EXPORTED_FUNC scalable_malloc (size_t size);
  45. /** The "free" analogue to discard a previously allocated piece of memory.
  46. @ingroup memory_allocation */
  47. void __TBB_EXPORTED_FUNC scalable_free (void* ptr);
  48. /** The "realloc" analogue complementing scalable_malloc.
  49. @ingroup memory_allocation */
  50. void * __TBB_EXPORTED_FUNC scalable_realloc (void* ptr, size_t size);
  51. /** The "calloc" analogue complementing scalable_malloc.
  52. @ingroup memory_allocation */
  53. void * __TBB_EXPORTED_FUNC scalable_calloc (size_t nobj, size_t size);
  54. /** The "posix_memalign" analogue.
  55. @ingroup memory_allocation */
  56. int __TBB_EXPORTED_FUNC scalable_posix_memalign (void** memptr, size_t alignment, size_t size);
  57. /** The "_aligned_malloc" analogue.
  58. @ingroup memory_allocation */
  59. void * __TBB_EXPORTED_FUNC scalable_aligned_malloc (size_t size, size_t alignment);
  60. /** The "_aligned_realloc" analogue.
  61. @ingroup memory_allocation */
  62. void * __TBB_EXPORTED_FUNC scalable_aligned_realloc (void* ptr, size_t size, size_t alignment);
  63. /** The "_aligned_free" analogue.
  64. @ingroup memory_allocation */
  65. void __TBB_EXPORTED_FUNC scalable_aligned_free (void* ptr);
  66. /** The analogue of _msize/malloc_size/malloc_usable_size.
  67. Returns the usable size of a memory block previously allocated by scalable_*,
  68. or 0 (zero) if ptr does not point to such a block.
  69. @ingroup memory_allocation */
  70. size_t __TBB_EXPORTED_FUNC scalable_msize (void* ptr);
  71. /* Setting TBB_MALLOC_USE_HUGE_PAGES environment variable to 1 enables huge pages.
  72. scalable_allocation_mode call has priority over environment variable. */
  73. enum AllocationModeParam {
  74. USE_HUGE_PAGES /* value turns using huge pages on and off */
  75. };
  76. /** Set TBB allocator-specific allocation modes.
  77. @ingroup memory_allocation */
  78. int __TBB_EXPORTED_FUNC scalable_allocation_mode(int param, intptr_t value);
  79. #ifdef __cplusplus
  80. } /* extern "C" */
  81. #endif /* __cplusplus */
  82. #ifdef __cplusplus
  83. namespace rml {
  84. class MemoryPool;
  85. typedef void *(*rawAllocType)(intptr_t pool_id, size_t &bytes);
  86. typedef int (*rawFreeType)(intptr_t pool_id, void* raw_ptr, size_t raw_bytes);
  87. /*
  88. MemPoolPolicy extension must be compatible with such structure fields layout
  89. struct MemPoolPolicy {
  90. rawAllocType pAlloc;
  91. rawFreeType pFree;
  92. size_t granularity; // granularity of pAlloc allocations
  93. };
  94. */
  95. struct MemPoolPolicy {
  96. enum {
  97. TBBMALLOC_POOL_VERSION = 1
  98. };
  99. rawAllocType pAlloc;
  100. rawFreeType pFree;
  101. // granularity of pAlloc allocations. 0 means default used.
  102. size_t granularity;
  103. int version;
  104. // all memory consumed at 1st pAlloc call and never returned,
  105. // no more pAlloc calls after 1st
  106. unsigned fixedPool : 1,
  107. // memory consumed but returned only at pool termination
  108. keepAllMemory : 1,
  109. reserved : 30;
  110. MemPoolPolicy(rawAllocType pAlloc_, rawFreeType pFree_,
  111. size_t granularity_ = 0, bool fixedPool_ = false,
  112. bool keepAllMemory_ = false) :
  113. pAlloc(pAlloc_), pFree(pFree_), granularity(granularity_), version(TBBMALLOC_POOL_VERSION),
  114. fixedPool(fixedPool_), keepAllMemory(keepAllMemory_),
  115. reserved(0) {}
  116. };
  117. enum MemPoolError {
  118. POOL_OK, // pool created successfully
  119. INVALID_POLICY, // invalid policy parameters found
  120. UNSUPPORTED_POLICY, // requested pool policy is not supported by allocator library
  121. NO_MEMORY // lack of memory during pool creation
  122. };
  123. MemPoolError pool_create_v1(intptr_t pool_id, const MemPoolPolicy *policy,
  124. rml::MemoryPool **pool);
  125. bool pool_destroy(MemoryPool* memPool);
  126. void *pool_malloc(MemoryPool* memPool, size_t size);
  127. void *pool_realloc(MemoryPool* memPool, void *object, size_t size);
  128. void *pool_aligned_malloc(MemoryPool* mPool, size_t size, size_t alignment);
  129. void *pool_aligned_realloc(MemoryPool* mPool, void *ptr, size_t size, size_t alignment);
  130. bool pool_reset(MemoryPool* memPool);
  131. bool pool_free(MemoryPool *memPool, void *object);
  132. }
  133. #include <new> /* To use new with the placement argument */
  134. /* Ensure that including this header does not cause implicit linkage with TBB */
  135. #ifndef __TBB_NO_IMPLICIT_LINKAGE
  136. #define __TBB_NO_IMPLICIT_LINKAGE 1
  137. #include "tbb_stddef.h"
  138. #undef __TBB_NO_IMPLICIT_LINKAGE
  139. #else
  140. #include "tbb_stddef.h"
  141. #endif
  142. #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
  143. #include <utility> // std::forward
  144. #endif
  145. namespace tbb {
  146. #if _MSC_VER && !defined(__INTEL_COMPILER)
  147. // Workaround for erroneous "unreferenced parameter" warning in method destroy.
  148. #pragma warning (push)
  149. #pragma warning (disable: 4100)
  150. #endif
  151. //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
  152. /** The members are ordered the same way they are in section 20.4.1
  153. of the ISO C++ standard.
  154. @ingroup memory_allocation */
  155. template<typename T>
  156. class scalable_allocator {
  157. public:
  158. typedef typename internal::allocator_type<T>::value_type value_type;
  159. typedef value_type* pointer;
  160. typedef const value_type* const_pointer;
  161. typedef value_type& reference;
  162. typedef const value_type& const_reference;
  163. typedef size_t size_type;
  164. typedef ptrdiff_t difference_type;
  165. template<class U> struct rebind {
  166. typedef scalable_allocator<U> other;
  167. };
  168. scalable_allocator() throw() {}
  169. scalable_allocator( const scalable_allocator& ) throw() {}
  170. template<typename U> scalable_allocator(const scalable_allocator<U>&) throw() {}
  171. pointer address(reference x) const {return &x;}
  172. const_pointer address(const_reference x) const {return &x;}
  173. //! Allocate space for n objects.
  174. pointer allocate( size_type n, const void* /*hint*/ =0 ) {
  175. return static_cast<pointer>( scalable_malloc( n * sizeof(value_type) ) );
  176. }
  177. //! Free previously allocated block of memory
  178. void deallocate( pointer p, size_type ) {
  179. scalable_free( p );
  180. }
  181. //! Largest value for which method allocate might succeed.
  182. size_type max_size() const throw() {
  183. size_type absolutemax = static_cast<size_type>(-1) / sizeof (value_type);
  184. return (absolutemax > 0 ? absolutemax : 1);
  185. }
  186. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  187. template<typename U, typename... Args>
  188. void construct(U *p, Args&&... args)
  189. #if __TBB_CPP11_STD_FORWARD_BROKEN
  190. { ::new((void *)p) U((args)...); }
  191. #else
  192. { ::new((void *)p) U(std::forward<Args>(args)...); }
  193. #endif
  194. #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  195. void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
  196. #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  197. void destroy( pointer p ) {p->~value_type();}
  198. };
  199. #if _MSC_VER && !defined(__INTEL_COMPILER)
  200. #pragma warning (pop)
  201. #endif // warning 4100 is back
  202. //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
  203. /** @ingroup memory_allocation */
  204. template<>
  205. class scalable_allocator<void> {
  206. public:
  207. typedef void* pointer;
  208. typedef const void* const_pointer;
  209. typedef void value_type;
  210. template<class U> struct rebind {
  211. typedef scalable_allocator<U> other;
  212. };
  213. };
  214. template<typename T, typename U>
  215. inline bool operator==( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return true;}
  216. template<typename T, typename U>
  217. inline bool operator!=( const scalable_allocator<T>&, const scalable_allocator<U>& ) {return false;}
  218. } // namespace tbb
  219. #if _MSC_VER
  220. #if (__TBB_BUILD || __TBBMALLOC_BUILD) && !defined(__TBBMALLOC_NO_IMPLICIT_LINKAGE)
  221. #define __TBBMALLOC_NO_IMPLICIT_LINKAGE 1
  222. #endif
  223. #if !__TBBMALLOC_NO_IMPLICIT_LINKAGE
  224. #ifdef _DEBUG
  225. #pragma comment(lib, "tbbmalloc_debug.lib")
  226. #else
  227. #pragma comment(lib, "tbbmalloc.lib")
  228. #endif
  229. #endif
  230. #endif
  231. #endif /* __cplusplus */
  232. #if !defined(__cplusplus) && __ICC==1100
  233. #pragma warning (pop)
  234. #endif // ICC 11.0 warning 991 is back
  235. #endif /* __TBB_scalable_allocator_H */