monotonic_buffer_resource.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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_MONOTONIC_BUFFER_RESOURCE_HPP
  11. #define BOOST_CONTAINER_PMR_MONOTONIC_BUFFER_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/detail/auto_link.hpp>
  18. #include <boost/container/pmr/memory_resource.hpp>
  19. #include <boost/container/detail/block_slist.hpp>
  20. #include <cstddef>
  21. namespace boost {
  22. namespace container {
  23. namespace pmr {
  24. //! A monotonic_buffer_resource is a special-purpose memory resource intended for
  25. //! very fast memory allocations in situations where memory is used to build up a
  26. //! few objects and then is released all at once when the memory resource object
  27. //! is destroyed. It has the following qualities:
  28. //!
  29. //! - A call to deallocate has no effect, thus the amount of memory consumed
  30. //! increases monotonically until the resource is destroyed.
  31. //!
  32. //! - The program can supply an initial buffer, which the allocator uses to satisfy
  33. //! memory requests.
  34. //!
  35. //! - When the initial buffer (if any) is exhausted, it obtains additional buffers
  36. //! from an upstream memory resource supplied at construction. Each additional
  37. //! buffer is larger than the previous one, following a geometric progression.
  38. //!
  39. //! - It is intended for access from one thread of control at a time. Specifically,
  40. //! calls to allocate and deallocate do not synchronize with one another.
  41. //!
  42. //! - It owns the allocated memory and frees it on destruction, even if deallocate has
  43. //! not been called for some of the allocated blocks.
  44. class BOOST_CONTAINER_DECL monotonic_buffer_resource
  45. : public memory_resource
  46. {
  47. block_slist m_memory_blocks;
  48. void * m_current_buffer;
  49. std::size_t m_current_buffer_size;
  50. std::size_t m_next_buffer_size;
  51. void * const m_initial_buffer;
  52. std::size_t const m_initial_buffer_size;
  53. /// @cond
  54. void increase_next_buffer();
  55. void increase_next_buffer_at_least_to(std::size_t minimum_size);
  56. void *allocate_from_current(std::size_t aligner, std::size_t bytes);
  57. /// @endcond
  58. public:
  59. //! The number of bytes that will be requested by the default in the first call
  60. //! to the upstream allocator
  61. //!
  62. //! <b>Note</b>: Non-standard extension.
  63. static const std::size_t initial_next_buffer_size = 32u*sizeof(void*);
  64. //! <b>Requires</b>: `upstream` shall be the address of a valid memory resource or `nullptr`
  65. //!
  66. //! <b>Effects</b>: If `upstream` is not nullptr, sets the internal resource to `upstream`,
  67. //! to get_default_resource() otherwise.
  68. //! Sets the internal `current_buffer` to `nullptr` and the internal `next_buffer_size` to an
  69. //! implementation-defined size.
  70. explicit monotonic_buffer_resource(memory_resource* upstream = 0) BOOST_NOEXCEPT;
  71. //! <b>Requires</b>: `upstream` shall be the address of a valid memory resource or `nullptr`
  72. //! and `initial_size` shall be greater than zero.
  73. //!
  74. //! <b>Effects</b>: If `upstream` is not nullptr, sets the internal resource to `upstream`,
  75. //! to get_default_resource() otherwise. Sets the internal `current_buffer` to `nullptr` and
  76. //! `next_buffer_size` to at least `initial_size`.
  77. explicit monotonic_buffer_resource(std::size_t initial_size, memory_resource* upstream = 0) BOOST_NOEXCEPT;
  78. //! <b>Requires</b>: `upstream` shall be the address of a valid memory resource or `nullptr`,
  79. //! `buffer_size` shall be no larger than the number of bytes in buffer.
  80. //!
  81. //! <b>Effects</b>: If `upstream` is not nullptr, sets the internal resource to `upstream`,
  82. //! to get_default_resource() otherwise. Sets the internal `current_buffer` to `buffer`,
  83. //! and `next_buffer_size` to `buffer_size` (but not less than an implementation-defined size),
  84. //! then increases `next_buffer_size` by an implementation-defined growth factor (which need not be integral).
  85. monotonic_buffer_resource(void* buffer, std::size_t buffer_size, memory_resource* upstream = 0) BOOST_NOEXCEPT;
  86. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  87. monotonic_buffer_resource(const monotonic_buffer_resource&) = delete;
  88. monotonic_buffer_resource operator=(const monotonic_buffer_resource&) = delete;
  89. #else
  90. private:
  91. monotonic_buffer_resource (const monotonic_buffer_resource&);
  92. monotonic_buffer_resource operator=(const monotonic_buffer_resource&);
  93. public:
  94. #endif
  95. //! <b>Effects</b>: Calls
  96. //! `this->release()`.
  97. virtual ~monotonic_buffer_resource();
  98. //! <b>Effects</b>: `upstream_resource()->deallocate()` as necessary to release all allocated memory.
  99. //! [Note: memory is released back to `upstream_resource()` even if some blocks that were allocated
  100. //! from this have not been deallocated from this. - end note]
  101. void release() BOOST_NOEXCEPT;
  102. //! <b>Returns</b>: The value of
  103. //! the internal resource.
  104. memory_resource* upstream_resource() const BOOST_NOEXCEPT;
  105. //! <b>Returns</b>:
  106. //! The number of bytes of storage available for the specified alignment and
  107. //! the number of bytes wasted due to the requested alignment.
  108. //!
  109. //! <b>Note</b>: Non-standard extension.
  110. std::size_t remaining_storage(std::size_t alignment, std::size_t &wasted_due_to_alignment) const BOOST_NOEXCEPT;
  111. //! <b>Returns</b>:
  112. //! The number of bytes of storage available for the specified alignment.
  113. //!
  114. //! <b>Note</b>: Non-standard extension.
  115. std::size_t remaining_storage(std::size_t alignment = 1u) const BOOST_NOEXCEPT;
  116. //! <b>Returns</b>:
  117. //! The address pointing to the start of the current free storage.
  118. //!
  119. //! <b>Note</b>: Non-standard extension.
  120. const void *current_buffer() const BOOST_NOEXCEPT;
  121. //! <b>Returns</b>:
  122. //! The number of bytes that will be requested for the next buffer once the
  123. //! current one is exhausted.
  124. //!
  125. //! <b>Note</b>: Non-standard extension.
  126. std::size_t next_buffer_size() const BOOST_NOEXCEPT;
  127. protected:
  128. //! <b>Returns</b>: A pointer to allocated storage with a size of at least `bytes`. The size
  129. //! and alignment of the allocated memory shall meet the requirements for a class derived
  130. //! from `memory_resource`.
  131. //!
  132. //! <b>Effects</b>: If the unused space in the internal `current_buffer` can fit a block with the specified
  133. //! bytes and alignment, then allocate the return block from the internal `current_buffer`; otherwise sets
  134. //! the internal `current_buffer` to `upstream_resource()->allocate(n, m)`, where `n` is not less than
  135. //! `max(bytes, next_buffer_size)` and `m` is not less than alignment, and increase
  136. //! `next_buffer_size` by an implementation-defined growth factor (which need not be integral),
  137. //! then allocate the return block from the newly-allocated internal `current_buffer`.
  138. //!
  139. //! <b>Throws</b>: Nothing unless `upstream_resource()->allocate()` throws.
  140. virtual void* do_allocate(std::size_t bytes, std::size_t alignment);
  141. //! <b>Effects</b>: None
  142. //!
  143. //! <b>Throws</b>: Nothing
  144. //!
  145. //! <b>Remarks</b>: Memory used by this resource increases monotonically until its destruction.
  146. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_NOEXCEPT;
  147. //! <b>Returns</b>:
  148. //! `this == dynamic_cast<const monotonic_buffer_resource*>(&other)`.
  149. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT;
  150. };
  151. } //namespace pmr {
  152. } //namespace container {
  153. } //namespace boost {
  154. #include <boost/container/detail/config_end.hpp>
  155. #endif //BOOST_CONTAINER_PMR_MONOTONIC_BUFFER_RESOURCE_HPP