cache_aligned_allocator.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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_cache_aligned_allocator_H
  24. #define __TBB_cache_aligned_allocator_H
  25. #include <new>
  26. #include "tbb_stddef.h"
  27. #if __TBB_CPP11_RVALUE_REF_PRESENT && !__TBB_CPP11_STD_FORWARD_BROKEN
  28. #include <utility> // std::forward
  29. #endif
  30. namespace tbb {
  31. //! @cond INTERNAL
  32. namespace internal {
  33. //! Cache/sector line size.
  34. /** @ingroup memory_allocation */
  35. size_t __TBB_EXPORTED_FUNC NFS_GetLineSize();
  36. //! Allocate memory on cache/sector line boundary.
  37. /** @ingroup memory_allocation */
  38. void* __TBB_EXPORTED_FUNC NFS_Allocate( size_t n_element, size_t element_size, void* hint );
  39. //! Free memory allocated by NFS_Allocate.
  40. /** Freeing a NULL pointer is allowed, but has no effect.
  41. @ingroup memory_allocation */
  42. void __TBB_EXPORTED_FUNC NFS_Free( void* );
  43. }
  44. //! @endcond
  45. #if _MSC_VER && !defined(__INTEL_COMPILER)
  46. // Workaround for erroneous "unreferenced parameter" warning in method destroy.
  47. #pragma warning (push)
  48. #pragma warning (disable: 4100)
  49. #endif
  50. //! Meets "allocator" requirements of ISO C++ Standard, Section 20.1.5
  51. /** The members are ordered the same way they are in section 20.4.1
  52. of the ISO C++ standard.
  53. @ingroup memory_allocation */
  54. template<typename T>
  55. class cache_aligned_allocator {
  56. public:
  57. typedef typename internal::allocator_type<T>::value_type value_type;
  58. typedef value_type* pointer;
  59. typedef const value_type* const_pointer;
  60. typedef value_type& reference;
  61. typedef const value_type& const_reference;
  62. typedef size_t size_type;
  63. typedef ptrdiff_t difference_type;
  64. template<typename U> struct rebind {
  65. typedef cache_aligned_allocator<U> other;
  66. };
  67. cache_aligned_allocator() throw() {}
  68. cache_aligned_allocator( const cache_aligned_allocator& ) throw() {}
  69. template<typename U> cache_aligned_allocator(const cache_aligned_allocator<U>&) throw() {}
  70. pointer address(reference x) const {return &x;}
  71. const_pointer address(const_reference x) const {return &x;}
  72. //! Allocate space for n objects, starting on a cache/sector line.
  73. pointer allocate( size_type n, const void* hint=0 ) {
  74. // The "hint" argument is always ignored in NFS_Allocate thus const_cast shouldn't hurt
  75. return pointer(internal::NFS_Allocate( n, sizeof(value_type), const_cast<void*>(hint) ));
  76. }
  77. //! Free block of memory that starts on a cache line
  78. void deallocate( pointer p, size_type ) {
  79. internal::NFS_Free(p);
  80. }
  81. //! Largest value for which method allocate might succeed.
  82. size_type max_size() const throw() {
  83. return (~size_t(0)-internal::NFS_MaxLineSize)/sizeof(value_type);
  84. }
  85. //! Copy-construct value at location pointed to by p.
  86. #if __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  87. template<typename U, typename... Args>
  88. void construct(U *p, Args&&... args)
  89. #if __TBB_CPP11_STD_FORWARD_BROKEN
  90. { ::new((void *)p) U((args)...); }
  91. #else
  92. { ::new((void *)p) U(std::forward<Args>(args)...); }
  93. #endif
  94. #else // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  95. void construct( pointer p, const value_type& value ) {::new((void*)(p)) value_type(value);}
  96. #endif // __TBB_CPP11_VARIADIC_TEMPLATES_PRESENT && __TBB_CPP11_RVALUE_REF_PRESENT
  97. //! Destroy value at location pointed to by p.
  98. void destroy( pointer p ) {p->~value_type();}
  99. };
  100. #if _MSC_VER && !defined(__INTEL_COMPILER)
  101. #pragma warning (pop)
  102. #endif // warning 4100 is back
  103. //! Analogous to std::allocator<void>, as defined in ISO C++ Standard, Section 20.4.1
  104. /** @ingroup memory_allocation */
  105. template<>
  106. class cache_aligned_allocator<void> {
  107. public:
  108. typedef void* pointer;
  109. typedef const void* const_pointer;
  110. typedef void value_type;
  111. template<typename U> struct rebind {
  112. typedef cache_aligned_allocator<U> other;
  113. };
  114. };
  115. template<typename T, typename U>
  116. inline bool operator==( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return true;}
  117. template<typename T, typename U>
  118. inline bool operator!=( const cache_aligned_allocator<T>&, const cache_aligned_allocator<U>& ) {return false;}
  119. } // namespace tbb
  120. #endif /* __TBB_cache_aligned_allocator_H */