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
11#define BOOST_CONTAINER_SOURCE
12#include <boost/container/detail/config_begin.hpp>
13#include <boost/container/detail/workaround.hpp>
14
15#include <boost/container/pmr/global_resource.hpp>
16
17#include <boost/container/detail/pool_resource.hpp>
18#include <boost/container/detail/block_slist.hpp>
19#include <boost/container/detail/min_max.hpp>
20#include <boost/container/detail/placement_new.hpp>
21#include <boost/intrusive/linear_slist_algorithms.hpp>
22#include <boost/intrusive/detail/math.hpp>
23
24#include <cstddef>
25
26namespace boost {
27namespace container {
28namespace pmr {
29
30//pool_data_t
31
32class pool_data_t
33 : public block_slist_base<>
34{
35 typedef block_slist_base<> block_slist_base_t;
36
37 public:
38 explicit pool_data_t(std::size_t initial_blocks_per_chunk)
39 : block_slist_base_t(), next_blocks_per_chunk(initial_blocks_per_chunk)
40 { slist_algo::init_header(this_node: &free_slist); }
41
42 void *allocate_block() BOOST_NOEXCEPT
43 {
44 if(slist_algo::unique(this_node: &free_slist)){
45 return 0;
46 }
47 slist_node *pv = slist_algo::node_traits::get_next(n: &free_slist);
48 slist_algo::unlink_after(prev_node: &free_slist);
49 pv->~slist_node();
50 return pv;
51 }
52
53 void deallocate_block(void *p) BOOST_NOEXCEPT
54 {
55 slist_node *pv = ::new(p, boost_container_new_t()) slist_node();
56 slist_algo::link_after(prev_node: &free_slist, this_node: pv);
57 }
58
59 void release(memory_resource &upstream)
60 {
61 slist_algo::init_header(this_node: &free_slist);
62 this->block_slist_base_t::release(mr&: upstream);
63 next_blocks_per_chunk = pool_options_minimum_max_blocks_per_chunk;
64 }
65
66 void replenish(memory_resource &mr, std::size_t pool_block, std::size_t max_blocks_per_chunk)
67 {
68 //Limit max value
69 std::size_t blocks_per_chunk = boost::container::dtl::min_value(a: max_blocks_per_chunk, b: next_blocks_per_chunk);
70 //Avoid overflow
71 blocks_per_chunk = boost::container::dtl::min_value(a: blocks_per_chunk, b: std::size_t(-1)/pool_block);
72
73 //Minimum block size is at least max_align, so all pools allocate sizes that are multiple of max_align,
74 //meaning that all blocks are max_align-aligned.
75 char *p = static_cast<char *>(block_slist_base_t::allocate(size: blocks_per_chunk*pool_block, mr));
76
77 //Create header types. This is no-throw
78 for(std::size_t i = 0, max = blocks_per_chunk; i != max; ++i){
79 slist_node *const pv = ::new(p, boost_container_new_t()) slist_node();
80 slist_algo::link_after(prev_node: &free_slist, this_node: pv);
81 p += pool_block;
82 }
83
84 //Update next block per chunk
85 next_blocks_per_chunk = max_blocks_per_chunk/2u < blocks_per_chunk ? max_blocks_per_chunk : blocks_per_chunk*2u;
86 }
87
88 std::size_t cache_count() const
89 { return slist_algo::count(this_node: &free_slist) - 1u; }
90
91 slist_node free_slist;
92 std::size_t next_blocks_per_chunk;
93};
94
95//pool_resource
96
97//Detect overflow in ceil_pow2
98BOOST_CONTAINER_STATIC_ASSERT(pool_options_default_max_blocks_per_chunk <= (std::size_t(-1)/2u+1u));
99//Sanity checks
100BOOST_CONTAINER_STATIC_ASSERT(bi::detail::static_is_pow2<pool_options_default_max_blocks_per_chunk>::value);
101BOOST_CONTAINER_STATIC_ASSERT(bi::detail::static_is_pow2<pool_options_minimum_largest_required_pool_block>::value);
102
103//unsynchronized_pool_resource
104
105void pool_resource::priv_limit_option(std::size_t &val, std::size_t min, std::size_t max) //static
106{
107 if(!val){
108 val = max;
109 }
110 else{
111 val = val < min ? min : boost::container::dtl::min_value(a: val, b: max);
112 }
113}
114
115std::size_t pool_resource::priv_pool_index(std::size_t block_size) //static
116{
117 //For allocations equal or less than pool_options_minimum_largest_required_pool_block
118 //the smallest pool is used
119 block_size = boost::container::dtl::max_value(a: block_size, b: pool_options_minimum_largest_required_pool_block);
120 return bi::detail::ceil_log2(x: block_size)
121 - bi::detail::ceil_log2(x: pool_options_minimum_largest_required_pool_block);
122}
123
124std::size_t pool_resource::priv_pool_block(std::size_t index) //static
125{
126 //For allocations equal or less than pool_options_minimum_largest_required_pool_block
127 //the smallest pool is used
128 return pool_options_minimum_largest_required_pool_block << index;
129}
130
131void pool_resource::priv_fix_options()
132{
133 priv_limit_option(val&: m_options.max_blocks_per_chunk
134 , min: pool_options_minimum_max_blocks_per_chunk
135 , max: pool_options_default_max_blocks_per_chunk);
136 priv_limit_option
137 ( val&: m_options.largest_required_pool_block
138 , min: pool_options_minimum_largest_required_pool_block
139 , max: pool_options_default_largest_required_pool_block);
140 m_options.largest_required_pool_block = bi::detail::ceil_pow2(x: m_options.largest_required_pool_block);
141}
142
143void pool_resource::priv_init_pools()
144{
145 const std::size_t num_pools = priv_pool_index(block_size: m_options.largest_required_pool_block)+1u;
146 //Otherwise, just use the default alloc (zero pools)
147 void *p = 0;
148 //This can throw
149 p = m_upstream.allocate(bytes: sizeof(pool_data_t)*num_pools);
150 //This is nothrow
151 m_pool_data = static_cast<pool_data_t *>(p);
152 for(std::size_t i = 0, max = num_pools; i != max; ++i){
153 ::new(&m_pool_data[i], boost_container_new_t()) pool_data_t(pool_options_minimum_max_blocks_per_chunk);
154 }
155 m_pool_count = num_pools;
156}
157
158void pool_resource::priv_constructor_body()
159{
160 this->priv_fix_options();
161}
162
163pool_resource::pool_resource(const pool_options& opts, memory_resource* upstream) BOOST_NOEXCEPT
164 : m_options(opts), m_upstream(*upstream), m_oversized_list(), m_pool_data(), m_pool_count()
165{ this->priv_constructor_body(); }
166
167pool_resource::pool_resource() BOOST_NOEXCEPT
168 : m_options(), m_upstream(*get_default_resource()), m_oversized_list(), m_pool_data(), m_pool_count()
169{ this->priv_constructor_body(); }
170
171pool_resource::pool_resource(memory_resource* upstream) BOOST_NOEXCEPT
172 : m_options(), m_upstream(*upstream), m_oversized_list(), m_pool_data(), m_pool_count()
173{ this->priv_constructor_body(); }
174
175pool_resource::pool_resource(const pool_options& opts) BOOST_NOEXCEPT
176 : m_options(opts), m_upstream(*get_default_resource()), m_oversized_list(), m_pool_data(), m_pool_count()
177{ this->priv_constructor_body(); }
178
179pool_resource::~pool_resource()
180{
181 this->release();
182
183 for(std::size_t i = 0, max = m_pool_count; i != max; ++i){
184 m_pool_data[i].~pool_data_t();
185 }
186 if(m_pool_data){
187 m_upstream.deallocate(p: (void*)m_pool_data, bytes: sizeof(pool_data_t)*m_pool_count);
188 }
189}
190
191void pool_resource::release()
192{
193 m_oversized_list.release(mr&: m_upstream);
194 for(std::size_t i = 0, max = m_pool_count; i != max; ++i)
195 {
196 m_pool_data[i].release(upstream&: m_upstream);
197 }
198}
199
200memory_resource* pool_resource::upstream_resource() const
201{ return &m_upstream; }
202
203pool_options pool_resource::options() const
204{ return m_options; }
205
206void* pool_resource::do_allocate(std::size_t bytes, std::size_t alignment)
207{
208 if(!m_pool_data){
209 this->priv_init_pools();
210 }
211 (void)alignment; //alignment ignored here, max_align is used by pools
212 if(bytes > m_options.largest_required_pool_block){
213 return m_oversized_list.allocate(size: bytes, mr&: m_upstream);
214 }
215 else{
216 const std::size_t pool_idx = priv_pool_index(block_size: bytes);
217 pool_data_t & pool = m_pool_data[pool_idx];
218 void *p = pool.allocate_block();
219 if(!p){
220 pool.replenish(mr&: m_upstream, pool_block: priv_pool_block(index: pool_idx), max_blocks_per_chunk: m_options.max_blocks_per_chunk);
221 p = pool.allocate_block();
222 }
223 return p;
224 }
225}
226
227void pool_resource::do_deallocate(void* p, std::size_t bytes, std::size_t alignment)
228{
229 (void)alignment; //alignment ignored here, max_align is used by pools
230 if(bytes > m_options.largest_required_pool_block){
231 //Just cached
232 return m_oversized_list.deallocate(p, mr&: m_upstream);
233 }
234 else{
235 const std::size_t pool_idx = priv_pool_index(block_size: bytes);
236 return m_pool_data[pool_idx].deallocate_block(p);
237 }
238}
239
240std::size_t pool_resource::pool_count() const
241{
242 if(BOOST_LIKELY((0 != m_pool_data))){
243 return m_pool_count;
244 }
245 else{
246 return priv_pool_index(block_size: m_options.largest_required_pool_block)+1u;
247 }
248}
249
250std::size_t pool_resource::pool_index(std::size_t bytes) const
251{
252 if(bytes > m_options.largest_required_pool_block){
253 return pool_count();
254 }
255 else{
256 return priv_pool_index(block_size: bytes);
257 }
258}
259
260std::size_t pool_resource::pool_next_blocks_per_chunk(std::size_t pool_idx) const
261{
262 if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
263 return m_pool_data[pool_idx].next_blocks_per_chunk;
264 }
265 else{
266 return 1u;
267 }
268}
269
270std::size_t pool_resource::pool_block(std::size_t pool_idx) const
271{ return priv_pool_block(index: pool_idx); }
272
273std::size_t pool_resource::pool_cached_blocks(std::size_t pool_idx) const
274{
275 if(BOOST_LIKELY((m_pool_data && pool_idx < m_pool_count))){
276 return m_pool_data[pool_idx].cache_count();
277 }
278 else{
279 return 0u;
280 }
281}
282
283} //namespace pmr {
284} //namespace container {
285} //namespace boost {
286
287#include <boost/container/detail/config_end.hpp>
288

source code of boost/libs/container/src/pool_resource.cpp