pool_resource.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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_POOL_RESOURCE_HPP
  11. #define BOOST_CONTAINER_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/pmr/memory_resource.hpp>
  18. #include <boost/container/detail/block_list.hpp>
  19. #include <boost/container/pmr/pool_options.hpp>
  20. #include <cstddef>
  21. namespace boost {
  22. namespace container {
  23. namespace pmr {
  24. #if !defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  25. class pool_data_t;
  26. static const std::size_t pool_options_minimum_max_blocks_per_chunk = 1u;
  27. static const std::size_t pool_options_default_max_blocks_per_chunk = 32u;
  28. static const std::size_t pool_options_minimum_largest_required_pool_block =
  29. memory_resource::max_align > 2*sizeof(void*) ? memory_resource::max_align : 2*sizeof(void*);
  30. static const std::size_t pool_options_default_largest_required_pool_block =
  31. pool_options_minimum_largest_required_pool_block > 4096u
  32. ? pool_options_minimum_largest_required_pool_block : 4096u;
  33. #endif //BOOST_CONTAINER_DOXYGEN_INVOKED
  34. class pool_resource
  35. {
  36. typedef block_list_base<> block_list_base_t;
  37. pool_options m_options;
  38. memory_resource& m_upstream;
  39. block_list_base_t m_oversized_list;
  40. pool_data_t *m_pool_data;
  41. std::size_t m_pool_count;
  42. static void priv_limit_option(std::size_t &val, std::size_t min, std::size_t max);
  43. static std::size_t priv_pool_index(std::size_t block_size);
  44. static std::size_t priv_pool_block(std::size_t index);
  45. void priv_fix_options();
  46. void priv_init_pools();
  47. void priv_constructor_body();
  48. public:
  49. //! <b>Requires</b>: `upstream` is the address of a valid memory resource.
  50. //!
  51. //! <b>Effects</b>: Constructs a pool resource object that will obtain memory
  52. //! from upstream whenever the pool resource is unable to satisfy a memory
  53. //! request from its own internal data structures. The resulting object will hold
  54. //! a copy of upstream, but will not own the resource to which upstream points.
  55. //! [ Note: The intention is that calls to upstream->allocate() will be
  56. //! substantially fewer than calls to this->allocate() in most cases. - end note
  57. //! The behavior of the pooling mechanism is tuned according to the value of
  58. //! the opts argument.
  59. //!
  60. //! <b>Throws</b>: Nothing unless upstream->allocate() throws. It is unspecified if
  61. //! or under what conditions this constructor calls upstream->allocate().
  62. pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT;
  63. //! <b>Effects</b>: Same as
  64. //! `pool_resource(pool_options(), get_default_resource())`.
  65. pool_resource() BOOST_NOEXCEPT;
  66. //! <b>Effects</b>: Same as
  67. //! `pool_resource(pool_options(), upstream)`.
  68. explicit pool_resource(memory_resource* upstream) BOOST_NOEXCEPT;
  69. //! <b>Effects</b>: Same as
  70. //! `pool_resource(opts, get_default_resource())`.
  71. explicit pool_resource(const pool_options& opts) BOOST_NOEXCEPT;
  72. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  73. pool_resource(const pool_resource&) = delete;
  74. pool_resource operator=(const pool_resource&) = delete;
  75. #else
  76. private:
  77. pool_resource (const pool_resource&);
  78. pool_resource operator=(const pool_resource&);
  79. public:
  80. #endif
  81. //! <b>Effects</b>: Calls
  82. //! `this->release()`.
  83. virtual ~pool_resource();
  84. //! <b>Effects</b>: Calls Calls `upstream_resource()->deallocate()` as necessary
  85. //! to release all allocated memory. [ Note: memory is released back to
  86. //! `upstream_resource()` even if deallocate has not been called for some
  87. //! of the allocated blocks. - end note ]
  88. void release();
  89. //! <b>Returns</b>: The value of the upstream argument provided to the
  90. //! constructor of this object.
  91. memory_resource* upstream_resource() const;
  92. //! <b>Returns</b>: The options that control the pooling behavior of this resource.
  93. //! The values in the returned struct may differ from those supplied to the pool
  94. //! resource constructor in that values of zero will be replaced with
  95. //! implementation-defined defaults and sizes may be rounded to unspecified granularity.
  96. pool_options options() const;
  97. public: //public so that [un]synchronized_pool_resource can use them
  98. //! <b>Returns</b>: A pointer to allocated storage with a size of at least `bytes`.
  99. //! The size and alignment of the allocated memory shall meet the requirements for
  100. //! a class derived from `memory_resource`.
  101. //!
  102. //! <b>Effects</b>: If the pool selected for a block of size bytes is unable to
  103. //! satisfy the memory request from its own internal data structures, it will call
  104. //! `upstream_resource()->allocate()` to obtain more memory. If `bytes` is larger
  105. //! than that which the largest pool can handle, then memory will be allocated
  106. //! using `upstream_resource()->allocate()`.
  107. //!
  108. //! <b>Throws</b>: Nothing unless `upstream_resource()->allocate()` throws.
  109. virtual void* do_allocate(std::size_t bytes, std::size_t alignment);
  110. //! <b>Effects</b>: Return the memory at p to the pool. It is unspecified if or under
  111. //! what circumstances this operation will result in a call to
  112. //! `upstream_resource()->deallocate()`.
  113. //!
  114. //! <b>Throws</b>: Nothing.
  115. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment);
  116. //! <b>Returns</b>:
  117. //! `this == dynamic_cast<const pool_resource*>(&other)`.
  118. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT;
  119. //Non-standard observers
  120. public:
  121. //! <b>Returns</b>: The number of pools that will be used in the pool resource.
  122. //!
  123. //! <b>Note</b>: Non-standard extension.
  124. std::size_t pool_count() const;
  125. //! <b>Returns</b>: The index of the pool that will be used to serve the allocation of `bytes`.
  126. //! from the pool specified by `pool_index`. Returns `pool_count()` if `bytes` is bigger
  127. //! than `options().largest_required_pool_block` (no pool will be used to serve this).
  128. //!
  129. //! <b>Note</b>: Non-standard extension.
  130. std::size_t pool_index(std::size_t bytes) const;
  131. //! <b>Requires</b>: `pool_idx < pool_index()`
  132. //!
  133. //! <b>Returns</b>: The number blocks that will be allocated in the next chunk
  134. //! from the pool specified by `pool_idx`.
  135. //!
  136. //! <b>Note</b>: Non-standard extension.
  137. std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const;
  138. //! <b>Requires</b>: `pool_idx < pool_index()`
  139. //!
  140. //! <b>Returns</b>: The number of bytes of the block that the specified `pool_idx` pool manages.
  141. //!
  142. //! <b>Note</b>: Non-standard extension.
  143. std::size_t pool_block(std::size_t pool_idx) const;
  144. //! <b>Requires</b>: `pool_idx < pool_index()`
  145. //!
  146. //! <b>Returns</b>: The number of blocks that the specified `pool_idx` pool has cached
  147. //! and will be served without calling the upstream_allocator.
  148. //!
  149. //! <b>Note</b>: Non-standard extension.
  150. std::size_t pool_cached_blocks(std::size_t pool_idx) const;
  151. };
  152. } //namespace pmr {
  153. } //namespace container {
  154. } //namespace boost {
  155. #include <boost/container/detail/config_end.hpp>
  156. #endif //BOOST_CONTAINER_POOL_RESOURCE_HPP