adaptive_pool.hpp 17 KB

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