synchronized_pool_resource.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_SYNCHRONIZED_POOL_RESOURCE_HPP
  11. #define BOOST_CONTAINER_PMR_SYNCHRONIZED_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 <boost/container/detail/thread_mutex.hpp>
  21. #include <cstddef>
  22. namespace boost {
  23. namespace container {
  24. namespace pmr {
  25. //! A synchronized_pool_resource is a general-purpose memory resources having
  26. //! the following qualities:
  27. //!
  28. //! - Each resource owns the allocated memory, and frees it on destruction,
  29. //! even if deallocate has not been called for some of the allocated blocks.
  30. //!
  31. //! - A pool resource consists of a collection of pools, serving
  32. //! requests for different block sizes. Each individual pool manages a
  33. //! collection of chunks that are in turn divided into blocks of uniform size,
  34. //! returned via calls to do_allocate. Each call to do_allocate(size, alignment)
  35. //! is dispatched to the pool serving the smallest blocks accommodating at
  36. //! least size bytes.
  37. //!
  38. //! - When a particular pool is exhausted, allocating a block from that pool
  39. //! results in the allocation of an additional chunk of memory from the upstream
  40. //! allocator (supplied at construction), thus replenishing the pool. With
  41. //! each successive replenishment, the chunk size obtained increases
  42. //! geometrically. [ Note: By allocating memory in chunks, the pooling strategy
  43. //! increases the chance that consecutive allocations will be close together
  44. //! in memory. - end note ]
  45. //!
  46. //! - Allocation requests that exceed the largest block size of any pool are
  47. //! fulfilled directly from the upstream allocator.
  48. //!
  49. //! - A pool_options struct may be passed to the pool resource constructors to
  50. //! tune the largest block size and the maximum chunk size.
  51. //!
  52. //! A synchronized_pool_resource may be accessed from multiple threads without
  53. //! external synchronization and may have thread-specific pools to reduce
  54. //! synchronization costs.
  55. class BOOST_CONTAINER_DECL synchronized_pool_resource
  56. : public memory_resource
  57. {
  58. dtl::thread_mutex m_mut;
  59. pool_resource m_pool_resource;
  60. public:
  61. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options&,memory_resource*)
  62. synchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT;
  63. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource()
  64. synchronized_pool_resource() BOOST_NOEXCEPT;
  65. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(memory_resource*)
  66. explicit synchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT;
  67. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options&)
  68. explicit synchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT;
  69. #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
  70. synchronized_pool_resource(const synchronized_pool_resource&) = delete;
  71. synchronized_pool_resource operator=(const synchronized_pool_resource&) = delete;
  72. #else
  73. private:
  74. synchronized_pool_resource (const synchronized_pool_resource&);
  75. synchronized_pool_resource operator=(const synchronized_pool_resource&);
  76. public:
  77. #endif
  78. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::~unsynchronized_pool_resource()
  79. virtual ~synchronized_pool_resource();
  80. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::release()
  81. void release();
  82. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::upstream_resource()const
  83. memory_resource* upstream_resource() const;
  84. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::options()const
  85. pool_options options() const;
  86. protected:
  87. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_allocate()
  88. virtual void* do_allocate(std::size_t bytes, std::size_t alignment);
  89. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_deallocate(void*,std::size_t,std::size_t)
  90. virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment);
  91. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_is_equal(const memory_resource&)const
  92. virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT;
  93. //Non-standard observers
  94. public:
  95. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_count()
  96. std::size_t pool_count() const;
  97. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_index(std::size_t)const
  98. std::size_t pool_index(std::size_t bytes) const;
  99. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_next_blocks_per_chunk(std::size_t)const
  100. std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const;
  101. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_block(std::size_t)const
  102. std::size_t pool_block(std::size_t pool_idx) const;
  103. //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_cached_blocks(std::size_t)const
  104. std::size_t pool_cached_blocks(std::size_t pool_idx) const;
  105. };
  106. } //namespace pmr {
  107. } //namespace container {
  108. } //namespace boost {
  109. #include <boost/container/detail/config_end.hpp>
  110. #endif //BOOST_CONTAINER_PMR_SYNCHRONIZED_POOL_RESOURCE_HPP