adaptive_pool.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2013. 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/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP
  11. #define BOOST_CONTAINER_ADAPTIVE_POOL_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #if defined(BOOST_HAS_PRAGMA_ONCE)
  16. # pragma once
  17. #endif
  18. #include <boost/container/detail/config_begin.hpp>
  19. #include <boost/container/detail/workaround.hpp>
  20. #include <boost/container/container_fwd.hpp>
  21. #include <boost/container/detail/version_type.hpp>
  22. #include <boost/container/throw_exception.hpp>
  23. #include <boost/container/detail/adaptive_node_pool.hpp>
  24. #include <boost/container/detail/multiallocation_chain.hpp>
  25. #include <boost/container/detail/mpl.hpp>
  26. #include <boost/container/detail/dlmalloc.hpp>
  27. #include <boost/container/detail/singleton.hpp>
  28. #include <boost/container/detail/placement_new.hpp>
  29. #include <boost/assert.hpp>
  30. #include <boost/static_assert.hpp>
  31. #include <boost/move/utility_core.hpp>
  32. #include <cstddef>
  33. namespace boost {
  34. namespace container {
  35. //!An STL node allocator that uses a modified DLMalloc as memory
  36. //!source.
  37. //!
  38. //!This node allocator shares a segregated storage between all instances
  39. //!of adaptive_pool with equal sizeof(T).
  40. //!
  41. //!NodesPerBlock is the number of nodes allocated at once when the allocator
  42. //!needs runs out of nodes. MaxFreeBlocks is the maximum number of totally free blocks
  43. //!that the adaptive node pool will hold. The rest of the totally free blocks will be
  44. //!deallocated to the memory manager.
  45. //!
  46. //!OverheadPercent is the (approximated) maximum size overhead (1-20%) of the allocator:
  47. //!(memory usable for nodes / total memory allocated from the memory allocator)
  48. template < class T
  49. , std::size_t NodesPerBlock BOOST_CONTAINER_DOCONLY(= ADP_nodes_per_block)
  50. , std::size_t MaxFreeBlocks BOOST_CONTAINER_DOCONLY(= ADP_max_free_blocks)
  51. , std::size_t OverheadPercent BOOST_CONTAINER_DOCONLY(= ADP_overhead_percent)
  52. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I unsigned Version)
  53. >
  54. class adaptive_pool
  55. {
  56. //!If Version is 1, the allocator is a STL conforming allocator. If Version is 2,
  57. //!the allocator offers advanced expand in place and burst allocation capabilities.
  58. public:
  59. typedef unsigned int allocation_type;
  60. typedef adaptive_pool
  61. <T, NodesPerBlock, MaxFreeBlocks, OverheadPercent
  62. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
  63. > self_t;
  64. static const std::size_t nodes_per_block = NodesPerBlock;
  65. static const std::size_t max_free_blocks = MaxFreeBlocks;
  66. static const std::size_t overhead_percent = OverheadPercent;
  67. static const std::size_t real_nodes_per_block = NodesPerBlock;
  68. BOOST_CONTAINER_DOCIGN(BOOST_STATIC_ASSERT((Version <=2)));
  69. public:
  70. //-------
  71. typedef T value_type;
  72. typedef T * pointer;
  73. typedef const T * const_pointer;
  74. typedef typename ::boost::container::
  75. container_detail::unvoid_ref<T>::type reference;
  76. typedef typename ::boost::container::
  77. container_detail::unvoid_ref<const T>::type const_reference;
  78. typedef std::size_t size_type;
  79. typedef std::ptrdiff_t difference_type;
  80. typedef boost::container::container_detail::
  81. version_type<self_t, Version> version;
  82. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  83. typedef boost::container::container_detail::
  84. basic_multiallocation_chain<void*> multiallocation_chain_void;
  85. typedef boost::container::container_detail::
  86. transform_multiallocation_chain
  87. <multiallocation_chain_void, T> multiallocation_chain;
  88. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  89. //!Obtains adaptive_pool from
  90. //!adaptive_pool
  91. template<class T2>
  92. struct rebind
  93. {
  94. typedef adaptive_pool
  95. < T2
  96. , NodesPerBlock
  97. , MaxFreeBlocks
  98. , OverheadPercent
  99. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)
  100. > other;
  101. };
  102. #ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  103. private:
  104. //!Not assignable from related adaptive_pool
  105. template<class T2, unsigned Version2, std::size_t N2, std::size_t F2>
  106. adaptive_pool& operator=
  107. (const adaptive_pool<T2, Version2, N2, F2>&);
  108. //!Not assignable from other adaptive_pool
  109. adaptive_pool& operator=(const adaptive_pool&);
  110. #endif //#ifndef BOOST_CONTAINER_DOXYGEN_INVOKED
  111. public:
  112. //!Default constructor
  113. adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
  114. {}
  115. //!Copy constructor from other adaptive_pool.
  116. adaptive_pool(const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
  117. {}
  118. //!Copy constructor from related adaptive_pool.
  119. template<class T2>
  120. adaptive_pool
  121. (const adaptive_pool<T2, NodesPerBlock, MaxFreeBlocks, OverheadPercent
  122. BOOST_CONTAINER_DOCIGN(BOOST_MOVE_I Version)> &) BOOST_NOEXCEPT_OR_NOTHROW
  123. {}
  124. //!Destructor
  125. ~adaptive_pool() BOOST_NOEXCEPT_OR_NOTHROW
  126. {}
  127. //!Returns the number of elements that could be allocated.
  128. //!Never throws
  129. size_type max_size() const BOOST_NOEXCEPT_OR_NOTHROW
  130. { return size_type(-1)/sizeof(T); }
  131. //!Allocate memory for an array of count elements.
  132. //!Throws std::bad_alloc if there is no enough memory
  133. pointer allocate(size_type count, const void * = 0)
  134. {
  135. if(BOOST_UNLIKELY(count > this->max_size()))
  136. boost::container::throw_bad_alloc();
  137. if(Version == 1 && count == 1){
  138. typedef typename container_detail::shared_adaptive_node_pool
  139. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  140. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  141. return pointer(static_cast<T*>(singleton_t::instance().allocate_node()));
  142. }
  143. else{
  144. return static_cast<pointer>(dlmalloc_malloc(count*sizeof(T)));
  145. }
  146. }
  147. //!Deallocate allocated memory.
  148. //!Never throws
  149. void deallocate(const pointer &ptr, size_type count) BOOST_NOEXCEPT_OR_NOTHROW
  150. {
  151. (void)count;
  152. if(Version == 1 && count == 1){
  153. typedef container_detail::shared_adaptive_node_pool
  154. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  155. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  156. singleton_t::instance().deallocate_node(ptr);
  157. }
  158. else{
  159. dlmalloc_free(ptr);
  160. }
  161. }
  162. pointer allocation_command(allocation_type command,
  163. size_type limit_size,
  164. size_type &prefer_in_recvd_out_size,
  165. pointer &reuse)
  166. {
  167. pointer ret = this->priv_allocation_command(command, limit_size, prefer_in_recvd_out_size, reuse);
  168. if(BOOST_UNLIKELY(!ret && !(command & BOOST_CONTAINER_NOTHROW_ALLOCATION)))
  169. boost::container::throw_bad_alloc();
  170. return ret;
  171. }
  172. //!Returns maximum the number of objects the previously allocated memory
  173. //!pointed by p can hold.
  174. size_type size(pointer p) const BOOST_NOEXCEPT_OR_NOTHROW
  175. { return dlmalloc_size(p); }
  176. //!Allocates just one object. Memory allocated with this function
  177. //!must be deallocated only with deallocate_one().
  178. //!Throws bad_alloc if there is no enough memory
  179. pointer allocate_one()
  180. {
  181. typedef container_detail::shared_adaptive_node_pool
  182. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  183. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  184. return (pointer)singleton_t::instance().allocate_node();
  185. }
  186. //!Allocates many elements of size == 1.
  187. //!Elements must be individually deallocated with deallocate_one()
  188. void allocate_individual(std::size_t num_elements, multiallocation_chain &chain)
  189. {
  190. typedef container_detail::shared_adaptive_node_pool
  191. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  192. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  193. singleton_t::instance().allocate_nodes(num_elements, static_cast<typename shared_pool_t::multiallocation_chain&>(chain));
  194. //typename shared_pool_t::multiallocation_chain ch;
  195. //singleton_t::instance().allocate_nodes(num_elements, ch);
  196. //chain.incorporate_after
  197. //(chain.before_begin(), (T*)&*ch.begin(), (T*)&*ch.last(), ch.size());
  198. }
  199. //!Deallocates memory previously allocated with allocate_one().
  200. //!You should never use deallocate_one to deallocate memory allocated
  201. //!with other functions different from allocate_one(). Never throws
  202. void deallocate_one(pointer p) BOOST_NOEXCEPT_OR_NOTHROW
  203. {
  204. typedef container_detail::shared_adaptive_node_pool
  205. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  206. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  207. singleton_t::instance().deallocate_node(p);
  208. }
  209. void deallocate_individual(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  210. {
  211. typedef container_detail::shared_adaptive_node_pool
  212. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  213. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  214. //typename shared_pool_t::multiallocation_chain ch(&*chain.begin(), &*chain.last(), chain.size());
  215. //singleton_t::instance().deallocate_nodes(ch);
  216. singleton_t::instance().deallocate_nodes(chain);
  217. }
  218. //!Allocates many elements of size elem_size.
  219. //!Elements must be individually deallocated with deallocate()
  220. void allocate_many(size_type elem_size, std::size_t n_elements, multiallocation_chain &chain)
  221. {
  222. BOOST_STATIC_ASSERT(( Version > 1 ));/*
  223. dlmalloc_memchain ch;
  224. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  225. if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes(n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
  226. boost::container::throw_bad_alloc();
  227. }
  228. chain.incorporate_after(chain.before_begin()
  229. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  230. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  231. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
  232. if(BOOST_UNLIKELY(!dlmalloc_multialloc_nodes
  233. (n_elements, elem_size*sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
  234. boost::container::throw_bad_alloc();
  235. }
  236. }
  237. //!Allocates n_elements elements, each one of size elem_sizes[i]
  238. //!Elements must be individually deallocated with deallocate()
  239. void allocate_many(const size_type *elem_sizes, size_type n_elements, multiallocation_chain &chain)
  240. {
  241. BOOST_STATIC_ASSERT(( Version > 1 ));/*
  242. dlmalloc_memchain ch;
  243. BOOST_CONTAINER_MEMCHAIN_INIT(&ch);
  244. if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays(n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, &ch))){
  245. boost::container::throw_bad_alloc();
  246. }
  247. chain.incorporate_after(chain.before_begin()
  248. ,(T*)BOOST_CONTAINER_MEMCHAIN_FIRSTMEM(&ch)
  249. ,(T*)BOOST_CONTAINER_MEMCHAIN_LASTMEM(&ch)
  250. ,BOOST_CONTAINER_MEMCHAIN_SIZE(&ch) );*/
  251. if(BOOST_UNLIKELY(!dlmalloc_multialloc_arrays
  252. (n_elements, elem_sizes, sizeof(T), DL_MULTIALLOC_DEFAULT_CONTIGUOUS, reinterpret_cast<dlmalloc_memchain *>(&chain)))){
  253. boost::container::throw_bad_alloc();
  254. }
  255. }
  256. void deallocate_many(multiallocation_chain &chain) BOOST_NOEXCEPT_OR_NOTHROW
  257. {/*
  258. dlmalloc_memchain ch;
  259. void *beg(&*chain.begin()), *last(&*chain.last());
  260. size_t size(chain.size());
  261. BOOST_CONTAINER_MEMCHAIN_INIT_FROM(&ch, beg, last, size);
  262. dlmalloc_multidealloc(&ch);*/
  263. dlmalloc_multidealloc(reinterpret_cast<dlmalloc_memchain *>(&chain));
  264. }
  265. //!Deallocates all free blocks of the pool
  266. static void deallocate_free_blocks() BOOST_NOEXCEPT_OR_NOTHROW
  267. {
  268. typedef container_detail::shared_adaptive_node_pool
  269. <sizeof(T), NodesPerBlock, MaxFreeBlocks, OverheadPercent> shared_pool_t;
  270. typedef container_detail::singleton_default<shared_pool_t> singleton_t;
  271. singleton_t::instance().deallocate_free_blocks();
  272. }
  273. //!Swaps allocators. Does not throw. If each allocator is placed in a
  274. //!different memory segment, the result is undefined.
  275. friend void swap(adaptive_pool &, adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
  276. {}
  277. //!An allocator always compares to true, as memory allocated with one
  278. //!instance can be deallocated by another instance
  279. friend bool operator==(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
  280. { return true; }
  281. //!An allocator always compares to false, as memory allocated with one
  282. //!instance can be deallocated by another instance
  283. friend bool operator!=(const adaptive_pool &, const adaptive_pool &) BOOST_NOEXCEPT_OR_NOTHROW
  284. { return false; }
  285. private:
  286. pointer priv_allocation_command
  287. (allocation_type command, std::size_t limit_size
  288. ,size_type &prefer_in_recvd_out_size, pointer &reuse_ptr)
  289. {
  290. std::size_t const preferred_size = prefer_in_recvd_out_size;
  291. dlmalloc_command_ret_t ret = {0 , 0};
  292. if(BOOST_UNLIKELY(limit_size > this->max_size() || preferred_size > this->max_size())){
  293. return pointer();
  294. }
  295. std::size_t l_size = limit_size*sizeof(T);
  296. std::size_t p_size = preferred_size*sizeof(T);
  297. std::size_t r_size;
  298. {
  299. void* reuse_ptr_void = reuse_ptr;
  300. ret = dlmalloc_allocation_command(command, sizeof(T), l_size, p_size, &r_size, reuse_ptr_void);
  301. reuse_ptr = ret.second ? static_cast<T*>(reuse_ptr_void) : 0;
  302. }
  303. prefer_in_recvd_out_size = r_size/sizeof(T);
  304. return (pointer)ret.first;
  305. }
  306. };
  307. } //namespace container {
  308. } //namespace boost {
  309. #include <boost/container/detail/config_end.hpp>
  310. #endif //#ifndef BOOST_CONTAINER_ADAPTIVE_POOL_HPP