node_allocator.hpp 16 KB

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