/Src/Dependencies/Boost/boost/interprocess/containers/container/detail/node_pool_impl.hpp

http://hadesmem.googlecode.com/ · C++ Header · 366 lines · 255 code · 51 blank · 60 comment · 23 complexity · d6e4eaf55eef94e6d6bd1bffef6ce0cf 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/container for documentation.
  8. //
  9. //////////////////////////////////////////////////////////////////////////////
  10. #ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_IMPL_HPP
  11. #define BOOST_CONTAINER_DETAIL_NODE_POOL_IMPL_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include "config_begin.hpp"
  16. #include INCLUDE_BOOST_CONTAINER_CONTAINER_FWD_HPP
  17. #include INCLUDE_BOOST_CONTAINER_DETAIL_WORKAROUND_HPP
  18. #include INCLUDE_BOOST_CONTAINER_DETAIL_UTILITIES_HPP
  19. #include <boost/pointer_to_other.hpp>
  20. #include <boost/intrusive/set.hpp>
  21. #include <boost/intrusive/slist.hpp>
  22. #include INCLUDE_BOOST_CONTAINER_DETAIL_TYPE_TRAITS_HPP
  23. #include INCLUDE_BOOST_CONTAINER_DETAIL_MATH_FUNCTIONS_HPP
  24. #include INCLUDE_BOOST_CONTAINER_DETAIL_MPL_HPP
  25. #include INCLUDE_BOOST_CONTAINER_DETAIL_POOL_COMMON_HPP
  26. #include <boost/assert.hpp>
  27. #include <cstddef>
  28. #include <functional> //std::unary_function
  29. namespace boost {
  30. namespace container {
  31. namespace containers_detail {
  32. template<class SegmentManagerBase>
  33. class private_node_pool_impl
  34. {
  35. //Non-copyable
  36. private_node_pool_impl();
  37. private_node_pool_impl(const private_node_pool_impl &);
  38. private_node_pool_impl &operator=(const private_node_pool_impl &);
  39. //A node object will hold node_t when it's not allocated
  40. public:
  41. typedef typename SegmentManagerBase::void_pointer void_pointer;
  42. typedef typename node_slist<void_pointer>::slist_hook_t slist_hook_t;
  43. typedef typename node_slist<void_pointer>::node_t node_t;
  44. typedef typename node_slist<void_pointer>::node_slist_t free_nodes_t;
  45. typedef typename SegmentManagerBase::multiallocation_chain multiallocation_chain;
  46. private:
  47. typedef typename bi::make_slist
  48. < node_t, bi::base_hook<slist_hook_t>
  49. , bi::linear<true>
  50. , bi::constant_time_size<false> >::type blockslist_t;
  51. public:
  52. //!Segment manager typedef
  53. typedef SegmentManagerBase segment_manager_base_type;
  54. //!Constructor from a segment manager. Never throws
  55. private_node_pool_impl(segment_manager_base_type *segment_mngr_base, std::size_t node_size, std::size_t nodes_per_block)
  56. : m_nodes_per_block(nodes_per_block)
  57. , m_real_node_size(lcm(node_size, std::size_t(alignment_of<node_t>::value)))
  58. //General purpose allocator
  59. , mp_segment_mngr_base(segment_mngr_base)
  60. , m_blocklist()
  61. , m_freelist()
  62. //Debug node count
  63. , m_allocated(0)
  64. {}
  65. //!Destructor. Deallocates all allocated blocks. Never throws
  66. ~private_node_pool_impl()
  67. { this->purge_blocks(); }
  68. std::size_t get_real_num_node() const
  69. { return m_nodes_per_block; }
  70. //!Returns the segment manager. Never throws
  71. segment_manager_base_type* get_segment_manager_base()const
  72. { return containers_detail::get_pointer(mp_segment_mngr_base); }
  73. void *allocate_node()
  74. { return priv_alloc_node(); }
  75. //!Deallocates an array pointed by ptr. Never throws
  76. void deallocate_node(void *ptr)
  77. { priv_dealloc_node(ptr); }
  78. //!Allocates a singly linked list of n nodes ending in null pointer.
  79. multiallocation_chain allocate_nodes(const std::size_t n)
  80. {
  81. //Preallocate all needed blocks to fulfill the request
  82. std::size_t cur_nodes = m_freelist.size();
  83. if(cur_nodes < n){
  84. priv_alloc_block(((n - cur_nodes) - 1)/m_nodes_per_block + 1);
  85. }
  86. //We just iterate the needed nodes to get the last we'll erase
  87. typedef typename free_nodes_t::iterator free_iterator;
  88. free_iterator before_last_new_it = m_freelist.before_begin();
  89. for(std::size_t j = 0; j != n; ++j){
  90. ++before_last_new_it;
  91. }
  92. //Cache the first node of the allocated range before erasing
  93. free_iterator first_node(m_freelist.begin());
  94. free_iterator last_node (before_last_new_it);
  95. //Erase the range. Since we already have the distance, this is O(1)
  96. m_freelist.erase_after( m_freelist.before_begin()
  97. , ++free_iterator(before_last_new_it)
  98. , n);
  99. //Now take the last erased node and just splice it in the end
  100. //of the intrusive list that will be traversed by the multialloc iterator.
  101. multiallocation_chain chain;
  102. chain.incorporate_after(chain.before_begin(), &*first_node, &*last_node, n);
  103. m_allocated += n;
  104. return BOOST_CONTAINER_MOVE_NAMESPACE::move(chain);
  105. }
  106. void deallocate_nodes(multiallocation_chain chain)
  107. {
  108. typedef typename multiallocation_chain::iterator iterator;
  109. iterator it(chain.begin()), itend(chain.end());
  110. while(it != itend){
  111. void *pElem = &*it;
  112. ++it;
  113. priv_dealloc_node(pElem);
  114. }
  115. }
  116. //!Deallocates all the free blocks of memory. Never throws
  117. void deallocate_free_blocks()
  118. {
  119. typedef typename free_nodes_t::iterator nodelist_iterator;
  120. typename blockslist_t::iterator bit(m_blocklist.before_begin()),
  121. it(m_blocklist.begin()),
  122. itend(m_blocklist.end());
  123. free_nodes_t backup_list;
  124. nodelist_iterator backup_list_last = backup_list.before_begin();
  125. //Execute the algorithm and get an iterator to the last value
  126. std::size_t blocksize = get_rounded_size
  127. (m_real_node_size*m_nodes_per_block, alignment_of<node_t>::value);
  128. while(it != itend){
  129. //Collect all the nodes from the block pointed by it
  130. //and push them in the list
  131. free_nodes_t free_nodes;
  132. nodelist_iterator last_it = free_nodes.before_begin();
  133. const void *addr = get_block_from_hook(&*it, blocksize);
  134. m_freelist.remove_and_dispose_if
  135. (is_between(addr, blocksize), push_in_list(free_nodes, last_it));
  136. //If the number of nodes is equal to m_nodes_per_block
  137. //this means that the block can be deallocated
  138. if(free_nodes.size() == m_nodes_per_block){
  139. //Unlink the nodes
  140. free_nodes.clear();
  141. it = m_blocklist.erase_after(bit);
  142. mp_segment_mngr_base->deallocate((void*)addr);
  143. }
  144. //Otherwise, insert them in the backup list, since the
  145. //next "remove_if" does not need to check them again.
  146. else{
  147. //Assign the iterator to the last value if necessary
  148. if(backup_list.empty() && !m_freelist.empty()){
  149. backup_list_last = last_it;
  150. }
  151. //Transfer nodes. This is constant time.
  152. backup_list.splice_after
  153. ( backup_list.before_begin()
  154. , free_nodes
  155. , free_nodes.before_begin()
  156. , last_it
  157. , free_nodes.size());
  158. bit = it;
  159. ++it;
  160. }
  161. }
  162. //We should have removed all the nodes from the free list
  163. BOOST_ASSERT(m_freelist.empty());
  164. //Now pass all the node to the free list again
  165. m_freelist.splice_after
  166. ( m_freelist.before_begin()
  167. , backup_list
  168. , backup_list.before_begin()
  169. , backup_list_last
  170. , backup_list.size());
  171. }
  172. std::size_t num_free_nodes()
  173. { return m_freelist.size(); }
  174. //!Deallocates all used memory. Precondition: all nodes allocated from this pool should
  175. //!already be deallocated. Otherwise, undefined behaviour. Never throws
  176. void purge_blocks()
  177. {
  178. //check for memory leaks
  179. BOOST_ASSERT(m_allocated==0);
  180. std::size_t blocksize = get_rounded_size
  181. (m_real_node_size*m_nodes_per_block, alignment_of<node_t>::value);
  182. typename blockslist_t::iterator
  183. it(m_blocklist.begin()), itend(m_blocklist.end()), aux;
  184. //We iterate though the NodeBlock list to free the memory
  185. while(!m_blocklist.empty()){
  186. void *addr = get_block_from_hook(&m_blocklist.front(), blocksize);
  187. m_blocklist.pop_front();
  188. mp_segment_mngr_base->deallocate((void*)addr);
  189. }
  190. //Just clear free node list
  191. m_freelist.clear();
  192. }
  193. void swap(private_node_pool_impl &other)
  194. {
  195. BOOST_ASSERT(m_nodes_per_block == other.m_nodes_per_block);
  196. BOOST_ASSERT(m_real_node_size == other.m_real_node_size);
  197. std::swap(mp_segment_mngr_base, other.mp_segment_mngr_base);
  198. m_blocklist.swap(other.m_blocklist);
  199. m_freelist.swap(other.m_freelist);
  200. std::swap(m_allocated, other.m_allocated);
  201. }
  202. private:
  203. struct push_in_list
  204. {
  205. push_in_list(free_nodes_t &l, typename free_nodes_t::iterator &it)
  206. : slist_(l), last_it_(it)
  207. {}
  208. void operator()(typename free_nodes_t::pointer p) const
  209. {
  210. slist_.push_front(*p);
  211. if(slist_.size() == 1){ //Cache last element
  212. ++last_it_ = slist_.begin();
  213. }
  214. }
  215. private:
  216. free_nodes_t &slist_;
  217. typename free_nodes_t::iterator &last_it_;
  218. };
  219. struct is_between
  220. : std::unary_function<typename free_nodes_t::value_type, bool>
  221. {
  222. is_between(const void *addr, std::size_t size)
  223. : beg_(static_cast<const char *>(addr)), end_(beg_+size)
  224. {}
  225. bool operator()(typename free_nodes_t::const_reference v) const
  226. {
  227. return (beg_ <= reinterpret_cast<const char *>(&v) &&
  228. end_ > reinterpret_cast<const char *>(&v));
  229. }
  230. private:
  231. const char * beg_;
  232. const char * end_;
  233. };
  234. //!Allocates one node, using single segregated storage algorithm.
  235. //!Never throws
  236. node_t *priv_alloc_node()
  237. {
  238. //If there are no free nodes we allocate a new block
  239. if (m_freelist.empty())
  240. priv_alloc_block();
  241. //We take the first free node
  242. node_t *n = (node_t*)&m_freelist.front();
  243. m_freelist.pop_front();
  244. ++m_allocated;
  245. return n;
  246. }
  247. //!Deallocates one node, using single segregated storage algorithm.
  248. //!Never throws
  249. void priv_dealloc_node(void *pElem)
  250. {
  251. //We put the node at the beginning of the free node list
  252. node_t * to_deallocate = static_cast<node_t*>(pElem);
  253. m_freelist.push_front(*to_deallocate);
  254. BOOST_ASSERT(m_allocated>0);
  255. --m_allocated;
  256. }
  257. //!Allocates several blocks of nodes. Can throw
  258. void priv_alloc_block(std::size_t num_blocks = 1)
  259. {
  260. if(!num_blocks)
  261. return;
  262. std::size_t blocksize =
  263. get_rounded_size(m_real_node_size*m_nodes_per_block, alignment_of<node_t>::value);
  264. try{
  265. for(std::size_t i = 0; i != num_blocks; ++i){
  266. //We allocate a new NodeBlock and put it as first
  267. //element in the free Node list
  268. char *pNode = reinterpret_cast<char*>
  269. (mp_segment_mngr_base->allocate(blocksize + sizeof(node_t)));
  270. char *pBlock = pNode;
  271. m_blocklist.push_front(get_block_hook(pBlock, blocksize));
  272. //We initialize all Nodes in Node Block to insert
  273. //them in the free Node list
  274. for(std::size_t i = 0; i < m_nodes_per_block; ++i, pNode += m_real_node_size){
  275. m_freelist.push_front(*new (pNode) node_t);
  276. }
  277. }
  278. }
  279. catch(...){
  280. //to-do: if possible, an efficient way to deallocate allocated blocks
  281. throw;
  282. }
  283. }
  284. //!Deprecated, use deallocate_free_blocks
  285. void deallocate_free_chunks()
  286. { this->deallocate_free_blocks(); }
  287. //!Deprecated, use purge_blocks
  288. void purge_chunks()
  289. { this->purge_blocks(); }
  290. private:
  291. //!Returns a reference to the block hook placed in the end of the block
  292. static node_t & get_block_hook (void *block, std::size_t blocksize)
  293. {
  294. return *reinterpret_cast<node_t*>(reinterpret_cast<char*>(block) + blocksize);
  295. }
  296. //!Returns the starting address of the block reference to the block hook placed in the end of the block
  297. void *get_block_from_hook (node_t *hook, std::size_t blocksize)
  298. {
  299. return (reinterpret_cast<char*>(hook) - blocksize);
  300. }
  301. private:
  302. typedef typename boost::pointer_to_other
  303. <void_pointer, segment_manager_base_type>::type segment_mngr_base_ptr_t;
  304. const std::size_t m_nodes_per_block;
  305. const std::size_t m_real_node_size;
  306. segment_mngr_base_ptr_t mp_segment_mngr_base; //Segment manager
  307. blockslist_t m_blocklist; //Intrusive container of blocks
  308. free_nodes_t m_freelist; //Intrusive container of free nods
  309. std::size_t m_allocated; //Used nodes for debugging
  310. };
  311. } //namespace containers_detail {
  312. } //namespace container {
  313. } //namespace boost {
  314. #include INCLUDE_BOOST_CONTAINER_DETAIL_CONFIG_END_HPP
  315. #endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_IMPL_HPP