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