node_allocator.hpp 16 KB

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