node_allocator.hpp 16 KB

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