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