aligned_allocator.hpp 3.9 KB

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