1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. 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/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP
12 #define BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/interprocess_fwd.hpp>
26 #include <boost/interprocess/allocators/detail/node_pool.hpp>
27 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
28 #include <boost/interprocess/detail/workaround.hpp>
29 #include <boost/interprocess/detail/utilities.hpp>
30 #include <boost/interprocess/containers/version_type.hpp>
31 #include <boost/interprocess/allocators/detail/node_tools.hpp>
35 //!Describes cached_cached_node_allocator pooled shared memory STL compatible allocator
38 namespace interprocess {
41 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
46 , class SegmentManager
47 , std::size_t NodesPerBlock = 64
49 class cached_node_allocator_v1
50 : public ipcdetail::cached_allocator_impl
52 , ipcdetail::shared_node_pool
54 , sizeof_value<T>::value
60 typedef ipcdetail::cached_allocator_impl
62 , ipcdetail::shared_node_pool
64 , sizeof_value<T>::value
72 typedef cached_node_allocator_v1
73 <T2, SegmentManager, NodesPerBlock> other;
76 typedef typename base_t::size_type size_type;
78 cached_node_allocator_v1(SegmentManager *segment_mngr,
79 size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
80 : base_t(segment_mngr, max_cached_nodes)
84 cached_node_allocator_v1
85 (const cached_node_allocator_v1
86 <T2, SegmentManager, NodesPerBlock> &other)
91 } //namespace ipcdetail{
93 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
96 , class SegmentManager
97 , std::size_t NodesPerBlock
99 class cached_node_allocator
100 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
101 : public ipcdetail::cached_allocator_impl
103 , ipcdetail::shared_node_pool
105 , sizeof_value<T>::value
109 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
112 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
114 typedef ipcdetail::cached_allocator_impl
116 , ipcdetail::shared_node_pool
118 , sizeof_value<T>::value
124 typedef boost::interprocess::version_type<cached_node_allocator, 2> version;
125 typedef typename base_t::size_type size_type;
130 typedef cached_node_allocator<T2, SegmentManager, NodesPerBlock> other;
133 cached_node_allocator(SegmentManager *segment_mngr,
134 size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
135 : base_t(segment_mngr, max_cached_nodes)
139 cached_node_allocator
140 (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other)
146 typedef implementation_defined::segment_manager segment_manager;
147 typedef segment_manager::void_pointer void_pointer;
148 typedef implementation_defined::pointer pointer;
149 typedef implementation_defined::const_pointer const_pointer;
150 typedef T value_type;
151 typedef typename ipcdetail::add_reference
152 <value_type>::type reference;
153 typedef typename ipcdetail::add_reference
154 <const value_type>::type const_reference;
155 typedef typename SegmentManager::size_type size_type;
156 typedef typename SegmentManager::difference_type difference_type;
158 //!Obtains cached_node_allocator from
159 //!cached_node_allocator
163 typedef cached_node_allocator<T2, SegmentManager> other;
167 //!Not assignable from
168 //!related cached_node_allocator
169 template<class T2, class SegmentManager2, std::size_t N2>
170 cached_node_allocator& operator=
171 (const cached_node_allocator<T2, SegmentManager2, N2>&);
173 //!Not assignable from
174 //!other cached_node_allocator
175 cached_node_allocator& operator=(const cached_node_allocator&);
178 //!Constructor from a segment manager. If not present, constructs a node
179 //!pool. Increments the reference count of the associated node pool.
180 //!Can throw boost::interprocess::bad_alloc
181 cached_node_allocator(segment_manager *segment_mngr);
183 //!Copy constructor from other cached_node_allocator. Increments the reference
184 //!count of the associated node pool. Never throws
185 cached_node_allocator(const cached_node_allocator &other);
187 //!Copy constructor from related cached_node_allocator. If not present, constructs
188 //!a node pool. Increments the reference count of the associated node pool.
189 //!Can throw boost::interprocess::bad_alloc
191 cached_node_allocator
192 (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other);
194 //!Destructor, removes node_pool_t from memory
195 //!if its reference count reaches to zero. Never throws
196 ~cached_node_allocator();
198 //!Returns a pointer to the node pool.
200 node_pool_t* get_node_pool() const;
202 //!Returns the segment manager.
204 segment_manager* get_segment_manager()const;
206 //!Returns the number of elements that could be allocated.
208 size_type max_size() const;
210 //!Allocate memory for an array of count elements.
211 //!Throws boost::interprocess::bad_alloc if there is no enough memory
212 pointer allocate(size_type count, cvoid_pointer hint = 0);
214 //!Deallocate allocated memory.
216 void deallocate(const pointer &ptr, size_type count);
218 //!Deallocates all free blocks
220 void deallocate_free_blocks();
222 //!Swaps allocators. Does not throw. If each allocator is placed in a
223 //!different memory segment, the result is undefined.
224 friend void swap(self_t &alloc1, self_t &alloc2);
226 //!Returns address of mutable object.
228 pointer address(reference value) const;
230 //!Returns address of non mutable object.
232 const_pointer address(const_reference value) const;
234 //!Default construct an object.
235 //!Throws if T's default constructor throws
236 void construct(const pointer &ptr, const_reference v);
238 //!Destroys object. Throws if object's
240 void destroy(const pointer &ptr);
242 //!Returns maximum the number of objects the previously allocated memory
243 //!pointed by p can hold. This size only works for memory allocated with
244 //!allocate, allocation_command and allocate_many.
245 size_type size(const pointer &p) const;
247 pointer allocation_command(boost::interprocess::allocation_type command,
248 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
250 //!Allocates many elements of size elem_size in a contiguous block
251 //!of memory. The minimum number to be allocated is min_elements,
252 //!the preferred and maximum number is
253 //!preferred_elements. The number of actually allocated elements is
254 //!will be assigned to received_size. The elements must be deallocated
255 //!with deallocate(...)
256 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
258 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
260 //!of memory. The elements must be deallocated
261 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
263 //!Allocates many elements of size elem_size in a contiguous block
264 //!of memory. The minimum number to be allocated is min_elements,
265 //!the preferred and maximum number is
266 //!preferred_elements. The number of actually allocated elements is
267 //!will be assigned to received_size. The elements must be deallocated
268 //!with deallocate(...)
269 void deallocate_many(multiallocation_chain &chain);
271 //!Allocates just one object. Memory allocated with this function
272 //!must be deallocated only with deallocate_one().
273 //!Throws boost::interprocess::bad_alloc if there is no enough memory
274 pointer allocate_one();
276 //!Allocates many elements of size == 1 in a contiguous block
277 //!of memory. The minimum number to be allocated is min_elements,
278 //!the preferred and maximum number is
279 //!preferred_elements. The number of actually allocated elements is
280 //!will be assigned to received_size. Memory allocated with this function
281 //!must be deallocated only with deallocate_one().
282 multiallocation_chain allocate_individual(size_type num_elements);
284 //!Deallocates memory previously allocated with allocate_one().
285 //!You should never use deallocate_one to deallocate memory allocated
286 //!with other functions different from allocate_one(). Never throws
287 void deallocate_one(const pointer &p);
289 //!Allocates many elements of size == 1 in a contiguous block
290 //!of memory. The minimum number to be allocated is min_elements,
291 //!the preferred and maximum number is
292 //!preferred_elements. The number of actually allocated elements is
293 //!will be assigned to received_size. Memory allocated with this function
294 //!must be deallocated only with deallocate_one().
295 void deallocate_individual(multiallocation_chain it);
296 //!Sets the new max cached nodes value. This can provoke deallocations
297 //!if "newmax" is less than current cached nodes. Never throws
298 void set_max_cached_nodes(size_type newmax);
300 //!Returns the max cached nodes parameter.
302 size_type get_max_cached_nodes() const;
306 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
308 //!Equality test for same type
309 //!of cached_node_allocator
310 template<class T, class S, std::size_t NPC> inline
311 bool operator==(const cached_node_allocator<T, S, NPC> &alloc1,
312 const cached_node_allocator<T, S, NPC> &alloc2);
314 //!Inequality test for same type
315 //!of cached_node_allocator
316 template<class T, class S, std::size_t NPC> inline
317 bool operator!=(const cached_node_allocator<T, S, NPC> &alloc1,
318 const cached_node_allocator<T, S, NPC> &alloc2);
322 } //namespace interprocess {
323 } //namespace boost {
325 #include <boost/interprocess/detail/config_end.hpp>
327 #endif //#ifndef BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP