/Src/Dependencies/Boost/boost/interprocess/allocators/cached_adaptive_pool.hpp

http://hadesmem.googlecode.com/ · C++ Header · 354 lines · 182 code · 58 blank · 114 comment · 1 complexity · d4816ac15fa09dbbe0716af76003083c MD5 · raw file

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