cached_adaptive_pool.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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_CACHED_ADAPTIVE_POOL_HPP
  11. #define BOOST_INTERPROCESS_CACHED_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/interprocess/interprocess_fwd.hpp>
  18. #include <boost/interprocess/allocators/detail/adaptive_node_pool.hpp>
  19. #include <boost/interprocess/allocators/detail/allocator_common.hpp>
  20. #include <boost/interprocess/detail/utilities.hpp>
  21. #include <boost/interprocess/detail/workaround.hpp>
  22. #include <boost/interprocess/containers/version_type.hpp>
  23. #include <boost/interprocess/allocators/detail/node_tools.hpp>
  24. #include <cstddef>
  25. //!\file
  26. //!Describes cached_adaptive_pool pooled shared memory STL compatible allocator
  27. namespace boost {
  28. namespace interprocess {
  29. /// @cond
  30. namespace ipcdetail {
  31. template < class T
  32. , class SegmentManager
  33. , std::size_t NodesPerBlock = 64
  34. , std::size_t MaxFreeBlocks = 2
  35. , unsigned char OverheadPercent = 5
  36. >
  37. class cached_adaptive_pool_v1
  38. : public ipcdetail::cached_allocator_impl
  39. < T
  40. , ipcdetail::shared_adaptive_node_pool
  41. < SegmentManager
  42. , sizeof_value<T>::value
  43. , NodesPerBlock
  44. , MaxFreeBlocks
  45. , OverheadPercent
  46. >
  47. , 1>
  48. {
  49. public:
  50. typedef ipcdetail::cached_allocator_impl
  51. < T
  52. , ipcdetail::shared_adaptive_node_pool
  53. < SegmentManager
  54. , sizeof_value<T>::value
  55. , NodesPerBlock
  56. , MaxFreeBlocks
  57. , OverheadPercent
  58. >
  59. , 1> base_t;
  60. template<class T2>
  61. struct rebind
  62. {
  63. typedef cached_adaptive_pool_v1
  64. <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  65. };
  66. typedef typename base_t::size_type size_type;
  67. cached_adaptive_pool_v1(SegmentManager *segment_mngr,
  68. size_type max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
  69. : base_t(segment_mngr, max_cached_nodes)
  70. {}
  71. template<class T2>
  72. cached_adaptive_pool_v1
  73. (const cached_adaptive_pool_v1
  74. <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
  75. : base_t(other)
  76. {}
  77. };
  78. } //namespace ipcdetail{
  79. /// @endcond
  80. //!An STL node allocator that uses a segment manager as memory
  81. //!source. The internal pointer type will of the same type (raw, smart) as
  82. //!"typename SegmentManager::void_pointer" type. This allows
  83. //!placing the allocator in shared memory, memory mapped-files, etc...
  84. //!
  85. //!This node allocator shares a segregated storage between all instances of
  86. //!cached_adaptive_pool with equal sizeof(T) placed in the same
  87. //!memory segment. But also caches some nodes privately to
  88. //!avoid some synchronization overhead.
  89. //!
  90. //!NodesPerBlock is the minimum number of nodes of nodes allocated at once when
  91. //!the allocator needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
  92. //!that the adaptive node pool will hold. The rest of the totally free blocks will be
  93. //!deallocated with the segment manager.
  94. //!
  95. //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
  96. //!(memory usable for nodes / total memory allocated from the segment manager)
  97. template < class T
  98. , class SegmentManager
  99. , std::size_t NodesPerBlock
  100. , std::size_t MaxFreeBlocks
  101. , unsigned char OverheadPercent
  102. >
  103. class cached_adaptive_pool
  104. /// @cond
  105. : public ipcdetail::cached_allocator_impl
  106. < T
  107. , ipcdetail::shared_adaptive_node_pool
  108. < SegmentManager
  109. , sizeof_value<T>::value
  110. , NodesPerBlock
  111. , MaxFreeBlocks
  112. , OverheadPercent
  113. >
  114. , 2>
  115. /// @endcond
  116. {
  117. #ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  118. public:
  119. typedef ipcdetail::cached_allocator_impl
  120. < T
  121. , ipcdetail::shared_adaptive_node_pool
  122. < SegmentManager
  123. , sizeof_value<T>::value
  124. , NodesPerBlock
  125. , MaxFreeBlocks
  126. , OverheadPercent
  127. >
  128. , 2> base_t;
  129. public:
  130. typedef boost::interprocess::version_type<cached_adaptive_pool, 2> version;
  131. template<class T2>
  132. struct rebind
  133. {
  134. typedef cached_adaptive_pool
  135. <T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  136. };
  137. cached_adaptive_pool(SegmentManager *segment_mngr,
  138. std::size_t max_cached_nodes = base_t::DEFAULT_MAX_CACHED_NODES)
  139. : base_t(segment_mngr, max_cached_nodes)
  140. {}
  141. template<class T2>
  142. cached_adaptive_pool
  143. (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other)
  144. : base_t(other)
  145. {}
  146. #else
  147. public:
  148. typedef implementation_defined::segment_manager segment_manager;
  149. typedef segment_manager::void_pointer void_pointer;
  150. typedef implementation_defined::pointer pointer;
  151. typedef implementation_defined::const_pointer const_pointer;
  152. typedef T value_type;
  153. typedef typename ipcdetail::add_reference
  154. <value_type>::type reference;
  155. typedef typename ipcdetail::add_reference
  156. <const value_type>::type const_reference;
  157. typedef typename segment_manager::size_type size_type;
  158. typedef typename segment_manager::difference_type difference_type;
  159. //!Obtains cached_adaptive_pool from
  160. //!cached_adaptive_pool
  161. template<class T2>
  162. struct rebind
  163. {
  164. typedef cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> other;
  165. };
  166. private:
  167. //!Not assignable from
  168. //!related cached_adaptive_pool
  169. template<class T2, class SegmentManager2, std::size_t N2, std::size_t F2, unsigned char OP2>
  170. cached_adaptive_pool& operator=
  171. (const cached_adaptive_pool<T2, SegmentManager2, N2, F2, OP2>&);
  172. //!Not assignable from
  173. //!other cached_adaptive_pool
  174. cached_adaptive_pool& operator=(const cached_adaptive_pool&);
  175. public:
  176. //!Constructor from a segment manager. If not present, constructs a node
  177. //!pool. Increments the reference count of the associated node pool.
  178. //!Can throw boost::interprocess::bad_alloc
  179. cached_adaptive_pool(segment_manager *segment_mngr);
  180. //!Copy constructor from other cached_adaptive_pool. Increments the reference
  181. //!count of the associated node pool. Never throws
  182. cached_adaptive_pool(const cached_adaptive_pool &other);
  183. //!Copy constructor from related cached_adaptive_pool. If not present, constructs
  184. //!a node pool. Increments the reference count of the associated node pool.
  185. //!Can throw boost::interprocess::bad_alloc
  186. template<class T2>
  187. cached_adaptive_pool
  188. (const cached_adaptive_pool<T2, SegmentManager, NodesPerBlock, MaxFreeBlocks, OverheadPercent> &other);
  189. //!Destructor, removes node_pool_t from memory
  190. //!if its reference count reaches to zero. Never throws
  191. ~cached_adaptive_pool();
  192. //!Returns a pointer to the node pool.
  193. //!Never throws
  194. node_pool_t* get_node_pool() const;
  195. //!Returns the segment manager.
  196. //!Never throws
  197. segment_manager* get_segment_manager()const;
  198. //!Returns the number of elements that could be allocated.
  199. //!Never throws
  200. size_type max_size() const;
  201. //!Allocate memory for an array of count elements.
  202. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  203. pointer allocate(size_type count, cvoid_pointer hint = 0);
  204. //!Deallocate allocated memory.
  205. //!Never throws
  206. void deallocate(const pointer &ptr, size_type count);
  207. //!Deallocates all free blocks
  208. //!of the pool
  209. void deallocate_free_blocks();
  210. //!Swaps allocators. Does not throw. If each allocator is placed in a
  211. //!different memory segment, the result is undefined.
  212. friend void swap(self_t &alloc1, self_t &alloc2);
  213. //!Returns address of mutable object.
  214. //!Never throws
  215. pointer address(reference value) const;
  216. //!Returns address of non mutable object.
  217. //!Never throws
  218. const_pointer address(const_reference value) const;
  219. //!Copy construct an object.
  220. //!Throws if T's copy constructor throws
  221. void construct(const pointer &ptr, const_reference v);
  222. //!Destroys object. Throws if object's
  223. //!destructor throws
  224. void destroy(const pointer &ptr);
  225. //!Returns maximum the number of objects the previously allocated memory
  226. //!pointed by p can hold. This size only works for memory allocated with
  227. //!allocate, allocation_command and allocate_many.
  228. size_type size(const pointer &p) const;
  229. std::pair<pointer, bool>
  230. allocation_command(boost::interprocess::allocation_type command,
  231. size_type limit_size,
  232. size_type preferred_size,
  233. size_type &received_size, const pointer &reuse = 0);
  234. //!Allocates many elements of size elem_size in a contiguous block
  235. //!of memory. The minimum number to be allocated is min_elements,
  236. //!the preferred and maximum number is
  237. //!preferred_elements. The number of actually allocated elements is
  238. //!will be assigned to received_size. The elements must be deallocated
  239. //!with deallocate(...)
  240. void allocate_many(size_type elem_size, size_type num_elements, multiallocation_chain &chain);
  241. //!Allocates n_elements elements, each one of size elem_sizes[i]in a
  242. //!contiguous block
  243. //!of memory. The elements must be deallocated
  244. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain);
  245. //!Allocates many elements of size elem_size in a contiguous block
  246. //!of memory. The minimum number to be allocated is min_elements,
  247. //!the preferred and maximum number is
  248. //!preferred_elements. The number of actually allocated elements is
  249. //!will be assigned to received_size. The elements must be deallocated
  250. //!with deallocate(...)
  251. void deallocate_many(multiallocation_chain &chain);
  252. //!Allocates just one object. Memory allocated with this function
  253. //!must be deallocated only with deallocate_one().
  254. //!Throws boost::interprocess::bad_alloc if there is no enough memory
  255. pointer allocate_one();
  256. //!Allocates many elements of size == 1 in a contiguous block
  257. //!of memory. The minimum number to be allocated is min_elements,
  258. //!the preferred and maximum number is
  259. //!preferred_elements. The number of actually allocated elements is
  260. //!will be assigned to received_size. Memory allocated with this function
  261. //!must be deallocated only with deallocate_one().
  262. multiallocation_chain allocate_individual(size_type num_elements);
  263. //!Deallocates memory previously allocated with allocate_one().
  264. //!You should never use deallocate_one to deallocate memory allocated
  265. //!with other functions different from allocate_one(). Never throws
  266. void deallocate_one(const pointer &p);
  267. //!Allocates many elements of size == 1 in a contiguous block
  268. //!of memory. The minimum number to be allocated is min_elements,
  269. //!the preferred and maximum number is
  270. //!preferred_elements. The number of actually allocated elements is
  271. //!will be assigned to received_size. Memory allocated with this function
  272. //!must be deallocated only with deallocate_one().
  273. void deallocate_individual(multiallocation_chain &chain);
  274. //!Sets the new max cached nodes value. This can provoke deallocations
  275. //!if "newmax" is less than current cached nodes. Never throws
  276. void set_max_cached_nodes(size_type newmax);
  277. //!Returns the max cached nodes parameter.
  278. //!Never throws
  279. size_type get_max_cached_nodes() const;
  280. #endif
  281. };
  282. #ifdef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  283. //!Equality test for same type
  284. //!of cached_adaptive_pool
  285. template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
  286. bool operator==(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
  287. const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
  288. //!Inequality test for same type
  289. //!of cached_adaptive_pool
  290. template<class T, class S, std::size_t NodesPerBlock, std::size_t F, std::size_t OP> inline
  291. bool operator!=(const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc1,
  292. const cached_adaptive_pool<T, S, NodesPerBlock, F, OP> &alloc2);
  293. #endif
  294. } //namespace interprocess {
  295. } //namespace boost {
  296. #include <boost/interprocess/detail/config_end.hpp>
  297. #endif //#ifndef BOOST_INTERPROCESS_CACHED_ADAPTIVE_POOL_HPP