aligned_allocator.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. Copyright 2014-2015 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_HPP
  8. #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
  9. #include <boost/align/detail/addressof.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/aligned_alloc.hpp>
  14. #include <boost/align/aligned_allocator_forward.hpp>
  15. #include <boost/align/alignment_of.hpp>
  16. #include <boost/static_assert.hpp>
  17. #include <boost/throw_exception.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 std::size_t size_type;
  34. typedef std::ptrdiff_t difference_type;
  35. typedef T& reference;
  36. typedef const T& const_reference;
  37. template<class U>
  38. struct rebind {
  39. typedef aligned_allocator<U, Alignment> other;
  40. };
  41. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  42. aligned_allocator() = default;
  43. #else
  44. aligned_allocator() BOOST_NOEXCEPT { }
  45. #endif
  46. template<class U>
  47. aligned_allocator(const aligned_allocator<U, Alignment>&)
  48. BOOST_NOEXCEPT { }
  49. pointer address(reference value) const BOOST_NOEXCEPT {
  50. return detail::addressof(value);
  51. }
  52. const_pointer address(const_reference value) const BOOST_NOEXCEPT {
  53. return detail::addressof(value);
  54. }
  55. pointer allocate(size_type size, const_void_pointer = 0) {
  56. enum {
  57. m = detail::max_size<Alignment,
  58. alignment_of<value_type>::value>::value
  59. };
  60. if (size == 0) {
  61. return 0;
  62. }
  63. void* p = boost::alignment::aligned_alloc(m, sizeof(T) * size);
  64. if (!p) {
  65. boost::throw_exception(std::bad_alloc());
  66. }
  67. return static_cast<T*>(p);
  68. }
  69. void deallocate(pointer ptr, size_type) {
  70. boost::alignment::aligned_free(ptr);
  71. }
  72. BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
  73. return detail::max_objects<T>::value;
  74. }
  75. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  76. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  77. template<class U, class... Args>
  78. void construct(U* ptr, Args&&... args) {
  79. ::new((void*)ptr) U(std::forward<Args>(args)...);
  80. }
  81. #else
  82. template<class U, class V>
  83. void construct(U* ptr, V&& value) {
  84. ::new((void*)ptr) U(std::forward<V>(value));
  85. }
  86. #endif
  87. #else
  88. template<class U, class V>
  89. void construct(U* ptr, const V& value) {
  90. ::new((void*)ptr) U(value);
  91. }
  92. #endif
  93. template<class U>
  94. void construct(U* ptr) {
  95. ::new((void*)ptr) U();
  96. }
  97. template<class U>
  98. void destroy(U* ptr) {
  99. (void)ptr;
  100. ptr->~U();
  101. }
  102. };
  103. template<std::size_t Alignment>
  104. class aligned_allocator<void, Alignment> {
  105. BOOST_STATIC_ASSERT(detail::is_alignment_constant<Alignment>::value);
  106. public:
  107. typedef void value_type;
  108. typedef void* pointer;
  109. typedef const void* const_pointer;
  110. template<class U>
  111. struct rebind {
  112. typedef aligned_allocator<U, Alignment> other;
  113. };
  114. };
  115. template<class T, class U, std::size_t Alignment>
  116. inline bool
  117. operator==(const aligned_allocator<T, Alignment>&,
  118. const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
  119. {
  120. return true;
  121. }
  122. template<class T, class U, std::size_t Alignment>
  123. inline bool
  124. operator!=(const aligned_allocator<T, Alignment>&,
  125. const aligned_allocator<U, Alignment>&) BOOST_NOEXCEPT
  126. {
  127. return false;
  128. }
  129. } /* alignment */
  130. } /* boost */
  131. #endif