aligned_allocator.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. (c) 2014-2015 Glen Joseph Fernandes
  3. <glenjofe -at- gmail.com>
  4. Distributed under the Boost Software
  5. License, Version 1.0.
  6. http://boost.org/LICENSE_1_0.txt
  7. */
  8. #ifndef BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
  9. #define BOOST_ALIGN_ALIGNED_ALLOCATOR_HPP
  10. #include <boost/align/detail/addressof.hpp>
  11. #include <boost/align/detail/is_alignment_constant.hpp>
  12. #include <boost/align/detail/max_objects.hpp>
  13. #include <boost/align/detail/max_size.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 <boost/throw_exception.hpp>
  19. #include <new>
  20. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  21. #include <utility>
  22. #endif
  23. namespace boost {
  24. namespace alignment {
  25. template<class T, std::size_t Alignment>
  26. class aligned_allocator {
  27. BOOST_STATIC_ASSERT(detail::
  28. is_alignment_constant<Alignment>::value);
  29. public:
  30. typedef T value_type;
  31. typedef T* pointer;
  32. typedef const T* const_pointer;
  33. typedef void* void_pointer;
  34. typedef const void* const_void_pointer;
  35. typedef std::size_t size_type;
  36. typedef std::ptrdiff_t difference_type;
  37. typedef T& reference;
  38. typedef const T& const_reference;
  39. private:
  40. enum {
  41. min_align = detail::max_size<Alignment,
  42. alignment_of<value_type>::value>::value
  43. };
  44. public:
  45. template<class U>
  46. struct rebind {
  47. typedef aligned_allocator<U, Alignment> other;
  48. };
  49. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  50. aligned_allocator() = default;
  51. #else
  52. aligned_allocator() BOOST_NOEXCEPT {
  53. }
  54. #endif
  55. template<class U>
  56. aligned_allocator(const aligned_allocator<U, Alignment>&)
  57. BOOST_NOEXCEPT {
  58. }
  59. pointer address(reference value) const BOOST_NOEXCEPT {
  60. return detail::addressof(value);
  61. }
  62. const_pointer address(const_reference value) const BOOST_NOEXCEPT {
  63. return detail::addressof(value);
  64. }
  65. pointer allocate(size_type size, const_void_pointer = 0) {
  66. void* p = 0;
  67. if (size > 0) {
  68. p = aligned_alloc(min_align, sizeof(T) * size);
  69. if (!p) {
  70. ::boost::throw_exception(std::bad_alloc());
  71. }
  72. }
  73. return static_cast<T*>(p);
  74. }
  75. void deallocate(pointer ptr, size_type) {
  76. ::boost::alignment::aligned_free(ptr);
  77. }
  78. BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT {
  79. return detail::max_objects<T>::value;
  80. }
  81. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  82. #if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES)
  83. template<class U, class... Args>
  84. void construct(U* ptr, Args&&... args) {
  85. ::new(static_cast<void*>(ptr)) U(std::forward<Args>(args)...);
  86. }
  87. #else
  88. template<class U, class V>
  89. void construct(U* ptr, V&& value) {
  90. ::new(static_cast<void*>(ptr)) U(std::forward<V>(value));
  91. }
  92. #endif
  93. #else
  94. template<class U, class V>
  95. void construct(U* ptr, const V& value) {
  96. ::new(static_cast<void*>(ptr)) U(value);
  97. }
  98. #endif
  99. template<class U>
  100. void construct(U* ptr) {
  101. ::new(static_cast<void*>(ptr)) U();
  102. }
  103. template<class U>
  104. void destroy(U* ptr) {
  105. (void)ptr;
  106. ptr->~U();
  107. }
  108. };
  109. template<std::size_t Alignment>
  110. class aligned_allocator<void, Alignment> {
  111. BOOST_STATIC_ASSERT(detail::
  112. is_alignment_constant<Alignment>::value);
  113. public:
  114. typedef void value_type;
  115. typedef void* pointer;
  116. typedef const void* const_pointer;
  117. template<class U>
  118. struct rebind {
  119. typedef aligned_allocator<U, Alignment> other;
  120. };
  121. };
  122. template<class T1, class T2, std::size_t Alignment>
  123. inline bool operator==(const aligned_allocator<T1, Alignment>&,
  124. const aligned_allocator<T2, Alignment>&) BOOST_NOEXCEPT
  125. {
  126. return true;
  127. }
  128. template<class T1, class T2, std::size_t Alignment>
  129. inline bool operator!=(const aligned_allocator<T1, Alignment>&,
  130. const aligned_allocator<T2, Alignment>&) BOOST_NOEXCEPT
  131. {
  132. return false;
  133. }
  134. } /* .alignment */
  135. } /* .boost */
  136. #endif