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
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
21 #include <boost/interprocess/interprocess_fwd.hpp>
22 #include <boost/interprocess/allocators/detail/node_pool.hpp>
23 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
24 #include <boost/interprocess/detail/workaround.hpp>
25 #include <boost/interprocess/detail/utilities.hpp>
26 #include <boost/interprocess/containers/version_type.hpp>
27 #include <boost/interprocess/allocators/detail/node_tools.hpp>
31 //!Describes cached_cached_node_allocator pooled shared memory STL compatible allocator
34 namespace interprocess {
37 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
42 , class SegmentManager
43 , std::size_t NodesPerBlock = 64
45 class cached_node_allocator_v1
46 : public ipcdetail::cached_allocator_impl
48 , ipcdetail::shared_node_pool
50 , sizeof_value<T>::value
56 typedef ipcdetail::cached_allocator_impl
58 , ipcdetail::shared_node_pool
60 , sizeof_value<T>::value
68 typedef cached_node_allocator_v1
69 <T2, SegmentManager, NodesPerBlock> other;
72 typedef typename base_t::size_type size_type;
74 cached_node_allocator_v1(SegmentManager *segment_mngr,
75 size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
76 : base_t(segment_mngr, max_cached_nodes)
80 cached_node_allocator_v1
81 (const cached_node_allocator_v1
82 <T2, SegmentManager, NodesPerBlock> &other)
87 } //namespace ipcdetail{
89 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
92 , class SegmentManager
93 , std::size_t NodesPerBlock
95 class cached_node_allocator
96 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
97 : public ipcdetail::cached_allocator_impl
99 , ipcdetail::shared_node_pool
101 , sizeof_value<T>::value
105 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
108 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
110 typedef ipcdetail::cached_allocator_impl
112 , ipcdetail::shared_node_pool
114 , sizeof_value<T>::value
120 typedef boost::interprocess::version_type<cached_node_allocator, 2> version;
121 typedef typename base_t::size_type size_type;
126 typedef cached_node_allocator<T2, SegmentManager, NodesPerBlock> other;
129 cached_node_allocator(SegmentManager *segment_mngr,
130 size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
131 : base_t(segment_mngr, max_cached_nodes)
135 cached_node_allocator
136 (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other)
142 typedef implementation_defined::segment_manager segment_manager;
143 typedef segment_manager::void_pointer void_pointer;
144 typedef implementation_defined::pointer pointer;
145 typedef implementation_defined::const_pointer const_pointer;
146 typedef T value_type;
147 typedef typename ipcdetail::add_reference
148 <value_type>::type reference;
149 typedef typename ipcdetail::add_reference
150 <const value_type>::type const_reference;
151 typedef typename SegmentManager::size_type size_type;
152 typedef typename SegmentManager::difference_type difference_type;
154 //!Obtains cached_node_allocator from
155 //!cached_node_allocator
159 typedef cached_node_allocator<T2, SegmentManager> other;
163 //!Not assignable from
164 //!related cached_node_allocator
165 template<class T2, class SegmentManager2, std::size_t N2>
166 cached_node_allocator& operator=
167 (const cached_node_allocator<T2, SegmentManager2, N2>&);
169 //!Not assignable from
170 //!other cached_node_allocator
171 cached_node_allocator& operator=(const cached_node_allocator&);
174 //!Constructor from a segment manager. If not present, constructs a node
175 //!pool. Increments the reference count of the associated node pool.
176 //!Can throw boost::interprocess::bad_alloc
177 cached_node_allocator(segment_manager *segment_mngr);
179 //!Copy constructor from other cached_node_allocator. Increments the reference
180 //!count of the associated node pool. Never throws
181 cached_node_allocator(const cached_node_allocator &other);
183 //!Copy constructor from related cached_node_allocator. If not present, constructs
184 //!a node pool. Increments the reference count of the associated node pool.
185 //!Can throw boost::interprocess::bad_alloc
187 cached_node_allocator
188 (const cached_node_allocator<T2, SegmentManager, NodesPerBlock> &other);
190 //!Destructor, removes node_pool_t from memory
191 //!if its reference count reaches to zero. Never throws
192 ~cached_node_allocator();
194 //!Returns a pointer to the node pool.
196 node_pool_t* get_node_pool() const;
198 //!Returns the segment manager.
200 segment_manager* get_segment_manager()const;
202 //!Returns the number of elements that could be allocated.
204 size_type max_size() const;
206 //!Allocate memory for an array of count elements.
207 //!Throws boost::interprocess::bad_alloc if there is no enough memory
208 pointer allocate(size_type count, cvoid_pointer hint = 0);
210 //!Deallocate allocated memory.
212 void deallocate(const pointer &ptr, size_type count);
214 //!Deallocates all free blocks
216 void deallocate_free_blocks();
218 //!Swaps allocators. Does not throw. If each allocator is placed in a
219 //!different memory segment, the result is undefined.
220 friend void swap(self_t &alloc1, self_t &alloc2);
222 //!Returns address of mutable object.
224 pointer address(reference value) const;
226 //!Returns address of non mutable object.
228 const_pointer address(const_reference value) const;
230 //!Default construct an object.
231 //!Throws if T's default constructor throws
232 void construct(const pointer &ptr, const_reference v);
234 //!Destroys object. Throws if object's
236 void destroy(const pointer &ptr);
238 //!Returns maximum the number of objects the previously allocated memory
239 //!pointed by p can hold. This size only works for memory allocated with
240 //!allocate, allocation_command and allocate_many.
241 size_type size(const pointer &p) const;
243 std::pair<pointer, bool>
244 allocation_command(boost::interprocess::allocation_type command,
245 size_type limit_size,
246 size_type preferred_size,
247 size_type &received_size, const pointer &reuse = 0);
249 //!Allocates many elements of size elem_size in a contiguous block
250 //!of memory. The minimum number to be allocated is min_elements,
251 //!the preferred and maximum number is
252 //!preferred_elements. The number of actually allocated elements is
253 //!will be assigned to received_size. The elements must be deallocated
254 //!with deallocate(...)
255 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
257 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
259 //!of memory. The elements must be deallocated
260 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
262 //!Allocates many elements of size elem_size in a contiguous block
263 //!of memory. The minimum number to be allocated is min_elements,
264 //!the preferred and maximum number is
265 //!preferred_elements. The number of actually allocated elements is
266 //!will be assigned to received_size. The elements must be deallocated
267 //!with deallocate(...)
268 void deallocate_many(multiallocation_chain &chain);
270 //!Allocates just one object. Memory allocated with this function
271 //!must be deallocated only with deallocate_one().
272 //!Throws boost::interprocess::bad_alloc if there is no enough memory
273 pointer allocate_one();
275 //!Allocates many elements of size == 1 in a contiguous block
276 //!of memory. The minimum number to be allocated is min_elements,
277 //!the preferred and maximum number is
278 //!preferred_elements. The number of actually allocated elements is
279 //!will be assigned to received_size. Memory allocated with this function
280 //!must be deallocated only with deallocate_one().
281 multiallocation_chain allocate_individual(size_type num_elements);
283 //!Deallocates memory previously allocated with allocate_one().
284 //!You should never use deallocate_one to deallocate memory allocated
285 //!with other functions different from allocate_one(). Never throws
286 void deallocate_one(const pointer &p);
288 //!Allocates many elements of size == 1 in a contiguous block
289 //!of memory. The minimum number to be allocated is min_elements,
290 //!the preferred and maximum number is
291 //!preferred_elements. The number of actually allocated elements is
292 //!will be assigned to received_size. Memory allocated with this function
293 //!must be deallocated only with deallocate_one().
294 void deallocate_individual(multiallocation_chain it);
295 //!Sets the new max cached nodes value. This can provoke deallocations
296 //!if "newmax" is less than current cached nodes. Never throws
297 void set_max_cached_nodes(size_type newmax);
299 //!Returns the max cached nodes parameter.
301 size_type get_max_cached_nodes() const;
305 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
307 //!Equality test for same type
308 //!of cached_node_allocator
309 template<class T, class S, std::size_t NPC> inline
310 bool operator==(const cached_node_allocator<T, S, NPC> &alloc1,
311 const cached_node_allocator<T, S, NPC> &alloc2);
313 //!Inequality test for same type
314 //!of cached_node_allocator
315 template<class T, class S, std::size_t NPC> inline
316 bool operator!=(const cached_node_allocator<T, S, NPC> &alloc1,
317 const cached_node_allocator<T, S, NPC> &alloc2);
321 } //namespace interprocess {
322 } //namespace boost {
324 #include <boost/interprocess/detail/config_end.hpp>
326 #endif //#ifndef BOOST_INTERPROCESS_CACHED_NODE_ALLOCATOR_HPP