private_node_allocator.hpp 16 KB

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