private_adaptive_pool.hpp 17 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_PRIVATE_ADAPTIVE_POOL_HPP
  11. #define BOOST_INTERPROCESS_PRIVATE_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/allocators/detail/adaptive_node_pool.hpp>
  27. #include <boost/interprocess/containers/version_type.hpp>
  28. #include <boost/container/detail/multiallocation_chain.hpp>
  29. #include <boost/interprocess/exceptions.hpp>
  30. #include <boost/interprocess/detail/utilities.hpp>
  31. #include <boost/interprocess/detail/workaround.hpp>
  32. #include <boost/move/adl_move_swap.hpp>
  33. #include <cstddef>
  34. //!\file
  35. //!Describes private_adaptive_pool_base pooled shared memory STL compatible allocator
  36. namespace boost {
  37. namespace interprocess {
  38. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  39. namespace ipcdetail {
  40. template < unsigned int Version
  41. , class T
  42. , class SegmentManager
  43. , std::size_t NodesPerBlock
  44. , std::size_t MaxFreeBlocks
  45. , unsigned char OverheadPercent
  46. >
  47. class private_adaptive_pool_base
  48. : public node_pool_allocation_impl
  49. < private_adaptive_pool_base < Version, T, SegmentManager, NodesPerBlock
  50. , MaxFreeBlocks, OverheadPercent>
  51. , Version
  52. , T
  53. , SegmentManager
  54. >
  55. {
  56. public:
  57. //Segment manager
  58. typedef SegmentManager segment_manager;
  59. typedef typename SegmentManager::void_pointer void_pointer;
  60. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  61. private:
  62. typedef private_adaptive_pool_base
  63. < Version, T, SegmentManager, NodesPerBlock
  64. , MaxFreeBlocks, OverheadPercent> self_t;
  65. typedef ipcdetail::private_adaptive_node_pool
  66. <SegmentManager
  67. , sizeof_value<T>::value
  68. , NodesPerBlock
  69. , MaxFreeBlocks
  70. , OverheadPercent
  71. > node_pool_t;
  72. BOOST_STATIC_ASSERT((Version <=2));
  73. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  74. public:
  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
  89. <private_adaptive_pool_base, Version> version;
  90. typedef boost::container::dtl::transform_multiallocation_chain
  91. <typename SegmentManager::multiallocation_chain, T>multiallocation_chain;
  92. //!Obtains node_allocator from other node_allocator
  93. template<class T2>
  94. struct rebind
  95. {
  96. typedef private_adaptive_pool_base
  97. <Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  98. };
  99. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  100. template <int dummy>
  101. struct node_pool
  102. {
  103. typedef ipcdetail::private_adaptive_node_pool
  104. <SegmentManager
  105. , sizeof_value<T>::value
  106. , NodesPerBlock
  107. , MaxFreeBlocks
  108. , OverheadPercent
  109. > type;
  110. static type *get(void *p)
  111. { return static_cast<type*>(p); }
  112. };
  113. private:
  114. //!Not assignable from related private_adaptive_pool_base
  115. template<unsigned int Version2, class T2, class MemoryAlgorithm2, std::size_t N2, std::size_t F2, unsigned char OP2>
  116. private_adaptive_pool_base& operator=
  117. (const private_adaptive_pool_base<Version2, T2, MemoryAlgorithm2, N2, F2, OP2>&);
  118. //!Not assignable from other private_adaptive_pool_base
  119. private_adaptive_pool_base& operator=(const private_adaptive_pool_base&);
  120. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  121. public:
  122. //!Constructor from a segment manager
  123. private_adaptive_pool_base(segment_manager *segment_mngr)
  124. : m_node_pool(segment_mngr)
  125. {}
  126. //!Copy constructor from other private_adaptive_pool_base. Never throws
  127. private_adaptive_pool_base(const private_adaptive_pool_base &other)
  128. : m_node_pool(other.get_segment_manager())
  129. {}
  130. //!Copy constructor from related private_adaptive_pool_base. Never throws.
  131. template<class T2>
  132. private_adaptive_pool_base
  133. (const private_adaptive_pool_base
  134. <Version, T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
  135. : m_node_pool(other.get_segment_manager())
  136. {}
  137. //!Destructor, frees all used memory. Never throws
  138. ~private_adaptive_pool_base()
  139. {}
  140. //!Returns the segment manager. Never throws
  141. segment_manager* get_segment_manager()const
  142. { return m_node_pool.get_segment_manager(); }
  143. //!Returns the internal node pool. Never throws
  144. node_pool_t* get_node_pool() const
  145. { return const_cast<node_pool_t*>(&m_node_pool); }
  146. //!Swaps allocators. Does not throw. If each allocator is placed in a
  147. //!different shared memory segments, the result is undefined.
  148. friend void swap(self_t &alloc1,self_t &alloc2)
  149. { boost::adl_move_swap(alloc1.m_node_pool, alloc2.m_node_pool); }
  150. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  151. private:
  152. node_pool_t m_node_pool;
  153. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  154. };
  155. //!Equality test for same type of private_adaptive_pool_base
  156. template<unsigned int V, class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
  157. bool operator==(const private_adaptive_pool_base<V, T, S, NodesPerBlock, F, OP> &alloc1,
  158. const private_adaptive_pool_base<V, T, S, NodesPerBlock, F, OP> &alloc2)
  159. { return &alloc1 == &alloc2; }
  160. //!Inequality test for same type of private_adaptive_pool_base
  161. template<unsigned int V, class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
  162. bool operator!=(const private_adaptive_pool_base<V, T, S, NodesPerBlock, F, OP> &alloc1,
  163. const private_adaptive_pool_base<V, T, S, NodesPerBlock, F, OP> &alloc2)
  164. { return &alloc1 != &alloc2; }
  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 private_adaptive_pool_v1
  172. : public private_adaptive_pool_base
  173. < 1
  174. , T
  175. , SegmentManager
  176. , NodesPerBlock
  177. , MaxFreeBlocks
  178. , OverheadPercent
  179. >
  180. {
  181. public:
  182. typedef ipcdetail::private_adaptive_pool_base
  183. < 1, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
  184. template<class T2>
  185. struct rebind
  186. {
  187. typedef private_adaptive_pool_v1<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  188. };
  189. private_adaptive_pool_v1(SegmentManager *segment_mngr)
  190. : base_t(segment_mngr)
  191. {}
  192. template<class T2>
  193. private_adaptive_pool_v1
  194. (const private_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. //!This allocator has its own node pool.
  205. //!
  206. //!NodesPerBlock is the minimum number of nodes of nodes allocated at once when
  207. //!the allocator needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
  208. //!that the adaptive node pool will hold. The rest of the totally free blocks will be
  209. //!deallocated with the segment manager.
  210. //!
  211. //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
  212. //!(memory usable for nodes / total memory allocated from the segment manager)
  213. template < class T
  214. , class SegmentManager
  215. , std::size_t NodesPerBlock
  216. , std::size_t MaxFreeBlocks
  217. , unsigned char OverheadPercent
  218. >
  219. class private_adaptive_pool
  220. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  221. : public ipcdetail::private_adaptive_pool_base
  222. < 2
  223. , T
  224. , SegmentManager
  225. , NodesPerBlock
  226. , MaxFreeBlocks
  227. , OverheadPercent
  228. >
  229. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  230. {
  231. #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  232. typedef ipcdetail::private_adaptive_pool_base
  233. < 2, T, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> base_t;
  234. public:
  235. typedef boost::interprocess::version_type<private_adaptive_pool, 2> version;
  236. template<class T2>
  237. struct rebind
  238. {
  239. typedef private_adaptive_pool
  240. <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  241. };
  242. private_adaptive_pool(SegmentManager *segment_mngr)
  243. : base_t(segment_mngr)
  244. {}
  245. template<class T2>
  246. private_adaptive_pool
  247. (const private_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
  248. : base_t(other)
  249. {}
  250. #else
  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 private_adaptive_pool from
  264. //!private_adaptive_pool
  265. template<class T2>
  266. struct rebind
  267. {
  268. typedef private_adaptive_pool
  269. <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  270. };
  271. private:
  272. //!Not assignable from
  273. //!related private_adaptive_pool
  274. template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
  275. private_adaptive_pool& operator=
  276. (const private_adaptive_pool<T2, SegmentManager2, N2, F2>&);
  277. //!Not assignable from
  278. //!other private_adaptive_pool
  279. private_adaptive_pool& operator=(const private_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. private_adaptive_pool(segment_manager *segment_mngr);
  285. //!Copy constructor from other private_adaptive_pool. Increments the reference
  286. //!count of the associated node pool. Never throws
  287. private_adaptive_pool(const private_adaptive_pool &other);
  288. //!Copy constructor from related private_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. private_adaptive_pool
  293. (const private_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. ~private_adaptive_pool();
  297. //!Returns a pointer to the node pool.
  298. //!Never throws
  299. node_pool_t* 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. //!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. //!Returns maximum the number of objects the previously allocated memory
  331. //!pointed by p can hold. This size only works for memory allocated with
  332. //!allocate, allocation_command and allocate_many.
  333. size_type size(const pointer &p) const;
  334. pointer allocation_command(boost::interprocess::allocation_type command,
  335. size_type limit_size, size_type &prefer_in_recvd_out_size, pointer &reuse);
  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 private_adaptive_pool
  381. template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
  382. bool operator==(const private_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
  383. const private_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
  384. //!Inequality test for same type
  385. //!of private_adaptive_pool
  386. template<class T, class S, std::size_t NodesPerBlock, std::size_t F, unsigned char OP> inline
  387. bool operator!=(const private_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
  388. const private_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_PRIVATE_ADAPTIVE_POOL_HPP