aligned_allocator.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Copyright 2014-2015 Glen Joseph Fernandes
  3. ([email protected])
  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_HPP
  8. #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
  9. #include <boost/align/detail/add_reference.hpp>
  10. #include <boost/align/detail/is_alignment_constant.hpp>
  11. #include <boost/align/detail/max_objects.hpp>
  12. #include <boost/align/detail/max_size.hpp>
  13. #include <boost/align/detail/throw_exception.hpp>
  14. #include <boost/align/aligned_alloc.hpp>
  15. #include <boost/align/aligned_allocator_forward.hpp>
  16. #include <boost/align/alignment_of.hpp>
  17. #include <boost/static_assert.hpp>
  18. #include <new>
  19. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  20. #include <utility>
  21. #endif
  22. namespace boost {
  23. namespace alignment {
  24. template<class T, std::size_t Alignment>
  25. class aligned_allocator {
  26. BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
  27. public:
  28. typedef T value_type;
  29. typedef T* pointer;
  30. typedef const T* const_pointer;
  31. typedef void* void_pointer;
  32. typedef const void* const_void_pointer;
  33. typedef typename detail::add_lvalue_reference<T>::type reference;
  34. typedef typename detail::add_lvalue_reference<const
  35. T>::type const_reference;
  36. typedef std::size_t size_type;
  37. typedef std::ptrdiff_t difference_type;
  38. typedef detail::true_type propagate_on_container_move_assignment;
  39. typedef detail::true_type is_always_equal;
  40. template<class U>
  41. struct rebind {
  42. typedef aligned_allocator<U, Alignment> other;
  43. };
  44. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  45. aligned_allocator() = default;
  46. #else
  47. aligned_allocator() BOOST_NOEXCEPT { }
  48. #endif
  49. template<class U>
  50. aligned_allocator(const aligned_allocator<U, Alignment>&)
  51. BOOST_NOEXCEPT { }
  52. pointer allocate(size_type size, const_void_pointer = 0) {
  53. enum {
  54. m = detail::max_size<Alignment,
  55. alignment_of<value_type>::value>::value
  56. };
  57. if (size == 0) {
  58. return 0;
  59. }
  60. void* p = boost::alignment::aligned_alloc(m, sizeof(T) * size);
  61. if (!p) {
  62. detail::throw_exception(std::bad_alloc());
  63. }
  64. return static_cast<T*>(p);
  65. }
  66. void deallocate(pointer ptr, size_type) {
  67. boost::alignment::aligned_free(ptr);
  68. }
  69. BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
  70. return detail::max_objects<T>::value;
  71. }
  72. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  73. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  74. template<class U, class... Args>
  75. void construct(U* ptr, Args&&... args) {
  76. ::new((void*)ptr) U(std::forward<Args>(args)...);
  77. }
  78. #else
  79. template<class U, class V>
  80. void construct(U* ptr, V&& value) {
  81. ::new((void*)ptr) U(std::forward<V>(value));
  82. }
  83. #endif
  84. #else
  85. template<class U, class V>
  86. void construct(U* ptr, const V& value) {
  87. ::new((void*)ptr) U(value);
  88. }
  89. template<class U, class V>
  90. void construct(U* ptr, V& value) {
  91. ::new((void*)ptr) U(value);
  92. }
  93. #endif
  94. template<class U>
  95. void construct(U* ptr) {
  96. ::new((void*)ptr) U();
  97. }
  98. template<class U>
  99. void destroy(U* ptr) {
  100. (void)ptr;
  101. ptr->~U();
  102. }
  103. };
  104. template<class T, class U, std::size_t Alignment>
  105. inline bool
  106. operator==(const aligned_allocator<T, Alignment>&,
  107. const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
  108. {
  109. return true;
  110. }
  111. template<class T, class U, std::size_t Alignment>
  112. inline bool
  113. operator!=(const aligned_allocator<T, Alignment>&,
  114. const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
  115. {
  116. return false;
  117. }
  118. } /* alignment */
  119. } /* boost */
  120. #endif