adaptive_pool.hpp 18 KB

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