adaptive_pool.hpp 18 KB

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