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_NODE_ALLOCATOR_HPP
12 #define BOOST_INTERPROCESS_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/detail/utilities.hpp>
31 #include <boost/interprocess/detail/type_traits.hpp>
32 #include <boost/interprocess/allocators/detail/node_pool.hpp>
33 #include <boost/interprocess/containers/version_type.hpp>
34 #include <boost/container/detail/multiallocation_chain.hpp>
35 #include <boost/interprocess/exceptions.hpp>
36 #include <boost/interprocess/allocators/detail/allocator_common.hpp>
37 #include <boost/move/adl_move_swap.hpp>
41 //!Describes node_allocator pooled shared memory STL compatible allocator
44 namespace interprocess {
46 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
50 template < unsigned int Version
52 , class SegmentManager
53 , std::size_t NodesPerBlock
55 class node_allocator_base
56 : public node_pool_allocation_impl
58 < Version, T, SegmentManager, NodesPerBlock>
65 typedef typename SegmentManager::void_pointer void_pointer;
66 typedef SegmentManager segment_manager;
67 typedef node_allocator_base
68 <Version, T, SegmentManager, NodesPerBlock> self_t;
70 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
75 typedef ipcdetail::shared_node_pool
76 < SegmentManager, sizeof_value<T>::value, NodesPerBlock> type;
78 static type *get(void *p)
79 { return static_cast<type*>(p); }
81 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
83 BOOST_STATIC_ASSERT((Version <=2));
87 typedef typename boost::intrusive::
88 pointer_traits<void_pointer>::template
89 rebind_pointer<T>::type pointer;
90 typedef typename boost::intrusive::
91 pointer_traits<void_pointer>::template
92 rebind_pointer<const T>::type const_pointer;
94 typedef typename ipcdetail::add_reference
95 <value_type>::type reference;
96 typedef typename ipcdetail::add_reference
97 <const value_type>::type const_reference;
98 typedef typename segment_manager::size_type size_type;
99 typedef typename segment_manager::difference_type difference_type;
101 typedef boost::interprocess::version_type<node_allocator_base, Version> version;
102 typedef boost::container::container_detail::transform_multiallocation_chain
103 <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
105 //!Obtains node_allocator_base from
106 //!node_allocator_base
110 typedef node_allocator_base<Version, T2, SegmentManager, NodesPerBlock> other;
113 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
115 //!Not assignable from related node_allocator_base
116 template<unsigned int Version2, class T2, class SegmentManager2, std::size_t N2>
117 node_allocator_base& operator=
118 (const node_allocator_base<Version2, T2, SegmentManager2, N2>&);
120 //!Not assignable from other node_allocator_base
121 //node_allocator_base& operator=(const node_allocator_base&);
122 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
125 //!Constructor from a segment manager. If not present, constructs a node
126 //!pool. Increments the reference count of the associated node pool.
127 //!Can throw boost::interprocess::bad_alloc
128 node_allocator_base(segment_manager *segment_mngr)
129 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(segment_mngr)) { }
131 //!Copy constructor from other node_allocator_base. Increments the reference
132 //!count of the associated node pool. Never throws
133 node_allocator_base(const node_allocator_base &other)
134 : mp_node_pool(other.get_node_pool())
136 node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->inc_ref_count();
139 //!Copy constructor from related node_allocator_base. If not present, constructs
140 //!a node pool. Increments the reference count of the associated node pool.
141 //!Can throw boost::interprocess::bad_alloc
144 (const node_allocator_base<Version, T2, SegmentManager, NodesPerBlock> &other)
145 : mp_node_pool(ipcdetail::get_or_create_node_pool<typename node_pool<0>::type>(other.get_segment_manager())) { }
147 //!Assignment from other node_allocator_base
148 node_allocator_base& operator=(const node_allocator_base &other)
150 node_allocator_base c(other);
151 boost::adl_move_swap(*this, c);
155 //!Destructor, removes node_pool_t from memory
156 //!if its reference count reaches to zero. Never throws
157 ~node_allocator_base()
158 { ipcdetail::destroy_node_pool_if_last_link(node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))); }
160 //!Returns a pointer to the node pool.
162 void* get_node_pool() const
163 { return ipcdetail::to_raw_pointer(mp_node_pool); }
165 //!Returns the segment manager.
167 segment_manager* get_segment_manager()const
168 { return node_pool<0>::get(ipcdetail::to_raw_pointer(mp_node_pool))->get_segment_manager(); }
170 //!Swaps allocators. Does not throw. If each allocator is placed in a
171 //!different memory segment, the result is undefined.
172 friend void swap(self_t &alloc1, self_t &alloc2)
173 { boost::adl_move_swap(alloc1.mp_node_pool, alloc2.mp_node_pool); }
175 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
177 void_pointer mp_node_pool;
178 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
181 //!Equality test for same type
182 //!of node_allocator_base
183 template<unsigned int V, class T, class S, std::size_t NPC> inline
184 bool operator==(const node_allocator_base<V, T, S, NPC> &alloc1,
185 const node_allocator_base<V, T, S, NPC> &alloc2)
186 { return alloc1.get_node_pool() == alloc2.get_node_pool(); }
188 //!Inequality test for same type
189 //!of node_allocator_base
190 template<unsigned int V, class T, class S, std::size_t NPC> inline
191 bool operator!=(const node_allocator_base<V, T, S, NPC> &alloc1,
192 const node_allocator_base<V, T, S, NPC> &alloc2)
193 { return alloc1.get_node_pool() != alloc2.get_node_pool(); }
196 , class SegmentManager
197 , std::size_t NodesPerBlock = 64
199 class node_allocator_v1
200 : public node_allocator_base
208 typedef ipcdetail::node_allocator_base
209 < 1, T, SegmentManager, NodesPerBlock> base_t;
214 typedef node_allocator_v1<T2, SegmentManager, NodesPerBlock> other;
217 node_allocator_v1(SegmentManager *segment_mngr)
218 : base_t(segment_mngr)
223 (const node_allocator_v1<T2, SegmentManager, NodesPerBlock> &other)
228 } //namespace ipcdetail{
230 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
232 //!An STL node allocator that uses a segment manager as memory
233 //!source. The internal pointer type will of the same type (raw, smart) as
234 //!"typename SegmentManager::void_pointer" type. This allows
235 //!placing the allocator in shared memory, memory mapped-files, etc...
236 //!This node allocator shares a segregated storage between all instances
237 //!of node_allocator with equal sizeof(T) placed in the same segment
238 //!group. NodesPerBlock is the number of nodes allocated at once when the allocator
241 , class SegmentManager
242 , std::size_t NodesPerBlock
245 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
246 : public ipcdetail::node_allocator_base
252 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
255 #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
256 typedef ipcdetail::node_allocator_base
257 < 2, T, SegmentManager, NodesPerBlock> base_t;
259 typedef boost::interprocess::version_type<node_allocator, 2> version;
264 typedef node_allocator<T2, SegmentManager, NodesPerBlock> other;
267 node_allocator(SegmentManager *segment_mngr)
268 : base_t(segment_mngr)
273 (const node_allocator<T2, SegmentManager, NodesPerBlock> &other)
277 #else //BOOST_INTERPROCESS_DOXYGEN_INVOKED
279 typedef implementation_defined::segment_manager segment_manager;
280 typedef segment_manager::void_pointer void_pointer;
281 typedef implementation_defined::pointer pointer;
282 typedef implementation_defined::const_pointer const_pointer;
283 typedef T value_type;
284 typedef typename ipcdetail::add_reference
285 <value_type>::type reference;
286 typedef typename ipcdetail::add_reference
287 <const value_type>::type const_reference;
288 typedef typename segment_manager::size_type size_type;
289 typedef typename segment_manager::difference_type difference_type;
291 //!Obtains node_allocator from
296 typedef node_allocator<T2, SegmentManager, NodesPerBlock> other;
300 //!Not assignable from
301 //!related node_allocator
302 template<class T2, class SegmentManager2, std::size_t N2>
303 node_allocator& operator=
304 (const node_allocator<T2, SegmentManager2, N2>&);
306 //!Not assignable from
307 //!other node_allocator
308 //node_allocator& operator=(const node_allocator&);
311 //!Constructor from a segment manager. If not present, constructs a node
312 //!pool. Increments the reference count of the associated node pool.
313 //!Can throw boost::interprocess::bad_alloc
314 node_allocator(segment_manager *segment_mngr);
316 //!Copy constructor from other node_allocator. Increments the reference
317 //!count of the associated node pool. Never throws
318 node_allocator(const node_allocator &other);
320 //!Copy constructor from related node_allocator. If not present, constructs
321 //!a node pool. Increments the reference count of the associated node pool.
322 //!Can throw boost::interprocess::bad_alloc
325 (const node_allocator<T2, SegmentManager, NodesPerBlock> &other);
327 //!Destructor, removes node_pool_t from memory
328 //!if its reference count reaches to zero. Never throws
331 //!Returns a pointer to the node pool.
333 void* get_node_pool() const;
335 //!Returns the segment manager.
337 segment_manager* get_segment_manager()const;
339 //!Returns the number of elements that could be allocated.
341 size_type max_size() const;
343 //!Allocate memory for an array of count elements.
344 //!Throws boost::interprocess::bad_alloc if there is no enough memory
345 pointer allocate(size_type count, cvoid_pointer hint = 0);
347 //!Deallocate allocated memory.
349 void deallocate(const pointer &ptr, size_type count);
351 //!Deallocates all free blocks
353 void deallocate_free_blocks();
355 //!Swaps allocators. Does not throw. If each allocator is placed in a
356 //!different memory segment, the result is undefined.
357 friend void swap(self_t &alloc1, self_t &alloc2);
359 //!Returns address of mutable object.
361 pointer address(reference value) const;
363 //!Returns address of non mutable object.
365 const_pointer address(const_reference value) const;
367 //!Copy construct an object.
368 //!Throws if T's copy constructor throws
369 void construct(const pointer &ptr, const_reference v);
371 //!Destroys object. Throws if object's
373 void destroy(const pointer &ptr);
375 //!Returns maximum the number of objects the previously allocated memory
376 //!pointed by p can hold. This size only works for memory allocated with
377 //!allocate, allocation_command and allocate_many.
378 size_type size(const pointer &p) const;
380 pointer allocation_command(boost::interprocess::allocation_type command,
381 size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
383 //!Allocates many elements of size elem_size in a contiguous block
384 //!of memory. The minimum number to be allocated is min_elements,
385 //!the preferred and maximum number is
386 //!preferred_elements. The number of actually allocated elements is
387 //!will be assigned to received_size. The elements must be deallocated
388 //!with deallocate(...)
389 void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
391 //!Allocates n_elements elements, each one of size elem_sizes[i]in a
393 //!of memory. The elements must be deallocated
394 void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
396 //!Allocates many elements of size elem_size in a contiguous block
397 //!of memory. The minimum number to be allocated is min_elements,
398 //!the preferred and maximum number is
399 //!preferred_elements. The number of actually allocated elements is
400 //!will be assigned to received_size. The elements must be deallocated
401 //!with deallocate(...)
402 void deallocate_many(multiallocation_chain &chain);
404 //!Allocates just one object. Memory allocated with this function
405 //!must be deallocated only with deallocate_one().
406 //!Throws boost::interprocess::bad_alloc if there is no enough memory
407 pointer allocate_one();
409 //!Allocates many elements of size == 1 in a contiguous block
410 //!of memory. The minimum number to be allocated is min_elements,
411 //!the preferred and maximum number is
412 //!preferred_elements. The number of actually allocated elements is
413 //!will be assigned to received_size. Memory allocated with this function
414 //!must be deallocated only with deallocate_one().
415 void allocate_individual(size_type num_elements, multiallocation_chain &chain);
417 //!Deallocates memory previously allocated with allocate_one().
418 //!You should never use deallocate_one to deallocate memory allocated
419 //!with other functions different from allocate_one(). Never throws
420 void deallocate_one(const pointer &p);
422 //!Allocates many elements of size == 1 in a contiguous block
423 //!of memory. The minimum number to be allocated is min_elements,
424 //!the preferred and maximum number is
425 //!preferred_elements. The number of actually allocated elements is
426 //!will be assigned to received_size. Memory allocated with this function
427 //!must be deallocated only with deallocate_one().
428 void deallocate_individual(multiallocation_chain &chain);
432 #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
434 //!Equality test for same type
436 template<class T, class S, std::size_t NPC> inline
437 bool operator==(const node_allocator<T, S, NPC> &alloc1,
438 const node_allocator<T, S, NPC> &alloc2);
440 //!Inequality test for same type
442 template<class T, class S, std::size_t NPC> inline
443 bool operator!=(const node_allocator<T, S, NPC> &alloc1,
444 const node_allocator<T, S, NPC> &alloc2);
448 } //namespace interprocess {
449 } //namespace boost {
451 #include <boost/interprocess/detail/config_end.hpp>
453 #endif //#ifndef BOOST_INTERPROCESS_NODE_ALLOCATOR_HPP