memory_resource.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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 <cstddef>
  20. namespace boost {
  21. namespace container {
  22. namespace pmr {
  23. //! The memory_resource class is an abstract interface to an
  24. //! unbounded set of classes encapsulating memory resources.
  25. class BOOST_CONTAINER_DECL memory_resource
  26. {
  27. public:
  28. // For exposition only
  29. static BOOST_CONSTEXPR_OR_CONST std::size_t max_align =
  30. boost::move_detail::alignment_of<boost::move_detail::max_align_t>::value;
  31. //! <b>Effects</b>: Destroys
  32. //! this memory_resource.
  33. virtual ~memory_resource(){}
  34. //! <b>Effects</b>: Equivalent to
  35. //! `return do_allocate(bytes, alignment);`
  36. void* allocate(std::size_t bytes, std::size_t alignment = max_align)
  37. { return this->do_allocate(bytes, alignment); }
  38. //! <b>Effects</b>: Equivalent to
  39. //! `return do_deallocate(bytes, alignment);`
  40. void deallocate(void* p, std::size_t bytes, std::size_t alignment = max_align)
  41. { return this->do_deallocate(p, bytes, alignment); }
  42. //! <b>Effects</b>: Equivalent to
  43. //! `return return do_is_equal(other);`
  44. bool is_equal(const memory_resource& other) const BOOST_NOEXCEPT
  45. { return this->do_is_equal(other); }
  46. //! <b>Returns</b>:
  47. //! `&a == &b || a.is_equal(b)`.
  48. friend bool operator==(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  49. { return &a == &b || a.is_equal(b); }
  50. //! <b>Returns</b>:
  51. //! !(a == b).
  52. friend bool operator!=(const memory_resource& a, const memory_resource& b) BOOST_NOEXCEPT
  53. { return !(a == b); }
  54. protected:
  55. //! <b>Requires</b>: Alignment shall be a power of two.
  56. //!
  57. //! <b>Returns</b>: A derived class shall implement this function to return a pointer
  58. //! to allocated storage with a size of at least bytes. The returned storage is
  59. //! aligned to the specified alignment, if such alignment is supported; otherwise
  60. //! it is aligned to max_align.
  61. //!
  62. //! <b>Throws</b>: A derived class implementation shall throw an appropriate exception if
  63. //! it is unable to allocate memory with the requested size and alignment.
  64. virtual void* do_allocate(std::size_t bytes, std::size_t alignment) = 0;
  65. //! <b>Requires</b>: p shall have been returned from a prior call to
  66. //! `allocate(bytes, alignment)` on a memory resource equal to *this, and the storage
  67. //! at p shall not yet have been deallocated.
  68. //!
  69. //! <b>Effects</b>: A derived class shall implement this function to dispose of allocated storage.
  70. //!
  71. //! <b>Throws</b>: Nothing.
  72. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) = 0;
  73. //! <b>Returns</b>: A derived class shall implement this function to return true if memory
  74. //! allocated from this can be deallocated from other and vice-versa; otherwise it shall
  75. //! return false. <i>[Note: The most-derived type of other might not match the type of this.
  76. //! For a derived class, D, a typical implementation of this function will compute
  77. //! `dynamic_cast<const D*>(&other)` and go no further (i.e., return false)
  78. //! if it returns nullptr. - end note]</i>.
  79. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT = 0;
  80. };
  81. } //namespace pmr {
  82. } //namespace container {
  83. } //namespace boost {
  84. #include <boost/container/detail/config_end.hpp>
  85. #endif //BOOST_CONTAINER_PMR_MEMORY_RESOURCE_HPP