1 //////////////////////////////////////////////////////////////////////////////
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)
7 // See http://www.boost.org/libs/container for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
12 #define BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/container/detail/config_begin.hpp>
23 #include <boost/container/detail/workaround.hpp>
25 #include <boost/container/detail/mutex.hpp>
26 #include <boost/container/detail/pool_common_alloc.hpp>
27 #include <boost/container/detail/node_pool_impl.hpp>
28 #include <boost/container/detail/mutex.hpp>
29 #include <boost/move/utility_core.hpp>
35 namespace container_detail {
37 //!Pooled memory allocator using single segregated storage. Includes
38 //!a reference count but the class does not delete itself, this is
39 //!responsibility of user classes. Node size (NodeSize) and the number of
40 //!nodes allocated per block (NodesPerBlock) are known at compile time
41 template< std::size_t NodeSize, std::size_t NodesPerBlock >
42 class private_node_pool
43 //Inherit from the implementation to avoid template bloat
44 : public boost::container::container_detail::
45 private_node_pool_impl<fake_segment_manager>
47 typedef boost::container::container_detail::
48 private_node_pool_impl<fake_segment_manager> base_t;
50 private_node_pool(const private_node_pool &);
51 private_node_pool &operator=(const private_node_pool &);
54 typedef typename base_t::multiallocation_chain multiallocation_chain;
55 static const std::size_t nodes_per_block = NodesPerBlock;
57 //!Constructor from a segment manager. Never throws
59 : base_t(0, NodeSize, NodesPerBlock)
64 template< std::size_t NodeSize
65 , std::size_t NodesPerBlock
67 class shared_node_pool
68 : public private_node_pool<NodeSize, NodesPerBlock>
71 typedef private_node_pool<NodeSize, NodesPerBlock> private_node_allocator_t;
74 typedef typename private_node_allocator_t::free_nodes_t free_nodes_t;
75 typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain;
77 //!Constructor from a segment manager. Never throws
79 : private_node_allocator_t(){}
81 //!Destructor. Deallocates all allocated blocks. Never throws
85 //!Allocates array of count elements. Can throw std::bad_alloc
88 //-----------------------
89 scoped_lock<default_mutex> guard(mutex_);
90 //-----------------------
91 return private_node_allocator_t::allocate_node();
94 //!Deallocates an array pointed by ptr. Never throws
95 void deallocate_node(void *ptr)
97 //-----------------------
98 scoped_lock<default_mutex> guard(mutex_);
99 //-----------------------
100 private_node_allocator_t::deallocate_node(ptr);
103 //!Allocates a singly linked list of n nodes ending in null pointer.
104 //!can throw std::bad_alloc
105 void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
107 //-----------------------
108 scoped_lock<default_mutex> guard(mutex_);
109 //-----------------------
110 return private_node_allocator_t::allocate_nodes(n, chain);
113 void deallocate_nodes(multiallocation_chain &chain)
115 //-----------------------
116 scoped_lock<default_mutex> guard(mutex_);
117 //-----------------------
118 private_node_allocator_t::deallocate_nodes(chain);
121 //!Deallocates all the free blocks of memory. Never throws
122 void deallocate_free_blocks()
124 //-----------------------
125 scoped_lock<default_mutex> guard(mutex_);
126 //-----------------------
127 private_node_allocator_t::deallocate_free_blocks();
130 //!Deallocates all blocks. Never throws
133 //-----------------------
134 scoped_lock<default_mutex> guard(mutex_);
135 //-----------------------
136 private_node_allocator_t::purge_blocks();
139 std::size_t num_free_nodes()
141 //-----------------------
142 scoped_lock<default_mutex> guard(mutex_);
143 //-----------------------
144 return private_node_allocator_t::num_free_nodes();
148 default_mutex mutex_;
151 } //namespace container_detail {
152 } //namespace container {
153 } //namespace boost {
155 #include <boost/container/detail/config_end.hpp>
157 #endif //#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP