1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_PMR_SYNCHRONIZED_POOL_RESOURCE_HPP
12 #define BOOST_CONTAINER_PMR_SYNCHRONIZED_POOL_RESOURCE_HPP
14 #if defined (_MSC_VER)
18 #include <boost/container/detail/config_begin.hpp>
19 #include <boost/container/detail/workaround.hpp>
20 #include <boost/container/detail/auto_link.hpp>
21 #include <boost/container/pmr/memory_resource.hpp>
22 #include <boost/container/detail/pool_resource.hpp>
30 //! A synchronized_pool_resource is a general-purpose memory resources having
31 //! the following qualities:
33 //! - Each resource owns the allocated memory, and frees it on destruction,
34 //! even if deallocate has not been called for some of the allocated blocks.
36 //! - A pool resource consists of a collection of pools, serving
37 //! requests for different block sizes. Each individual pool manages a
38 //! collection of chunks that are in turn divided into blocks of uniform size,
39 //! returned via calls to do_allocate. Each call to do_allocate(size, alignment)
40 //! is dispatched to the pool serving the smallest blocks accommodating at
43 //! - When a particular pool is exhausted, allocating a block from that pool
44 //! results in the allocation of an additional chunk of memory from the upstream
45 //! allocator (supplied at construction), thus replenishing the pool. With
46 //! each successive replenishment, the chunk size obtained increases
47 //! geometrically. [ Note: By allocating memory in chunks, the pooling strategy
48 //! increases the chance that consecutive allocations will be close together
49 //! in memory. - end note ]
51 //! - Allocation requests that exceed the largest block size of any pool are
52 //! fulfilled directly from the upstream allocator.
54 //! - A pool_options struct may be passed to the pool resource constructors to
55 //! tune the largest block size and the maximum chunk size.
57 //! A synchronized_pool_resource may be accessed from multiple threads without
58 //! external synchronization and may have thread-specific pools to reduce
59 //! synchronization costs.
60 class BOOST_CONTAINER_DECL synchronized_pool_resource
61 : public memory_resource
63 pool_resource m_pool_resource;
68 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options&,memory_resource*)
69 synchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT;
71 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource()
72 synchronized_pool_resource() BOOST_NOEXCEPT;
74 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(memory_resource*)
75 explicit synchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT;
77 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::unsynchronized_pool_resource(const pool_options&)
78 explicit synchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT;
80 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED)
81 synchronized_pool_resource(const synchronized_pool_resource&) = delete;
82 synchronized_pool_resource operator=(const synchronized_pool_resource&) = delete;
85 synchronized_pool_resource (const synchronized_pool_resource&);
86 synchronized_pool_resource operator=(const synchronized_pool_resource&);
90 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::~unsynchronized_pool_resource()
91 virtual ~synchronized_pool_resource();
93 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::release()
96 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::upstream_resource()const
97 memory_resource* upstream_resource() const;
99 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::options()const
100 pool_options options() const;
104 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_allocate()
105 virtual void* do_allocate(std::size_t bytes, std::size_t alignment);
107 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_deallocate(void*,std::size_t,std::size_t)
108 virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment);
110 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::do_is_equal(const memory_resource&)const
111 virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT;
113 //Non-standard observers
116 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_count()
117 std::size_t pool_count() const;
119 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_index(std::size_t)const
120 std::size_t pool_index(std::size_t bytes) const;
122 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_next_blocks_per_chunk(std::size_t)const
123 std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const;
125 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_block(std::size_t)const
126 std::size_t pool_block(std::size_t pool_idx) const;
128 //! @copydoc ::boost::container::pmr::unsynchronized_pool_resource::pool_cached_blocks(std::size_t)const
129 std::size_t pool_cached_blocks(std::size_t pool_idx) const;
133 } //namespace container {
134 } //namespace boost {
136 #include <boost/container/detail/config_end.hpp>
138 #endif //BOOST_CONTAINER_PMR_SYNCHRONIZED_POOL_RESOURCE_HPP