unsynchronized_pool_resource.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_UNSYNCHRONIZED_POOL_RESOURCE_HPP
  11. #define BOOST_CONTAINER_PMR_UNSYNCHRONIZED_POOL_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/pool_resource.hpp>
  20. #include <cstddef>
  21. namespace boost {
  22. namespace container {
  23. namespace pmr {
  24. //! A unsynchronized_pool_resource is a general-purpose memory resources having
  25. //! the following qualities:
  26. //!
  27. //! - Each resource owns the allocated memory, and frees it on destruction,
  28. //! even if deallocate has not been called for some of the allocated blocks.
  29. //!
  30. //! - A pool resource consists of a collection of pools, serving
  31. //! requests for different block sizes. Each individual pool manages a
  32. //! collection of chunks that are in turn divided into blocks of uniform size,
  33. //! returned via calls to do_allocate. Each call to do_allocate(size, alignment)
  34. //! is dispatched to the pool serving the smallest blocks accommodating at
  35. //! least size bytes.
  36. //!
  37. //! - When a particular pool is exhausted, allocating a block from that pool
  38. //! results in the allocation of an additional chunk of memory from the upstream
  39. //! allocator (supplied at construction), thus replenishing the pool. With
  40. //! each successive replenishment, the chunk size obtained increases
  41. //! geometrically. [ Note: By allocating memory in chunks, the pooling strategy
  42. //! increases the chance that consecutive allocations will be close together
  43. //! in memory. - end note ]
  44. //!
  45. //! - Allocation requests that exceed the largest block size of any pool are
  46. //! fulfilled directly from the upstream allocator.
  47. //!
  48. //! - A pool_options struct may be passed to the pool resource constructors to
  49. //! tune the largest block size and the maximum chunk size.
  50. //!
  51. //! An unsynchronized_pool_resource class may not be accessed from multiple threads
  52. //! simultaneously and thus avoids the cost of synchronization entirely in
  53. //! single-threaded applications.
  54. class BOOST_CONTAINER_DECL unsynchronized_pool_resource
  55. : public memory_resource
  56. {
  57. pool_resource m_resource;
  58. public:
  59. //! <b>Requires</b>: `upstream` is the address of a valid memory resource.
  60. //!
  61. //! <b>Effects</b>: Constructs a pool resource object that will obtain memory
  62. //! from upstream whenever the pool resource is unable to satisfy a memory
  63. //! request from its own internal data structures. The resulting object will hold
  64. //! a copy of upstream, but will not own the resource to which upstream points.
  65. //! [ Note: The intention is that calls to upstream->allocate() will be
  66. //! substantially fewer than calls to this->allocate() in most cases. - end note
  67. //! The behavior of the pooling mechanism is tuned according to the value of
  68. //! the opts argument.
  69. //!
  70. //! <b>Throws</b>: Nothing unless upstream->allocate() throws. It is unspecified if
  71. //! or under what conditions this constructor calls upstream->allocate().
  72. unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT;
  73. //! <b>Effects</b>: Same as
  74. //! `unsynchronized_pool_resource(pool_options(), get_default_resource())`.
  75. unsynchronized_pool_resource() BOOST_NOEXCEPT;
  76. //! <b>Effects</b>: Same as
  77. //! `unsynchronized_pool_resource(pool_options(), upstream)`.
  78. explicit unsynchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT;
  79. //! <b>Effects</b>: Same as
  80. //! `unsynchronized_pool_resource(opts, get_default_resource())`.
  81. explicit unsynchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT;
  82. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  83. unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete;
  84. unsynchronized_pool_resource operator=(const unsynchronized_pool_resource&) = delete;
  85. #else
  86. private:
  87. unsynchronized_pool_resource (const unsynchronized_pool_resource&);
  88. unsynchronized_pool_resource operator=(const unsynchronized_pool_resource&);
  89. public:
  90. #endif
  91. //! <b>Effects</b>: Calls
  92. //! `this->release()`.
  93. virtual ~unsynchronized_pool_resource();
  94. //! <b>Effects</b>: Calls Calls `upstream_resource()->deallocate()` as necessary
  95. //! to release all allocated memory. [ Note: memory is released back to
  96. //! `upstream_resource()` even if deallocate has not been called for some
  97. //! of the allocated blocks. - end note ]
  98. void release();
  99. //! <b>Returns</b>: The value of the upstream argument provided to the
  100. //! constructor of this object.
  101. memory_resource* upstream_resource() const;
  102. //! <b>Returns</b>: The options that control the pooling behavior of this resource.
  103. //! The values in the returned struct may differ from those supplied to the pool
  104. //! resource constructor in that values of zero will be replaced with
  105. //! implementation-defined defaults and sizes may be rounded to unspecified granularity.
  106. pool_options options() const;
  107. protected:
  108. //! <b>Returns</b>: A pointer to allocated storage with a size of at least `bytes`.
  109. //! The size and alignment of the allocated memory shall meet the requirements for
  110. //! a class derived from `memory_resource`.
  111. //!
  112. //! <b>Effects</b>: If the pool selected for a block of size bytes is unable to
  113. //! satisfy the memory request from its own internal data structures, it will call
  114. //! `upstream_resource()->allocate()` to obtain more memory. If `bytes` is larger
  115. //! than that which the largest pool can handle, then memory will be allocated
  116. //! using `upstream_resource()->allocate()`.
  117. //!
  118. //! <b>Throws</b>: Nothing unless `upstream_resource()->allocate()` throws.
  119. virtual void* do_allocate(std::size_t bytes, std::size_t alignment);
  120. //! <b>Effects</b>: Return the memory at p to the pool. It is unspecified if or under
  121. //! what circumstances this operation will result in a call to
  122. //! `upstream_resource()->deallocate()`.
  123. //!
  124. //! <b>Throws</b>: Nothing.
  125. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment);
  126. //! <b>Returns</b>:
  127. //! `this == dynamic_cast<const unsynchronized_pool_resource*>(&other)`.
  128. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT;
  129. //Non-standard observers
  130. public:
  131. //! <b>Returns</b>: The number of pools that will be used in the pool resource.
  132. //!
  133. //! <b>Note</b>: Non-standard extension.
  134. std::size_t pool_count() const;
  135. //! <b>Returns</b>: The index of the pool that will be used to serve the allocation of `bytes`.
  136. //! Returns `pool_count()` if `bytes` is bigger
  137. //! than `options().largest_required_pool_block` (no pool will be used to serve this).
  138. //!
  139. //! <b>Note</b>: Non-standard extension.
  140. std::size_t pool_index(std::size_t bytes) const;
  141. //! <b>Requires</b>: `pool_idx < pool_index()`
  142. //!
  143. //! <b>Returns</b>: The number blocks that will be allocated in the next chunk
  144. //! from the pool specified by `pool_idx`.
  145. //!
  146. //! <b>Note</b>: Non-standard extension.
  147. std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const;
  148. //! <b>Requires</b>: `pool_idx < pool_index()`
  149. //!
  150. //! <b>Returns</b>: The number of bytes of the block that the specified `pool_idx` pool manages.
  151. //!
  152. //! <b>Note</b>: Non-standard extension.
  153. std::size_t pool_block(std::size_t pool_idx) const;
  154. //! <b>Requires</b>: `pool_idx < pool_index()`
  155. //!
  156. //! <b>Returns</b>: The number of blocks that the specified `pool_idx` pool has cached
  157. //! and will be served without calling the upstream_allocator.
  158. //!
  159. //! <b>Note</b>: Non-standard extension.
  160. std::size_t pool_cached_blocks(std::size_t pool_idx) const;
  161. };
  162. } //namespace pmr {
  163. } //namespace container {
  164. } //namespace boost {
  165. #include <boost/container/detail/config_end.hpp>
  166. #endif //BOOST_CONTAINER_PMR_UNSYNCHRONIZED_POOL_RESOURCE_HPP