2
0

cache_aligned_allocator.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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_cache_aligned_allocator_H
  14. #define __TBB_cache_aligned_allocator_H
  15. #include <new>
  16. #include "tbb_stddef.h"
  17. #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  18. #include <utility> // std::forward
  19. #endif
  20. #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
  21. #include <memory_resource>
  22. #endif
  23. namespace tbb {
  24. //! @cond INTERNAL
  25. namespace internal {
  26. //! Cache/sector line size.
  27. /** @ingroup memory_allocation */
  28. size_t __TBB_EXPORTED_FUNC NFS_GetLineSize();
  29. //! Allocate memory on cache/sector line boundary.
  30. /** @ingroup memory_allocation */
  31. void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
  32. //! Free memory allocated by NFS_Allocate.
  33. /** Freeing a NULL pointer is allowed, but has no effect.
  34. @ingroup memory_allocation */
  35. void __TBB_EXPORTED_FUNC NFS_Free( void* );
  36. }
  37. //! @endcond
  38. #if _MSC_VER && !defined(__INTEL_COMPILER)
  39. // Workaround for erroneous "unreferenced parameter" warning in method destroy.
  40. #pragma warning (push)
  41. #pragma warning (disable: 4100)
  42. #endif
  43. //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
  44. /** The members are ordered the same way they are in section 20.4.1
  45. of the ISO C++ standard.
  46. @ingroup memory_allocation */
  47. template<typename T>
  48. class cache_aligned_allocator {
  49. public:
  50. typedef typename internal::allocator_type<T>::value_type value_type;
  51. typedef value_type* pointer;
  52. typedef const value_type* const_pointer;
  53. typedef value_type& reference;
  54. typedef const value_type& const_reference;
  55. typedef size_t size_type;
  56. typedef ptrdiff_t difference_type;
  57. template<typename U> struct rebind {
  58. typedef cache_aligned_allocator<U> other;
  59. };
  60. cache_aligned_allocator() throw() {}
  61. cache_aligned_allocator( const cache_aligned_allocator& ) throw() {}
  62. template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
  63. pointer address(reference x) const {return &x;}
  64. const_pointer address(const_reference x) const {return &x;}
  65. //! Allocate space for n objects, starting on a cache/sector line.
  66. pointer allocate( size_type n, const void* hint=0 ) {
  67. // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
  68. return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
  69. }
  70. //! Free block of memory that starts on a cache line
  71. void deallocate( pointer p, size_type ) {
  72. internal::NFS_Free(p);
  73. }
  74. //! Largest value for which method allocate might succeed.
  75. size_type max_size() const throw() {
  76. return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
  77. }
  78. //! Copy-construct value at location pointed to by p.
  79. #if __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  80. template<typename U, typename... Args>
  81. void construct(U *p, Args&&... args)
  82. { ::new((void *)p) U(std::forward<Args>(args)...); }
  83. #else // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  84. #if __TBB_CPP11_RVALUE_REF_PRESENT
  85. void construct( pointer p, value_type&& value ) {::new((void*)(p)) value_type(std::move(value));}
  86. #endif
  87. void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
  88. #endif // __TBB_ALLOCATOR_CONSTRUCT_VARIADIC
  89. //! Destroy value at location pointed to by p.
  90. void destroy( pointer p ) {p->~value_type();}
  91. };
  92. #if _MSC_VER && !defined(__INTEL_COMPILER)
  93. #pragma warning (pop)
  94. #endif // warning 4100 is back
  95. //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
  96. /** @ingroup memory_allocation */
  97. template<>
  98. class cache_aligned_allocator<void> {
  99. public:
  100. typedef void* pointer;
  101. typedef const void* const_pointer;
  102. typedef void value_type;
  103. template<typename U> struct rebind {
  104. typedef cache_aligned_allocator<U> other;
  105. };
  106. };
  107. template<typename T, typename U>
  108. inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
  109. template<typename T, typename U>
  110. inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
  111. #if __TBB_CPP17_MEMORY_RESOURCE_PRESENT
  112. //! C++17 memory resource wrapper to ensure cache line size alignment
  113. class cache_aligned_resource : public std::pmr::memory_resource {
  114. public:
  115. cache_aligned_resource() : cache_aligned_resource(std::pmr::get_default_resource()) {}
  116. explicit cache_aligned_resource(std::pmr::memory_resource* upstream) : m_upstream(upstream) {}
  117. std::pmr::memory_resource* upstream_resource() const {
  118. return m_upstream;
  119. }
  120. private:
  121. //! We don't know what memory resource set. Use padding to guarantee alignment
  122. void* do_allocate(size_t bytes, size_t alignment) override {
  123. size_t cache_line_alignment = correct_alignment(alignment);
  124. uintptr_t base = (uintptr_t)m_upstream->allocate(correct_size(bytes) + cache_line_alignment);
  125. __TBB_ASSERT(base != 0, "Upstream resource returned NULL.");
  126. #if _MSC_VER && !defined(__INTEL_COMPILER)
  127. // unary minus operator applied to unsigned type, result still unsigned
  128. #pragma warning(push)
  129. #pragma warning(disable: 4146 4706)
  130. #endif
  131. // Round up to the next cache line (align the base address)
  132. uintptr_t result = (base + cache_line_alignment) & -cache_line_alignment;
  133. #if _MSC_VER && !defined(__INTEL_COMPILER)
  134. #pragma warning(pop)
  135. #endif
  136. // Record where block actually starts.
  137. ((uintptr_t*)result)[-1] = base;
  138. return (void*)result;
  139. }
  140. void do_deallocate(void* ptr, size_t bytes, size_t alignment) override {
  141. if (ptr) {
  142. // Recover where block actually starts
  143. uintptr_t base = ((uintptr_t*)ptr)[-1];
  144. m_upstream->deallocate((void*)base, correct_size(bytes) + correct_alignment(alignment));
  145. }
  146. }
  147. bool do_is_equal(const std::pmr::memory_resource& other) const noexcept override {
  148. if (this == &other) { return true; }
  149. #if __TBB_USE_OPTIONAL_RTTI
  150. const cache_aligned_resource* other_res = dynamic_cast<const cache_aligned_resource*>(&other);
  151. return other_res && (this->upstream_resource() == other_res->upstream_resource());
  152. #else
  153. return false;
  154. #endif
  155. }
  156. size_t correct_alignment(size_t alignment) {
  157. __TBB_ASSERT(tbb::internal::is_power_of_two(alignment), "Alignment is not a power of 2");
  158. #if __TBB_CPP17_HW_INTERFERENCE_SIZE_PRESENT
  159. size_t cache_line_size = std::hardware_destructive_interference_size;
  160. #else
  161. size_t cache_line_size = internal::NFS_GetLineSize();
  162. #endif
  163. return alignment < cache_line_size ? cache_line_size : alignment;
  164. }
  165. size_t correct_size(size_t bytes) {
  166. // To handle the case, when small size requested. There could be not
  167. // enough space to store the original pointer.
  168. return bytes < sizeof(uintptr_t) ? sizeof(uintptr_t) : bytes;
  169. }
  170. std::pmr::memory_resource* m_upstream;
  171. };
  172. #endif /* __TBB_CPP17_MEMORY_RESOURCE_PRESENT */
  173. } // namespace tbb
  174. #endif /* __TBB_cache_aligned_allocator_H */