new_allocator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2014-2015. Distributed under the Boost
  4. // Software License, Version 1.0. (See accompanying file
  5. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. // See http://www.boost.org/libs/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_NEW_ALLOCATOR_HPP
  11. #define BOOST_CONTAINER_NEW_ALLOCATOR_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/throw_exception.hpp>
  21. #include <boost/container/detail/operator_new_helpers.hpp>
  22. #include <cstddef>
  23. //!\file
  24. namespace boost {
  25. namespace container {
  26. /// @cond
  27. template<bool Value>
  28. struct new_allocator_bool
  29. { BOOST_STATIC_CONSTEXPR bool value = Value; };
  30. template<class T>
  31. class new_allocator;
  32. /// @endcond
  33. //! Specialization of new_allocator for void types
  34. template<>
  35. class new_allocator<void>
  36. {
  37. public:
  38. typedef void value_type;
  39. typedef void * pointer;
  40. typedef const void* const_pointer;
  41. //!A integral constant of type bool with value true
  42. typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
  43. //!A integral constant of type bool with value true
  44. typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
  45. // reference-to-void members are impossible
  46. //!Obtains an new_allocator that allocates
  47. //!objects of type T2
  48. template<class T2>
  49. struct rebind
  50. {
  51. typedef new_allocator< T2> other;
  52. };
  53. //!Default constructor
  54. //!Never throws
  55. new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  56. {}
  57. //!Constructor from other new_allocator.
  58. //!Never throws
  59. new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  60. {}
  61. //!Copy assignment operator from other new_allocator.
  62. //!Never throws
  63. new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  64. {
  65. return *this;
  66. }
  67. //!Constructor from related new_allocator.
  68. //!Never throws
  69. template<class T2>
  70. new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
  71. {}
  72. //!Swaps two allocators, does nothing
  73. //!because this new_allocator is stateless
  74. friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  75. {}
  76. //!An new_allocator always compares to true, as memory allocated with one
  77. //!instance can be deallocated by another instance
  78. friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  79. { return true; }
  80. //!An new_allocator always compares to false, as memory allocated with one
  81. //!instance can be deallocated by another instance
  82. friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  83. { return false; }
  84. };
  85. //! This class is a reduced STL-compatible allocator that allocates memory using operator new
  86. template<class T>
  87. class new_allocator
  88. {
  89. public:
  90. typedef T value_type;
  91. typedef T * pointer;
  92. typedef const T * const_pointer;
  93. typedef T & reference;
  94. typedef const T & const_reference;
  95. typedef std::size_t size_type;
  96. typedef std::ptrdiff_t difference_type;
  97. //!A integral constant of type bool with value true
  98. typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) propagate_on_container_move_assignment;
  99. //!A integral constant of type bool with value true
  100. typedef BOOST_CONTAINER_IMPDEF(new_allocator_bool<true>) is_always_equal;
  101. //!Obtains an new_allocator that allocates
  102. //!objects of type T2
  103. template<class T2>
  104. struct rebind
  105. {
  106. typedef new_allocator<T2> other;
  107. };
  108. //!Default constructor
  109. //!Never throws
  110. inline new_allocator() BOOST_NOEXCEPT_OR_NOTHROW
  111. {}
  112. //!Constructor from other new_allocator.
  113. //!Never throws
  114. inline new_allocator(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  115. {}
  116. //!Copy assignment operator from other new_allocator.
  117. //!Never throws
  118. inline new_allocator& operator=(const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  119. { return *this; }
  120. //!Constructor from related new_allocator.
  121. //!Never throws
  122. template<class T2>
  123. inline new_allocator(const new_allocator<T2> &) BOOST_NOEXCEPT_OR_NOTHROW
  124. {}
  125. //!Allocates memory for an array of count elements.
  126. //!Throws bad_alloc if there is no enough memory
  127. pointer allocate(size_type count)
  128. {
  129. return dtl::operator_new_allocate<T>(count);
  130. }
  131. //!Deallocates previously allocated memory.
  132. //!Never throws
  133. void deallocate(pointer ptr, size_type n) BOOST_NOEXCEPT_OR_NOTHROW
  134. {
  135. return dtl::operator_delete_deallocate<T>(ptr, n);
  136. }
  137. //!Returns the maximum number of elements that could be allocated.
  138. //!Never throws
  139. inline size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  140. { return std::size_t(-1)/(2*sizeof(T)); }
  141. //!Swaps two allocators, does nothing
  142. //!because this new_allocator is stateless
  143. inline friend void swap(new_allocator &, new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  144. {}
  145. //!An new_allocator always compares to true, as memory allocated with one
  146. //!instance can be deallocated by another instance
  147. inline friend bool operator==(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  148. { return true; }
  149. //!An new_allocator always compares to false, as memory allocated with one
  150. //!instance can be deallocated by another instance
  151. inline friend bool operator!=(const new_allocator &, const new_allocator &) BOOST_NOEXCEPT_OR_NOTHROW
  152. { return false; }
  153. };
  154. } //namespace container {
  155. } //namespace boost {
  156. #include <boost/container/detail/config_end.hpp>
  157. #endif //BOOST_CONTAINER_NEW_ALLOCATOR_HPP