Chris@102
|
1 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
2 //
|
Chris@102
|
3 // (C) Copyright Ion Gaztanaga 2005-2013. Distributed under the Boost
|
Chris@102
|
4 // Software License, Version 1.0. (See accompanying file
|
Chris@102
|
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@102
|
6 //
|
Chris@102
|
7 // See http://www.boost.org/libs/container for documentation.
|
Chris@102
|
8 //
|
Chris@102
|
9 //////////////////////////////////////////////////////////////////////////////
|
Chris@102
|
10
|
Chris@102
|
11 #ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
|
Chris@102
|
12 #define BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
|
Chris@102
|
13
|
Chris@102
|
14 #ifndef BOOST_CONFIG_HPP
|
Chris@102
|
15 # include <boost/config.hpp>
|
Chris@102
|
16 #endif
|
Chris@102
|
17
|
Chris@102
|
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
|
Chris@102
|
19 # pragma once
|
Chris@102
|
20 #endif
|
Chris@102
|
21
|
Chris@102
|
22 #include <boost/container/detail/config_begin.hpp>
|
Chris@102
|
23 #include <boost/container/detail/workaround.hpp>
|
Chris@102
|
24
|
Chris@102
|
25 #include <boost/container/detail/mutex.hpp>
|
Chris@102
|
26 #include <boost/container/detail/pool_common_alloc.hpp>
|
Chris@102
|
27 #include <boost/container/detail/node_pool_impl.hpp>
|
Chris@102
|
28 #include <boost/container/detail/mutex.hpp>
|
Chris@102
|
29 #include <boost/move/utility_core.hpp>
|
Chris@102
|
30 #include <cstddef>
|
Chris@102
|
31 #include <cassert>
|
Chris@102
|
32
|
Chris@102
|
33 namespace boost {
|
Chris@102
|
34 namespace container {
|
Chris@102
|
35 namespace container_detail {
|
Chris@102
|
36
|
Chris@102
|
37 //!Pooled memory allocator using single segregated storage. Includes
|
Chris@102
|
38 //!a reference count but the class does not delete itself, this is
|
Chris@102
|
39 //!responsibility of user classes. Node size (NodeSize) and the number of
|
Chris@102
|
40 //!nodes allocated per block (NodesPerBlock) are known at compile time
|
Chris@102
|
41 template< std::size_t NodeSize, std::size_t NodesPerBlock >
|
Chris@102
|
42 class private_node_pool
|
Chris@102
|
43 //Inherit from the implementation to avoid template bloat
|
Chris@102
|
44 : public boost::container::container_detail::
|
Chris@102
|
45 private_node_pool_impl<fake_segment_manager>
|
Chris@102
|
46 {
|
Chris@102
|
47 typedef boost::container::container_detail::
|
Chris@102
|
48 private_node_pool_impl<fake_segment_manager> base_t;
|
Chris@102
|
49 //Non-copyable
|
Chris@102
|
50 private_node_pool(const private_node_pool &);
|
Chris@102
|
51 private_node_pool &operator=(const private_node_pool &);
|
Chris@102
|
52
|
Chris@102
|
53 public:
|
Chris@102
|
54 typedef typename base_t::multiallocation_chain multiallocation_chain;
|
Chris@102
|
55 static const std::size_t nodes_per_block = NodesPerBlock;
|
Chris@102
|
56
|
Chris@102
|
57 //!Constructor from a segment manager. Never throws
|
Chris@102
|
58 private_node_pool()
|
Chris@102
|
59 : base_t(0, NodeSize, NodesPerBlock)
|
Chris@102
|
60 {}
|
Chris@102
|
61
|
Chris@102
|
62 };
|
Chris@102
|
63
|
Chris@102
|
64 template< std::size_t NodeSize
|
Chris@102
|
65 , std::size_t NodesPerBlock
|
Chris@102
|
66 >
|
Chris@102
|
67 class shared_node_pool
|
Chris@102
|
68 : public private_node_pool<NodeSize, NodesPerBlock>
|
Chris@102
|
69 {
|
Chris@102
|
70 private:
|
Chris@102
|
71 typedef private_node_pool<NodeSize, NodesPerBlock> private_node_allocator_t;
|
Chris@102
|
72
|
Chris@102
|
73 public:
|
Chris@102
|
74 typedef typename private_node_allocator_t::free_nodes_t free_nodes_t;
|
Chris@102
|
75 typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain;
|
Chris@102
|
76
|
Chris@102
|
77 //!Constructor from a segment manager. Never throws
|
Chris@102
|
78 shared_node_pool()
|
Chris@102
|
79 : private_node_allocator_t(){}
|
Chris@102
|
80
|
Chris@102
|
81 //!Destructor. Deallocates all allocated blocks. Never throws
|
Chris@102
|
82 ~shared_node_pool()
|
Chris@102
|
83 {}
|
Chris@102
|
84
|
Chris@102
|
85 //!Allocates array of count elements. Can throw std::bad_alloc
|
Chris@102
|
86 void *allocate_node()
|
Chris@102
|
87 {
|
Chris@102
|
88 //-----------------------
|
Chris@102
|
89 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
90 //-----------------------
|
Chris@102
|
91 return private_node_allocator_t::allocate_node();
|
Chris@102
|
92 }
|
Chris@102
|
93
|
Chris@102
|
94 //!Deallocates an array pointed by ptr. Never throws
|
Chris@102
|
95 void deallocate_node(void *ptr)
|
Chris@102
|
96 {
|
Chris@102
|
97 //-----------------------
|
Chris@102
|
98 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
99 //-----------------------
|
Chris@102
|
100 private_node_allocator_t::deallocate_node(ptr);
|
Chris@102
|
101 }
|
Chris@102
|
102
|
Chris@102
|
103 //!Allocates a singly linked list of n nodes ending in null pointer.
|
Chris@102
|
104 //!can throw std::bad_alloc
|
Chris@102
|
105 void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
|
Chris@102
|
106 {
|
Chris@102
|
107 //-----------------------
|
Chris@102
|
108 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
109 //-----------------------
|
Chris@102
|
110 return private_node_allocator_t::allocate_nodes(n, chain);
|
Chris@102
|
111 }
|
Chris@102
|
112
|
Chris@102
|
113 void deallocate_nodes(multiallocation_chain &chain)
|
Chris@102
|
114 {
|
Chris@102
|
115 //-----------------------
|
Chris@102
|
116 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
117 //-----------------------
|
Chris@102
|
118 private_node_allocator_t::deallocate_nodes(chain);
|
Chris@102
|
119 }
|
Chris@102
|
120
|
Chris@102
|
121 //!Deallocates all the free blocks of memory. Never throws
|
Chris@102
|
122 void deallocate_free_blocks()
|
Chris@102
|
123 {
|
Chris@102
|
124 //-----------------------
|
Chris@102
|
125 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
126 //-----------------------
|
Chris@102
|
127 private_node_allocator_t::deallocate_free_blocks();
|
Chris@102
|
128 }
|
Chris@102
|
129
|
Chris@102
|
130 //!Deallocates all blocks. Never throws
|
Chris@102
|
131 void purge_blocks()
|
Chris@102
|
132 {
|
Chris@102
|
133 //-----------------------
|
Chris@102
|
134 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
135 //-----------------------
|
Chris@102
|
136 private_node_allocator_t::purge_blocks();
|
Chris@102
|
137 }
|
Chris@102
|
138
|
Chris@102
|
139 std::size_t num_free_nodes()
|
Chris@102
|
140 {
|
Chris@102
|
141 //-----------------------
|
Chris@102
|
142 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
143 //-----------------------
|
Chris@102
|
144 return private_node_allocator_t::num_free_nodes();
|
Chris@102
|
145 }
|
Chris@102
|
146
|
Chris@102
|
147 private:
|
Chris@102
|
148 default_mutex mutex_;
|
Chris@102
|
149 };
|
Chris@102
|
150
|
Chris@102
|
151 } //namespace container_detail {
|
Chris@102
|
152 } //namespace container {
|
Chris@102
|
153 } //namespace boost {
|
Chris@102
|
154
|
Chris@102
|
155 #include <boost/container/detail/config_end.hpp>
|
Chris@102
|
156
|
Chris@102
|
157 #endif //#ifndef BOOST_CONTAINER_DETAIL_NODE_POOL_HPP
|