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_PRIVATE_NODE_ALLOCATOR_HPP
12 #define BOOST_INTERPROCESS_PRIVATE_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/intrusive/pointer_traits.hpp>
27 #include <boost/interprocess/interprocess_fwd.hpp>
28 #include <boost/assert.hpp>
29 #include <boost/utility/addressof.hpp>
30 #include <boost/interprocess/allocators/detail/node_pool.hpp>
31 #include <boost/interprocess/containers/version_type.hpp>
32 #include <boost/container/detail/multiallocation_chain.hpp>
33 #include <boost/interprocess/exceptions.hpp>
34 #include <boost/interprocess/detail/utilities.hpp>
35 #include <boost/interprocess/detail/workaround.hpp>
36 #include <boost/move/adl_move_swap.hpp>
40 //!Describes private_node_allocator_base pooled shared memory STL compatible allocator
43 namespace interprocess {
45 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
49 template < unsigned int Version
51 , class SegmentManager
52 , std::size_t NodesPerBlock
54 class private_node_allocator_base
55 : public node_pool_allocation_impl
56 < private_node_allocator_base < Version, T, SegmentManager, NodesPerBlock>
64 typedef SegmentManager segment_manager;
65 typedef typename SegmentManager::void_pointer void_pointer;
67 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
69 typedef private_node_allocator_base
70 < Version, T, SegmentManager, NodesPerBlock> self_t;
71 typedef ipcdetail::private_node_pool
73 , sizeof_value<T>::value
77 BOOST_STATIC_ASSERT((Version <=2));
79 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
83 typedef typename boost::intrusive::
84 pointer_traits<void_pointer>::template
85 rebind_pointer<T>::type pointer;
86 typedef typename boost::intrusive::
87 pointer_traits<void_pointer>::template
88 rebind_pointer<const T>::type const_pointer;
90 typedef typename ipcdetail::add_reference
91 <value_type>::type reference;
92 typedef typename ipcdetail::add_reference
93 <const value_type>::type const_reference;
94 typedef typename segment_manager::size_type size_type;
95 typedef typename segment_manager::difference_type difference_type;
96 typedef boost::interprocess::version_type
97 <private_node_allocator_base, Version> version;
98 typedef boost::container::container_detail::transform_multiallocation_chain
99 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
101 //!Obtains node_allocator from other node_allocator
105 typedef private_node_allocator_base
106 <Version, T2, SegmentManager, NodesPerBlock> other;
109 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
113 typedef ipcdetail::private_node_pool
115 , sizeof_value<T>::value
119 static type *get(void *p)
120 { return static_cast<type*>(p); }
124 //!Not assignable from related private_node_allocator_base
125 template<unsigned int Version2, class T2, class MemoryAlgorithm2, std::size_t N2>
126 private_node_allocator_base& operator=
127 (const private_node_allocator_base<Version2, T2, MemoryAlgorithm2, N2>&);
129 //!Not assignable from other private_node_allocator_base
130 private_node_allocator_base& operator=(const private_node_allocator_base&);
131 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
134 //!Constructor from a segment manager
135 private_node_allocator_base(segment_manager *segment_mngr)
136 : m_node_pool(segment_mngr)
139 //!Copy constructor from other private_node_allocator_base. Never throws
140 private_node_allocator_base(const private_node_allocator_base &other)
141 : m_node_pool(other.get_segment_manager())
144 //!Copy constructor from related private_node_allocator_base. Never throws.
146 private_node_allocator_base
147 (const private_node_allocator_base
148 <Version, T2, SegmentManager, NodesPerBlock> &other)
149 : m_node_pool(other.get_segment_manager())
152 //!Destructor, frees all used memory. Never throws
153 ~private_node_allocator_base()
156 //!Returns the segment manager. Never throws
157 segment_manager* get_segment_manager()const
158 { return m_node_pool.get_segment_manager(); }
160 //!Returns the internal node pool. Never throws
161 node_pool_t* get_node_pool() const
162 { return const_cast<node_pool_t*>(&m_node_pool); }
164 //!Swaps allocators. Does not throw. If each allocator is placed in a
165 //!different shared memory segments, the result is undefined.
166 friend void swap(self_t &alloc1,self_t &alloc2)
167 { boost::adl_move_swap(alloc1.m_node_pool, alloc2.m_node_pool); }
169 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
171 node_pool_t m_node_pool;
172 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
175 //!Equality test for same type of private_node_allocator_base
176 template<unsigned int V, class T, class S, std::size_t NPC> inline
177 bool operator==(const private_node_allocator_base<V, T, S, NPC> &alloc1,
178 const private_node_allocator_base<V, T, S, NPC> &alloc2)
179 { return &alloc1 == &alloc2; }
181 //!Inequality test for same type of private_node_allocator_base
182 template<unsigned int V, class T, class S, std::size_t NPC> inline
183 bool operator!=(const private_node_allocator_base<V, T, S, NPC> &alloc1,
184 const private_node_allocator_base<V, T, S, NPC> &alloc2)
185 { return &alloc1 != &alloc2; }
188 , class SegmentManager
189 , std::size_t NodesPerBlock = 64
191 class private_node_allocator_v1
192 : public private_node_allocator_base
200 typedef ipcdetail::private_node_allocator_base
201 < 1, T, SegmentManager, NodesPerBlock> base_t;
206 typedef private_node_allocator_v1<T2, SegmentManager, NodesPerBlock> other;
209 private_node_allocator_v1(SegmentManager *segment_mngr)
210 : base_t(segment_mngr)
214 private_node_allocator_v1
215 (const private_node_allocator_v1<T2, SegmentManager, NodesPerBlock> &other)
220 } //namespace ipcdetail {
222 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
224 //!An STL node allocator that uses a segment manager as memory
225 //!source. The internal pointer type will of the same type (raw, smart) as
226 //!"typename SegmentManager::void_pointer" type. This allows
227 //!placing the allocator in shared memory, memory mapped-files, etc...
228 //!This allocator has its own node pool. NodesPerBlock is the number of nodes allocated
229 //!at once when the allocator needs runs out of nodes
231 , class SegmentManager
232 , std::size_t NodesPerBlock
234 class private_node_allocator
235 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
236 : public ipcdetail::private_node_allocator_base
242 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
245 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
246 typedef ipcdetail::private_node_allocator_base
247 < 2, T, SegmentManager, NodesPerBlock> base_t;
249 typedef boost::interprocess::version_type<private_node_allocator, 2> version;
254 typedef private_node_allocator
255 <T2, SegmentManager, NodesPerBlock> other;
258 private_node_allocator(SegmentManager *segment_mngr)
259 : base_t(segment_mngr)
263 private_node_allocator
264 (const private_node_allocator<T2, SegmentManager, NodesPerBlock> &other)
270 typedef implementation_defined::segment_manager segment_manager;
271 typedef segment_manager::void_pointer void_pointer;
272 typedef implementation_defined::pointer pointer;
273 typedef implementation_defined::const_pointer const_pointer;
274 typedef T value_type;
275 typedef typename ipcdetail::add_reference
276 <value_type>::type reference;
277 typedef typename ipcdetail::add_reference
278 <const value_type>::type const_reference;
279 typedef typename segment_manager::size_type size_type;
280 typedef typename segment_manage::difference_type difference_type;
282 //!Obtains private_node_allocator from
283 //!private_node_allocator
287 typedef private_node_allocator
288 <T2, SegmentManager, NodesPerBlock> other;
292 //!Not assignable from
293 //!related private_node_allocator
294 template<class T2, class SegmentManager2, std::size_t N2>
295 private_node_allocator& operator=
296 (const private_node_allocator<T2, SegmentManager2, N2>&);
298 //!Not assignable from
299 //!other private_node_allocator
300 private_node_allocator& operator=(const private_node_allocator&);
303 //!Constructor from a segment manager. If not present, constructs a node
304 //!pool. Increments the reference count of the associated node pool.
305 //!Can throw boost::interprocess::bad_alloc
306 private_node_allocator(segment_manager *segment_mngr);
308 //!Copy constructor from other private_node_allocator. Increments the reference
309 //!count of the associated node pool. Never throws
310 private_node_allocator(const private_node_allocator &other);
312 //!Copy constructor from related private_node_allocator. If not present, constructs
313 //!a node pool. Increments the reference count of the associated node pool.
314 //!Can throw boost::interprocess::bad_alloc
316 private_node_allocator
317 (const private_node_allocator<T2, SegmentManager, NodesPerBlock> &other);
319 //!Destructor, removes node_pool_t from memory
320 //!if its reference count reaches to zero. Never throws
321 ~private_node_allocator();
323 //!Returns a pointer to the node pool.
325 node_pool_t* get_node_pool() const;
327 //!Returns the segment manager.
329 segment_manager* get_segment_manager()const;
331 //!Returns the number of elements that could be allocated.
333 size_type max_size() const;
335 //!Allocate memory for an array of count elements.
336 //!Throws boost::interprocess::bad_alloc if there is no enough memory
337 pointer allocate(size_type count, cvoid_pointer hint = 0);
339 //!Deallocate allocated memory.
341 void deallocate(const pointer &ptr, size_type count);
343 //!Deallocates all free blocks
345 void deallocate_free_blocks();
347 //!Swaps allocators. Does not throw. If each allocator is placed in a
348 //!different memory segment, the result is undefined.
349 friend void swap(self_t &alloc1, self_t &alloc2);
351 //!Returns address of mutable object.
353 pointer address(reference value) const;
355 //!Returns address of non mutable object.
357 const_pointer address(const_reference value) const;
359 //!Copy construct an object.
360 //!Throws if T's copy constructor throws
361 void construct(const pointer &ptr, const_reference v);
363 //!Destroys object. Throws if object's
365 void destroy(const pointer &ptr);
367 //!Returns maximum the number of objects the previously allocated memory
368 //!pointed by p can hold. This size only works for memory allocated with
369 //!allocate, allocation_command and allocate_many.
370 size_type size(const pointer &p) const;
372 pointer allocation_command(boost::interprocess::allocation_type command,
373 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
375 //!Allocates many elements of size elem_size in a contiguous block
376 //!of memory. The minimum number to be allocated is min_elements,
377 //!the preferred and maximum number is
378 //!preferred_elements. The number of actually allocated elements is
379 //!will be assigned to received_size. The elements must be deallocated
380 //!with deallocate(...)
381 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
383 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
385 //!of memory. The elements must be deallocated
386 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
388 //!Allocates many elements of size elem_size in a contiguous block
389 //!of memory. The minimum number to be allocated is min_elements,
390 //!the preferred and maximum number is
391 //!preferred_elements. The number of actually allocated elements is
392 //!will be assigned to received_size. The elements must be deallocated
393 //!with deallocate(...)
394 void deallocate_many(multiallocation_chain &chain);
396 //!Allocates just one object. Memory allocated with this function
397 //!must be deallocated only with deallocate_one().
398 //!Throws boost::interprocess::bad_alloc if there is no enough memory
399 pointer allocate_one();
401 //!Allocates many elements of size == 1 in a contiguous block
402 //!of memory. The minimum number to be allocated is min_elements,
403 //!the preferred and maximum number is
404 //!preferred_elements. The number of actually allocated elements is
405 //!will be assigned to received_size. Memory allocated with this function
406 //!must be deallocated only with deallocate_one().
407 void allocate_individual(size_type num_elements, multiallocation_chain &chain);
409 //!Deallocates memory previously allocated with allocate_one().
410 //!You should never use deallocate_one to deallocate memory allocated
411 //!with other functions different from allocate_one(). Never throws
412 void deallocate_one(const pointer &p);
414 //!Allocates many elements of size == 1 in a contiguous block
415 //!of memory. The minimum number to be allocated is min_elements,
416 //!the preferred and maximum number is
417 //!preferred_elements. The number of actually allocated elements is
418 //!will be assigned to received_size. Memory allocated with this function
419 //!must be deallocated only with deallocate_one().
420 void deallocate_individual(multiallocation_chain &chain);
424 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
426 //!Equality test for same type
427 //!of private_node_allocator
428 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
429 bool operator==(const private_node_allocator<T, S, NodesPerBlock, F, OP> &alloc1,
430 const private_node_allocator<T, S, NodesPerBlock, F, OP> &alloc2);
432 //!Inequality test for same type
433 //!of private_node_allocator
434 template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
435 bool operator!=(const private_node_allocator<T, S, NodesPerBlock, F, OP> &alloc1,
436 const private_node_allocator<T, S, NodesPerBlock, F, OP> &alloc2);
440 } //namespace interprocess {
441 } //namespace boost {
443 #include <boost/interprocess/detail/config_end.hpp>
445 #endif //#ifndef BOOST_INTERPROCESS_PRIVATE_NODE_ALLOCATOR_HPP