default_allocator.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. Copyright 2019 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_CORE_DEFAULT_ALLOCATOR_HPP
  8. #define BOOST_CORE_DEFAULT_ALLOCATOR_HPP
  9. #include <boost/config.hpp>
  10. #include <new>
  11. #include <climits>
  12. #if defined(BOOST_LIBSTDCXX_VERSION) && BOOST_LIBSTDCXX_VERSION < 60000
  13. #define BOOST_CORE_NO_CXX11_ALLOCATOR
  14. #endif
  15. namespace boost {
  16. #if defined(BOOST_NO_EXCEPTIONS)
  17. BOOST_NORETURN void throw_exception(const std::exception&);
  18. #endif
  19. namespace default_ {
  20. struct true_type {
  21. typedef bool value_type;
  22. typedef true_type type;
  23. BOOST_STATIC_CONSTANT(bool, value = true);
  24. BOOST_CONSTEXPR operator bool() const BOOST_NOEXCEPT {
  25. return true;
  26. }
  27. BOOST_CONSTEXPR bool operator()() const BOOST_NOEXCEPT {
  28. return true;
  29. }
  30. };
  31. template<class T>
  32. struct add_reference {
  33. typedef T& type;
  34. };
  35. template<>
  36. struct add_reference<void> {
  37. typedef void type;
  38. };
  39. template<>
  40. struct add_reference<const void> {
  41. typedef const void type;
  42. };
  43. template<class T>
  44. struct default_allocator {
  45. typedef T value_type;
  46. typedef T* pointer;
  47. typedef const T* const_pointer;
  48. typedef typename add_reference<T>::type reference;
  49. typedef typename add_reference<const T>::type const_reference;
  50. typedef std::size_t size_type;
  51. typedef std::ptrdiff_t difference_type;
  52. typedef true_type propagate_on_container_move_assignment;
  53. typedef true_type is_always_equal;
  54. template<class U>
  55. struct rebind {
  56. typedef default_allocator<U> other;
  57. };
  58. #if !defined(BOOST_NO_CXX11_DEFAULTED_FUNCTIONS)
  59. default_allocator() = default;
  60. #else
  61. BOOST_CONSTEXPR default_allocator() BOOST_NOEXCEPT { }
  62. #endif
  63. template<class U>
  64. BOOST_CONSTEXPR default_allocator(const default_allocator<U>&)
  65. BOOST_NOEXCEPT { }
  66. #if defined(PTRDIFF_MAX) && defined(SIZE_MAX)
  67. BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
  68. return PTRDIFF_MAX < SIZE_MAX / sizeof(T)
  69. ? PTRDIFF_MAX : SIZE_MAX / sizeof(T);
  70. }
  71. #else
  72. BOOST_CONSTEXPR std::size_t max_size() const BOOST_NOEXCEPT {
  73. return ~static_cast<std::size_t>(0) / sizeof(T);
  74. }
  75. #endif
  76. #if !defined(BOOST_NO_EXCEPTIONS)
  77. T* allocate(std::size_t n) {
  78. if (n > max_size()) {
  79. throw std::bad_alloc();
  80. }
  81. return static_cast<T*>(::operator new(sizeof(T) * n));
  82. }
  83. void deallocate(T* p, std::size_t) {
  84. ::operator delete(p);
  85. }
  86. #else
  87. T* allocate(std::size_t n) {
  88. if (n > max_size()) {
  89. boost::throw_exception(std::bad_alloc());
  90. }
  91. void* p = ::operator new(sizeof(T) * n, std::nothrow);
  92. if (!p) {
  93. boost::throw_exception(std::bad_alloc());
  94. }
  95. return static_cast<T*>(p);
  96. }
  97. void deallocate(T* p, std::size_t) {
  98. ::operator delete(p, std::nothrow);
  99. }
  100. #endif
  101. #if defined(BOOST_NO_CXX11_ALLOCATOR) || defined(BOOST_CORE_NO_CXX11_ALLOCATOR)
  102. template<class U, class V>
  103. void construct(U* p, const V& v) {
  104. ::new(p) U(v);
  105. }
  106. template<class U>
  107. void destroy(U* p) {
  108. p->~U();
  109. }
  110. #endif
  111. };
  112. template<class T, class U>
  113. BOOST_CONSTEXPR inline bool
  114. operator==(const default_allocator<T>&,
  115. const default_allocator<U>&) BOOST_NOEXCEPT
  116. {
  117. return true;
  118. }
  119. template<class T, class U>
  120. BOOST_CONSTEXPR inline bool
  121. operator!=(const default_allocator<T>&,
  122. const default_allocator<U>&) BOOST_NOEXCEPT
  123. {
  124. return false;
  125. }
  126. } /* default_ */
  127. using default_::default_allocator;
  128. } /* boost */
  129. #endif