aligned_allocator_adaptor.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. Copyright 2014-2016 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_ADAPTOR_HPP
  8. #define BOOST_ALIGN_ALIGNED_ALLOCATOR_ADAPTOR_HPP
  9. #include <boost/align/detail/is_alignment_constant.hpp>
  10. #include <boost/align/detail/max_align.hpp>
  11. #include <boost/align/detail/max_size.hpp>
  12. #include <boost/align/align.hpp>
  13. #include <boost/align/aligned_allocator_adaptor_forward.hpp>
  14. #include <boost/align/alignment_of.hpp>
  15. #include <boost/core/pointer_traits.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <new>
  18. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  19. #include <memory>
  20. #endif
  21. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  22. #include <utility>
  23. #endif
  24. namespace boost {
  25. namespace alignment {
  26. template<class Allocator, std::size_t Alignment>
  27. class aligned_allocator_adaptor
  28. : public Allocator {
  29. BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
  30. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  31. typedef std::allocator_traits<Allocator> traits;
  32. typedef typename traits::template rebind_alloc<char> char_alloc;
  33. typedef typename traits::template rebind_traits<char> char_traits;
  34. typedef typename char_traits::pointer char_ptr;
  35. #else
  36. typedef typename Allocator::template rebind<char>::other char_alloc;
  37. typedef typename char_alloc::pointer char_ptr;
  38. #endif
  39. public:
  40. typedef typename Allocator::value_type value_type;
  41. typedef value_type* pointer;
  42. typedef const value_type* const_pointer;
  43. typedef void* void_pointer;
  44. typedef const void* const_void_pointer;
  45. typedef std::size_t size_type;
  46. typedef std::ptrdiff_t difference_type;
  47. private:
  48. enum {
  49. min_align = detail::max_size<Alignment,
  50. detail::max_align<value_type, char_ptr>::value>::value
  51. };
  52. public:
  53. template<class U>
  54. struct rebind {
  55. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  56. typedef aligned_allocator_adaptor<typename traits::template
  57. rebind_alloc<U>, Alignment> other;
  58. #else
  59. typedef aligned_allocator_adaptor<typename Allocator::template
  60. rebind<U>::other, Alignment> other;
  61. #endif
  62. };
  63. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  64. aligned_allocator_adaptor() = default;
  65. #else
  66. aligned_allocator_adaptor()
  67. : Allocator() { }
  68. #endif
  69. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  70. template<class A>
  71. explicit aligned_allocator_adaptor(A&& alloc) BOOST_NOEXCEPT
  72. : Allocator(std::forward<A>(alloc)) { }
  73. #else
  74. template<class A>
  75. explicit aligned_allocator_adaptor(const A& alloc) BOOST_NOEXCEPT
  76. : Allocator(alloc) { }
  77. #endif
  78. template<class U>
  79. aligned_allocator_adaptor(const aligned_allocator_adaptor<U,
  80. Alignment>& other) BOOST_NOEXCEPT
  81. : Allocator(other.base()) { }
  82. Allocator& base() BOOST_NOEXCEPT {
  83. return static_cast<Allocator&>(*this);
  84. }
  85. const Allocator& base() const BOOST_NOEXCEPT {
  86. return static_cast<const Allocator&>(*this);
  87. }
  88. pointer allocate(size_type size) {
  89. std::size_t s = size * sizeof(value_type);
  90. std::size_t n = s + min_align - 1;
  91. char_alloc a(base());
  92. char_ptr p = a.allocate(sizeof p + n);
  93. void* r = boost::to_address(p) + sizeof p;
  94. (void)align(min_align, s, r, n);
  95. ::new(static_cast<void*>(static_cast<char_ptr*>(r) - 1)) char_ptr(p);
  96. return static_cast<pointer>(r);
  97. }
  98. pointer allocate(size_type size, const_void_pointer hint) {
  99. std::size_t s = size * sizeof(value_type);
  100. std::size_t n = s + min_align - 1;
  101. char_ptr h = char_ptr();
  102. if (hint) {
  103. h = *(static_cast<const char_ptr*>(hint) - 1);
  104. }
  105. char_alloc a(base());
  106. #if !defined(BOOST_NO_CXX11_ALLOCATOR)
  107. char_ptr p = char_traits::allocate(a, sizeof p + n, h);
  108. #else
  109. char_ptr p = a.allocate(sizeof p + n, h);
  110. #endif
  111. void* r = boost::to_address(p) + sizeof p;
  112. (void)align(min_align, s, r, n);
  113. ::new(static_cast<void*>(static_cast<char_ptr*>(r) - 1)) char_ptr(p);
  114. return static_cast<pointer>(r);
  115. }
  116. void deallocate(pointer ptr, size_type size) {
  117. char_ptr* p = reinterpret_cast<char_ptr*>(ptr) - 1;
  118. char_ptr r = *p;
  119. p->~char_ptr();
  120. char_alloc a(base());
  121. a.deallocate(r, sizeof r + size * sizeof(value_type) + min_align - 1);
  122. }
  123. };
  124. template<class A, class B, std::size_t Alignment>
  125. inline bool
  126. operator==(const aligned_allocator_adaptor<A, Alignment>& a,
  127. const aligned_allocator_adaptor<B, Alignment>& b) BOOST_NOEXCEPT
  128. {
  129. return a.base() == b.base();
  130. }
  131. template<class A, class B, std::size_t Alignment>
  132. inline bool
  133. operator!=(const aligned_allocator_adaptor<A, Alignment>& a,
  134. const aligned_allocator_adaptor<B, Alignment>& b) BOOST_NOEXCEPT
  135. {
  136. return !(a == b);
  137. }
  138. } /* alignment */
  139. } /* boost */
  140. #endif