private_node_allocator.hpp 16 KB

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