Chris@16
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
2 //
|
Chris@16
|
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
|
Chris@16
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@16
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 //
|
Chris@16
|
7 // See http://www.boost.org/libs/interprocess for documentation.
|
Chris@16
|
8 //
|
Chris@16
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP
|
Chris@16
|
12 #define BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP
|
Chris@16
|
13
|
Chris@101
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@101
|
15 # include <boost/config.hpp>
|
Chris@101
|
16 #endif
|
Chris@101
|
17 #
|
Chris@101
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@16
|
19 # pragma once
|
Chris@16
|
20 #endif
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/interprocess/detail/config_begin.hpp>
|
Chris@16
|
23 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
24
|
Chris@16
|
25 #include <boost/interprocess/interprocess_fwd.hpp>
|
Chris@16
|
26 #include <boost/interprocess/allocators/detail/adaptive_node_pool.hpp>
|
Chris@16
|
27 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
|
Chris@16
|
28 #include <boost/interprocess/detail/utilities.hpp>
|
Chris@16
|
29 #include <boost/interprocess/detail/workaround.hpp>
|
Chris@16
|
30 #include <boost/interprocess/containers/version_type.hpp>
|
Chris@16
|
31 #include <boost/interprocess/allocators/detail/node_tools.hpp>
|
Chris@16
|
32 #include <cstddef>
|
Chris@16
|
33
|
Chris@16
|
34 //!\file
|
Chris@16
|
35 //!Describes cached_adaptive_pool pooled shared memory STL compatible allocator
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost {
|
Chris@16
|
38 namespace interprocess {
|
Chris@16
|
39
|
Chris@101
|
40 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
41
|
Chris@16
|
42 namespace ipcdetail {
|
Chris@16
|
43
|
Chris@16
|
44 template < class T
|
Chris@16
|
45 , class SegmentManager
|
Chris@16
|
46 , std::size_t NodesPerBlock = 64
|
Chris@16
|
47 , std::size_t MaxFreeBlocks = 2
|
Chris@16
|
48 , unsigned char OverheadPercent = 5
|
Chris@16
|
49 >
|
Chris@16
|
50 class cached_adaptive_pool_v1
|
Chris@16
|
51 : public ipcdetail::cached_allocator_impl
|
Chris@16
|
52 < T
|
Chris@16
|
53 , ipcdetail::shared_adaptive_node_pool
|
Chris@16
|
54 < SegmentManager
|
Chris@16
|
55 , sizeof_value<T>::value
|
Chris@16
|
56 , NodesPerBlock
|
Chris@16
|
57 , MaxFreeBlocks
|
Chris@16
|
58 , OverheadPercent
|
Chris@16
|
59 >
|
Chris@16
|
60 , 1>
|
Chris@16
|
61 {
|
Chris@16
|
62 public:
|
Chris@16
|
63 typedef ipcdetail::cached_allocator_impl
|
Chris@16
|
64 < T
|
Chris@16
|
65 , ipcdetail::shared_adaptive_node_pool
|
Chris@16
|
66 < SegmentManager
|
Chris@16
|
67 , sizeof_value<T>::value
|
Chris@16
|
68 , NodesPerBlock
|
Chris@16
|
69 , MaxFreeBlocks
|
Chris@16
|
70 , OverheadPercent
|
Chris@16
|
71 >
|
Chris@16
|
72 , 1> base_t;
|
Chris@16
|
73
|
Chris@16
|
74 template<class T2>
|
Chris@16
|
75 struct rebind
|
Chris@16
|
76 {
|
Chris@16
|
77 typedef cached_adaptive_pool_v1
|
Chris@16
|
78 <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
|
Chris@16
|
79 };
|
Chris@16
|
80
|
Chris@16
|
81 typedef typename base_t::size_type size_type;
|
Chris@16
|
82
|
Chris@16
|
83 cached_adaptive_pool_v1(SegmentManager *segment_mngr,
|
Chris@16
|
84 size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
|
Chris@16
|
85 : base_t(segment_mngr, max_cached_nodes)
|
Chris@16
|
86 {}
|
Chris@16
|
87
|
Chris@16
|
88 template<class T2>
|
Chris@16
|
89 cached_adaptive_pool_v1
|
Chris@16
|
90 (const cached_adaptive_pool_v1
|
Chris@16
|
91 <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
|
Chris@16
|
92 : base_t(other)
|
Chris@16
|
93 {}
|
Chris@16
|
94 };
|
Chris@16
|
95
|
Chris@16
|
96 } //namespace ipcdetail{
|
Chris@16
|
97
|
Chris@101
|
98 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
99
|
Chris@16
|
100 //!An STL node allocator that uses a segment manager as memory
|
Chris@16
|
101 //!source. The internal pointer type will of the same type (raw, smart) as
|
Chris@16
|
102 //!"typename SegmentManager::void_pointer" type. This allows
|
Chris@16
|
103 //!placing the allocator in shared memory, memory mapped-files, etc...
|
Chris@16
|
104 //!
|
Chris@16
|
105 //!This node allocator shares a segregated storage between all instances of
|
Chris@16
|
106 //!cached_adaptive_pool with equal sizeof(T) placed in the same
|
Chris@16
|
107 //!memory segment. But also caches some nodes privately to
|
Chris@16
|
108 //!avoid some synchronization overhead.
|
Chris@16
|
109 //!
|
Chris@16
|
110 //!NodesPerBlock is the minimum number of nodes of nodes allocated at once when
|
Chris@16
|
111 //!the allocator needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
|
Chris@16
|
112 //!that the adaptive node pool will hold. The rest of the totally free blocks will be
|
Chris@16
|
113 //!deallocated with the segment manager.
|
Chris@16
|
114 //!
|
Chris@16
|
115 //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
|
Chris@16
|
116 //!(memory usable for nodes / total memory allocated from the segment manager)
|
Chris@16
|
117 template < class T
|
Chris@16
|
118 , class SegmentManager
|
Chris@16
|
119 , std::size_t NodesPerBlock
|
Chris@16
|
120 , std::size_t MaxFreeBlocks
|
Chris@16
|
121 , unsigned char OverheadPercent
|
Chris@16
|
122 >
|
Chris@16
|
123 class cached_adaptive_pool
|
Chris@101
|
124 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
|
Chris@16
|
125 : public ipcdetail::cached_allocator_impl
|
Chris@16
|
126 < T
|
Chris@16
|
127 , ipcdetail::shared_adaptive_node_pool
|
Chris@16
|
128 < SegmentManager
|
Chris@16
|
129 , sizeof_value<T>::value
|
Chris@16
|
130 , NodesPerBlock
|
Chris@16
|
131 , MaxFreeBlocks
|
Chris@16
|
132 , OverheadPercent
|
Chris@16
|
133 >
|
Chris@16
|
134 , 2>
|
Chris@101
|
135 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
136 {
|
Chris@16
|
137
|
Chris@16
|
138 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
139 public:
|
Chris@16
|
140 typedef ipcdetail::cached_allocator_impl
|
Chris@16
|
141 < T
|
Chris@16
|
142 , ipcdetail::shared_adaptive_node_pool
|
Chris@16
|
143 < SegmentManager
|
Chris@16
|
144 , sizeof_value<T>::value
|
Chris@16
|
145 , NodesPerBlock
|
Chris@16
|
146 , MaxFreeBlocks
|
Chris@16
|
147 , OverheadPercent
|
Chris@16
|
148 >
|
Chris@16
|
149 , 2> base_t;
|
Chris@16
|
150
|
Chris@16
|
151 public:
|
Chris@16
|
152 typedef boost::interprocess::version_type<cached_adaptive_pool, 2> version;
|
Chris@16
|
153
|
Chris@16
|
154 template<class T2>
|
Chris@16
|
155 struct rebind
|
Chris@16
|
156 {
|
Chris@16
|
157 typedef cached_adaptive_pool
|
Chris@16
|
158 <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
|
Chris@16
|
159 };
|
Chris@16
|
160
|
Chris@16
|
161 cached_adaptive_pool(SegmentManager *segment_mngr,
|
Chris@16
|
162 std::size_t max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
|
Chris@16
|
163 : base_t(segment_mngr, max_cached_nodes)
|
Chris@16
|
164 {}
|
Chris@16
|
165
|
Chris@16
|
166 template<class T2>
|
Chris@16
|
167 cached_adaptive_pool
|
Chris@16
|
168 (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
|
Chris@16
|
169 : base_t(other)
|
Chris@16
|
170 {}
|
Chris@16
|
171
|
Chris@16
|
172 #else
|
Chris@16
|
173 public:
|
Chris@16
|
174 typedef implementation_defined::segment_manager segment_manager;
|
Chris@16
|
175 typedef segment_manager::void_pointer void_pointer;
|
Chris@16
|
176 typedef implementation_defined::pointer pointer;
|
Chris@16
|
177 typedef implementation_defined::const_pointer const_pointer;
|
Chris@16
|
178 typedef T value_type;
|
Chris@16
|
179 typedef typename ipcdetail::add_reference
|
Chris@16
|
180 <value_type>::type reference;
|
Chris@16
|
181 typedef typename ipcdetail::add_reference
|
Chris@16
|
182 <const value_type>::type const_reference;
|
Chris@16
|
183 typedef typename segment_manager::size_type size_type;
|
Chris@16
|
184 typedef typename segment_manager::difference_type difference_type;
|
Chris@16
|
185
|
Chris@16
|
186 //!Obtains cached_adaptive_pool from
|
Chris@16
|
187 //!cached_adaptive_pool
|
Chris@16
|
188 template<class T2>
|
Chris@16
|
189 struct rebind
|
Chris@16
|
190 {
|
Chris@16
|
191 typedef cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
|
Chris@16
|
192 };
|
Chris@16
|
193
|
Chris@16
|
194 private:
|
Chris@16
|
195 //!Not assignable from
|
Chris@16
|
196 //!related cached_adaptive_pool
|
Chris@16
|
197 template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
|
Chris@16
|
198 cached_adaptive_pool& operator=
|
Chris@16
|
199 (const cached_adaptive_pool<T2, SegmentManager2, N2, F2, OP2>&);
|
Chris@16
|
200
|
Chris@16
|
201 //!Not assignable from
|
Chris@16
|
202 //!other cached_adaptive_pool
|
Chris@16
|
203 cached_adaptive_pool& operator=(const cached_adaptive_pool&);
|
Chris@16
|
204
|
Chris@16
|
205 public:
|
Chris@16
|
206 //!Constructor from a segment manager. If not present, constructs a node
|
Chris@16
|
207 //!pool. Increments the reference count of the associated node pool.
|
Chris@16
|
208 //!Can throw boost::interprocess::bad_alloc
|
Chris@16
|
209 cached_adaptive_pool(segment_manager *segment_mngr);
|
Chris@16
|
210
|
Chris@16
|
211 //!Copy constructor from other cached_adaptive_pool. Increments the reference
|
Chris@16
|
212 //!count of the associated node pool. Never throws
|
Chris@16
|
213 cached_adaptive_pool(const cached_adaptive_pool &other);
|
Chris@16
|
214
|
Chris@16
|
215 //!Copy constructor from related cached_adaptive_pool. If not present, constructs
|
Chris@16
|
216 //!a node pool. Increments the reference count of the associated node pool.
|
Chris@16
|
217 //!Can throw boost::interprocess::bad_alloc
|
Chris@16
|
218 template<class T2>
|
Chris@16
|
219 cached_adaptive_pool
|
Chris@16
|
220 (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
|
Chris@16
|
221
|
Chris@16
|
222 //!Destructor, removes node_pool_t from memory
|
Chris@16
|
223 //!if its reference count reaches to zero. Never throws
|
Chris@16
|
224 ~cached_adaptive_pool();
|
Chris@16
|
225
|
Chris@16
|
226 //!Returns a pointer to the node pool.
|
Chris@16
|
227 //!Never throws
|
Chris@16
|
228 node_pool_t* get_node_pool() const;
|
Chris@16
|
229
|
Chris@16
|
230 //!Returns the segment manager.
|
Chris@16
|
231 //!Never throws
|
Chris@16
|
232 segment_manager* get_segment_manager()const;
|
Chris@16
|
233
|
Chris@16
|
234 //!Returns the number of elements that could be allocated.
|
Chris@16
|
235 //!Never throws
|
Chris@16
|
236 size_type max_size() const;
|
Chris@16
|
237
|
Chris@16
|
238 //!Allocate memory for an array of count elements.
|
Chris@16
|
239 //!Throws boost::interprocess::bad_alloc if there is no enough memory
|
Chris@16
|
240 pointer allocate(size_type count, cvoid_pointer hint = 0);
|
Chris@16
|
241
|
Chris@16
|
242 //!Deallocate allocated memory.
|
Chris@16
|
243 //!Never throws
|
Chris@16
|
244 void deallocate(const pointer &ptr, size_type count);
|
Chris@16
|
245
|
Chris@16
|
246 //!Deallocates all free blocks
|
Chris@16
|
247 //!of the pool
|
Chris@16
|
248 void deallocate_free_blocks();
|
Chris@16
|
249
|
Chris@16
|
250 //!Swaps allocators. Does not throw. If each allocator is placed in a
|
Chris@16
|
251 //!different memory segment, the result is undefined.
|
Chris@16
|
252 friend void swap(self_t &alloc1, self_t &alloc2);
|
Chris@16
|
253
|
Chris@16
|
254 //!Returns address of mutable object.
|
Chris@16
|
255 //!Never throws
|
Chris@16
|
256 pointer address(reference value) const;
|
Chris@16
|
257
|
Chris@16
|
258 //!Returns address of non mutable object.
|
Chris@16
|
259 //!Never throws
|
Chris@16
|
260 const_pointer address(const_reference value) const;
|
Chris@16
|
261
|
Chris@16
|
262 //!Copy construct an object.
|
Chris@16
|
263 //!Throws if T's copy constructor throws
|
Chris@16
|
264 void construct(const pointer &ptr, const_reference v);
|
Chris@16
|
265
|
Chris@16
|
266 //!Destroys object. Throws if object's
|
Chris@16
|
267 //!destructor throws
|
Chris@16
|
268 void destroy(const pointer &ptr);
|
Chris@16
|
269
|
Chris@16
|
270 //!Returns maximum the number of objects the previously allocated memory
|
Chris@16
|
271 //!pointed by p can hold. This size only works for memory allocated with
|
Chris@16
|
272 //!allocate, allocation_command and allocate_many.
|
Chris@16
|
273 size_type size(const pointer &p) const;
|
Chris@16
|
274
|
Chris@101
|
275 pointer allocation_command(boost::interprocess::allocation_type command,
|
Chris@101
|
276 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
|
Chris@16
|
277
|
Chris@16
|
278 //!Allocates many elements of size elem_size in a contiguous block
|
Chris@16
|
279 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
280 //!the preferred and maximum number is
|
Chris@16
|
281 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
282 //!will be assigned to received_size. The elements must be deallocated
|
Chris@16
|
283 //!with deallocate(...)
|
Chris@16
|
284 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
|
Chris@16
|
285
|
Chris@16
|
286 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
|
Chris@16
|
287 //!contiguous block
|
Chris@16
|
288 //!of memory. The elements must be deallocated
|
Chris@16
|
289 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
|
Chris@16
|
290
|
Chris@16
|
291 //!Allocates many elements of size elem_size in a contiguous block
|
Chris@16
|
292 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
293 //!the preferred and maximum number is
|
Chris@16
|
294 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
295 //!will be assigned to received_size. The elements must be deallocated
|
Chris@16
|
296 //!with deallocate(...)
|
Chris@16
|
297 void deallocate_many(multiallocation_chain &chain);
|
Chris@16
|
298
|
Chris@16
|
299 //!Allocates just one object. Memory allocated with this function
|
Chris@16
|
300 //!must be deallocated only with deallocate_one().
|
Chris@16
|
301 //!Throws boost::interprocess::bad_alloc if there is no enough memory
|
Chris@16
|
302 pointer allocate_one();
|
Chris@16
|
303
|
Chris@16
|
304 //!Allocates many elements of size == 1 in a contiguous block
|
Chris@16
|
305 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
306 //!the preferred and maximum number is
|
Chris@16
|
307 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
308 //!will be assigned to received_size. Memory allocated with this function
|
Chris@16
|
309 //!must be deallocated only with deallocate_one().
|
Chris@16
|
310 multiallocation_chain allocate_individual(size_type num_elements);
|
Chris@16
|
311
|
Chris@16
|
312 //!Deallocates memory previously allocated with allocate_one().
|
Chris@16
|
313 //!You should never use deallocate_one to deallocate memory allocated
|
Chris@16
|
314 //!with other functions different from allocate_one(). Never throws
|
Chris@16
|
315 void deallocate_one(const pointer &p);
|
Chris@16
|
316
|
Chris@16
|
317 //!Allocates many elements of size == 1 in a contiguous block
|
Chris@16
|
318 //!of memory. The minimum number to be allocated is min_elements,
|
Chris@16
|
319 //!the preferred and maximum number is
|
Chris@16
|
320 //!preferred_elements. The number of actually allocated elements is
|
Chris@16
|
321 //!will be assigned to received_size. Memory allocated with this function
|
Chris@16
|
322 //!must be deallocated only with deallocate_one().
|
Chris@16
|
323 void deallocate_individual(multiallocation_chain &chain);
|
Chris@16
|
324 //!Sets the new max cached nodes value. This can provoke deallocations
|
Chris@16
|
325 //!if "newmax" is less than current cached nodes. Never throws
|
Chris@16
|
326 void set_max_cached_nodes(size_type newmax);
|
Chris@16
|
327
|
Chris@16
|
328 //!Returns the max cached nodes parameter.
|
Chris@16
|
329 //!Never throws
|
Chris@16
|
330 size_type get_max_cached_nodes() const;
|
Chris@16
|
331 #endif
|
Chris@16
|
332 };
|
Chris@16
|
333
|
Chris@16
|
334 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
|
Chris@16
|
335
|
Chris@16
|
336 //!Equality test for same type
|
Chris@16
|
337 //!of cached_adaptive_pool
|
Chris@16
|
338 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
|
Chris@16
|
339 bool operator==(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
|
Chris@16
|
340 const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
|
Chris@16
|
341
|
Chris@16
|
342 //!Inequality test for same type
|
Chris@16
|
343 //!of cached_adaptive_pool
|
Chris@16
|
344 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
|
Chris@16
|
345 bool operator!=(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
|
Chris@16
|
346 const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
|
Chris@16
|
347
|
Chris@16
|
348 #endif
|
Chris@16
|
349
|
Chris@16
|
350 } //namespace interprocess {
|
Chris@16
|
351 } //namespace boost {
|
Chris@16
|
352
|
Chris@16
|
353
|
Chris@16
|
354 #include <boost/interprocess/detail/config_end.hpp>
|
Chris@16
|
355
|
Chris@16
|
356 #endif //#ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP
|
Chris@16
|
357
|