memory_resource.hpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-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_PMR_MEMORY_RESOURCE_HPP
  11. #define BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP
  12. #if defined (_MSC_VER)
  13. # pragma once
  14. #endif
  15. #include <boost/container/detail/config_begin.hpp>
  16. #include <boost/container/detail/workaround.hpp>
  17. #include <boost/container/container_fwd.hpp>
  18. #include <boost/move/detail/type_traits.hpp>
  19. #include <boost/container/detail/placement_new.hpp>
  20. #include <cstddef>
  21. namespace boost {
  22. namespace container {
  23. namespace pmr {
  24. //! The memory_resource class is an abstract interface to an
  25. //! unbounded set of classes encapsulating memory resources.
  26. class memory_resource
  27. {
  28. public:
  29. // For exposition only
  30. BOOST_STATIC_CONSTEXPR std::size_t max_align =
  31. boost::move_detail::alignment_of<boost::move_detail::max_align_t>::value;
  32. //! <b>Effects</b>: Destroys
  33. //! this memory_resource.
  34. virtual ~memory_resource(){}
  35. //! <b>Effects</b>: Equivalent to
  36. //! `return do_allocate(bytes, alignment);`
  37. void* allocate(std::size_t bytes, std::size_t alignment = max_align)
  38. {
  39. //Obtain a pointer to enough storage and initialize the lifetime
  40. //of an array object of the given size in the address
  41. return ::operator new(bytes, this->do_allocate(bytes, alignment), boost_container_new_t());
  42. }
  43. //! <b>Effects</b>: Equivalent to
  44. //! `return do_deallocate(bytes, alignment);`
  45. void deallocate(void* p, std::size_t bytes, std::size_t alignment = max_align)
  46. { return this->do_deallocate(p, bytes, alignment); }
  47. //! <b>Effects</b>: Equivalent to
  48. //! `return do_is_equal(other);`
  49. bool is_equal(const memory_resource& other) const BOOST_NOEXCEPT
  50. { return this->do_is_equal(other); }
  51. #if !defined(BOOST_EMBTC)
  52. //! <b>Returns</b>:
  53. //! `&a == &b || a.is_equal(b)`.
  54. friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  55. { return &a == &b || a.is_equal(b); }
  56. //! <b>Returns</b>:
  57. //! !(a == b).
  58. friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  59. { return !(a == b); }
  60. #else
  61. //! <b>Returns</b>:
  62. //! `&a == &b || a.is_equal(b)`.
  63. friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT;
  64. //! <b>Returns</b>:
  65. //! !(a == b).
  66. friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT;
  67. #endif
  68. protected:
  69. //! <b>Requires</b>: Alignment shall be a power of two.
  70. //!
  71. //! <b>Returns</b>: A derived class shall implement this function to return a pointer
  72. //! to allocated storage with a size of at least bytes. The returned storage is
  73. //! aligned to the specified alignment, if such alignment is supported; otherwise
  74. //! it is aligned to max_align.
  75. //!
  76. //! <b>Throws</b>: A derived class implementation shall throw an appropriate exception if
  77. //! it is unable to allocate memory with the requested size and alignment.
  78. virtual void* do_allocate(std::size_t bytes, std::size_t alignment) = 0;
  79. //! <b>Requires</b>: p shall have been returned from a prior call to
  80. //! `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage
  81. //! at p shall not yet have been deallocated.
  82. //!
  83. //! <b>Effects</b>: A derived class shall implement this function to dispose of allocated storage.
  84. //!
  85. //! <b>Throws</b>: Nothing.
  86. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) = 0;
  87. //! <b>Returns</b>: A derived class shall implement this function to return true if memory
  88. //! allocated from this can be deallocated from other and vice-versa; otherwise it shall
  89. //! return false. <i>[Note: The most-derived type of other might not match the type of this.
  90. //! For a derived class, D, a typical implementation of this function will compute
  91. //! `dynamic_cast<const D*>(&other)` and go no further (i.e., return false)
  92. //! if it returns nullptr. - end note]</i>.
  93. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT = 0;
  94. };
  95. #if defined(BOOST_EMBTC)
  96. //! <b>Returns</b>:
  97. //! `&a == &b || a.is_equal(b)`.
  98. inline bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  99. { return &a == &b || a.is_equal(b); }
  100. //! <b>Returns</b>:
  101. //! !(a == b).
  102. inline bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  103. { return !(a == b); }
  104. #endif
  105. } //namespace pmr {
  106. } //namespace container {
  107. } //namespace boost {
  108. #include <boost/container/detail/config_end.hpp>
  109. #endif //BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP