|
||||
File indexing completed on 2025-01-30 09:34:56
0001 ////////////////////////////////////////////////////////////////////////////// 0002 // 0003 // (C) Copyright Ion Gaztanaga 2015-2015. Distributed under the Boost 0004 // Software License, Version 1.0. (See accompanying file 0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 0006 // 0007 // See http://www.boost.org/libs/container for documentation. 0008 // 0009 ////////////////////////////////////////////////////////////////////////////// 0010 0011 #ifndef BOOST_CONTAINER_PMR_UNSYNCHRONIZED_POOL_RESOURCE_HPP 0012 #define BOOST_CONTAINER_PMR_UNSYNCHRONIZED_POOL_RESOURCE_HPP 0013 0014 #if defined (_MSC_VER) 0015 # pragma once 0016 #endif 0017 0018 #include <boost/container/detail/config_begin.hpp> 0019 #include <boost/container/detail/workaround.hpp> 0020 #include <boost/container/detail/auto_link.hpp> 0021 #include <boost/container/pmr/memory_resource.hpp> 0022 #include <boost/container/detail/pool_resource.hpp> 0023 0024 #include <cstddef> 0025 0026 namespace boost { 0027 namespace container { 0028 namespace pmr { 0029 0030 //! A unsynchronized_pool_resource is a general-purpose memory resources having 0031 //! the following qualities: 0032 //! 0033 //! - Each resource owns the allocated memory, and frees it on destruction, 0034 //! even if deallocate has not been called for some of the allocated blocks. 0035 //! 0036 //! - A pool resource consists of a collection of pools, serving 0037 //! requests for different block sizes. Each individual pool manages a 0038 //! collection of chunks that are in turn divided into blocks of uniform size, 0039 //! returned via calls to do_allocate. Each call to do_allocate(size, alignment) 0040 //! is dispatched to the pool serving the smallest blocks accommodating at 0041 //! least size bytes. 0042 //! 0043 //! - When a particular pool is exhausted, allocating a block from that pool 0044 //! results in the allocation of an additional chunk of memory from the upstream 0045 //! allocator (supplied at construction), thus replenishing the pool. With 0046 //! each successive replenishment, the chunk size obtained increases 0047 //! geometrically. [ Note: By allocating memory in chunks, the pooling strategy 0048 //! increases the chance that consecutive allocations will be close together 0049 //! in memory. - end note ] 0050 //! 0051 //! - Allocation requests that exceed the largest block size of any pool are 0052 //! fulfilled directly from the upstream allocator. 0053 //! 0054 //! - A pool_options struct may be passed to the pool resource constructors to 0055 //! tune the largest block size and the maximum chunk size. 0056 //! 0057 //! An unsynchronized_pool_resource class may not be accessed from multiple threads 0058 //! simultaneously and thus avoids the cost of synchronization entirely in 0059 //! single-threaded applications. 0060 class BOOST_CONTAINER_DECL unsynchronized_pool_resource 0061 : public memory_resource 0062 { 0063 pool_resource m_resource; 0064 0065 public: 0066 0067 //! <b>Requires</b>: `upstream` is the address of a valid memory resource. 0068 //! 0069 //! <b>Effects</b>: Constructs a pool resource object that will obtain memory 0070 //! from upstream whenever the pool resource is unable to satisfy a memory 0071 //! request from its own internal data structures. The resulting object will hold 0072 //! a copy of upstream, but will not own the resource to which upstream points. 0073 //! [ Note: The intention is that calls to upstream->allocate() will be 0074 //! substantially fewer than calls to this->allocate() in most cases. - end note 0075 //! The behavior of the pooling mechanism is tuned according to the value of 0076 //! the opts argument. 0077 //! 0078 //! <b>Throws</b>: Nothing unless upstream->allocate() throws. It is unspecified if 0079 //! or under what conditions this constructor calls upstream->allocate(). 0080 unsynchronized_pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT; 0081 0082 //! <b>Effects</b>: Same as 0083 //! `unsynchronized_pool_resource(pool_options(), get_default_resource())`. 0084 unsynchronized_pool_resource() BOOST_NOEXCEPT; 0085 0086 //! <b>Effects</b>: Same as 0087 //! `unsynchronized_pool_resource(pool_options(), upstream)`. 0088 explicit unsynchronized_pool_resource(memory_resource* upstream) BOOST_NOEXCEPT; 0089 0090 //! <b>Effects</b>: Same as 0091 //! `unsynchronized_pool_resource(opts, get_default_resource())`. 0092 explicit unsynchronized_pool_resource(const pool_options& opts) BOOST_NOEXCEPT; 0093 0094 #if !defined(BOOST_NO_CXX11_DELETED_FUNCTIONS) || defined(BOOST_CONTAINER_DOXYGEN_INVOKED) 0095 unsynchronized_pool_resource(const unsynchronized_pool_resource&) = delete; 0096 unsynchronized_pool_resource operator=(const unsynchronized_pool_resource&) = delete; 0097 #else 0098 private: 0099 unsynchronized_pool_resource (const unsynchronized_pool_resource&); 0100 unsynchronized_pool_resource operator=(const unsynchronized_pool_resource&); 0101 public: 0102 #endif 0103 0104 //! <b>Effects</b>: Calls 0105 //! `this->release()`. 0106 ~unsynchronized_pool_resource() BOOST_OVERRIDE; 0107 0108 //! <b>Effects</b>: Calls Calls `upstream_resource()->deallocate()` as necessary 0109 //! to release all allocated memory. [ Note: memory is released back to 0110 //! `upstream_resource()` even if deallocate has not been called for some 0111 //! of the allocated blocks. - end note ] 0112 void release(); 0113 0114 //! <b>Returns</b>: The value of the upstream argument provided to the 0115 //! constructor of this object. 0116 memory_resource* upstream_resource() const; 0117 0118 //! <b>Returns</b>: The options that control the pooling behavior of this resource. 0119 //! The values in the returned struct may differ from those supplied to the pool 0120 //! resource constructor in that values of zero will be replaced with 0121 //! implementation-defined defaults and sizes may be rounded to unspecified granularity. 0122 pool_options options() const; 0123 0124 protected: 0125 0126 //! <b>Returns</b>: A pointer to allocated storage with a size of at least `bytes`. 0127 //! The size and alignment of the allocated memory shall meet the requirements for 0128 //! a class derived from `memory_resource`. 0129 //! 0130 //! <b>Effects</b>: If the pool selected for a block of size bytes is unable to 0131 //! satisfy the memory request from its own internal data structures, it will call 0132 //! `upstream_resource()->allocate()` to obtain more memory. If `bytes` is larger 0133 //! than that which the largest pool can handle, then memory will be allocated 0134 //! using `upstream_resource()->allocate()`. 0135 //! 0136 //! <b>Throws</b>: Nothing unless `upstream_resource()->allocate()` throws. 0137 virtual void* do_allocate(std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE; 0138 0139 //! <b>Effects</b>: Return the memory at p to the pool. It is unspecified if or under 0140 //! what circumstances this operation will result in a call to 0141 //! `upstream_resource()->deallocate()`. 0142 //! 0143 //! <b>Throws</b>: Nothing. 0144 virtual void do_deallocate(void* p, std::size_t bytes, std::size_t alignment) BOOST_OVERRIDE; 0145 0146 //! <b>Returns</b>: 0147 //! `this == dynamic_cast<const unsynchronized_pool_resource*>(&other)`. 0148 virtual bool do_is_equal(const memory_resource& other) const BOOST_NOEXCEPT BOOST_OVERRIDE; 0149 0150 //Non-standard observers 0151 public: 0152 //! <b>Returns</b>: The number of pools that will be used in the pool resource. 0153 //! 0154 //! <b>Note</b>: Non-standard extension. 0155 std::size_t pool_count() const; 0156 0157 //! <b>Returns</b>: The index of the pool that will be used to serve the allocation of `bytes`. 0158 //! Returns `pool_count()` if `bytes` is bigger 0159 //! than `options().largest_required_pool_block` (no pool will be used to serve this). 0160 //! 0161 //! <b>Note</b>: Non-standard extension. 0162 std::size_t pool_index(std::size_t bytes) const; 0163 0164 //! <b>Requires</b>: `pool_idx < pool_index()` 0165 //! 0166 //! <b>Returns</b>: The number blocks that will be allocated in the next chunk 0167 //! from the pool specified by `pool_idx`. 0168 //! 0169 //! <b>Note</b>: Non-standard extension. 0170 std::size_t pool_next_blocks_per_chunk(std::size_t pool_idx) const; 0171 0172 //! <b>Requires</b>: `pool_idx < pool_index()` 0173 //! 0174 //! <b>Returns</b>: The number of bytes of the block that the specified `pool_idx` pool manages. 0175 //! 0176 //! <b>Note</b>: Non-standard extension. 0177 std::size_t pool_block(std::size_t pool_idx) const; 0178 0179 //! <b>Requires</b>: `pool_idx < pool_index()` 0180 //! 0181 //! <b>Returns</b>: The number of blocks that the specified `pool_idx` pool has cached 0182 //! and will be served without calling the upstream_allocator. 0183 //! 0184 //! <b>Note</b>: Non-standard extension. 0185 std::size_t pool_cached_blocks(std::size_t pool_idx) const; 0186 }; 0187 0188 } //namespace pmr { 0189 } //namespace container { 0190 } //namespace boost { 0191 0192 #include <boost/container/detail/config_end.hpp> 0193 0194 #endif //BOOST_CONTAINER_PMR_UNSYNCHRONIZED_POOL_RESOURCE_HPP
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |