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_ADAPTIVE_NODE_POOL_HPP
|
Chris@102
|
12 #define BOOST_CONTAINER_DETAIL_ADAPTIVE_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/intrusive/set.hpp>
|
Chris@102
|
26 #include <boost/container/detail/alloc_lib_auto_link.hpp>
|
Chris@102
|
27 #include <boost/container/detail/multiallocation_chain.hpp>
|
Chris@102
|
28 #include <boost/container/detail/pool_common_alloc.hpp>
|
Chris@102
|
29 #include <boost/container/detail/mutex.hpp>
|
Chris@102
|
30 #include <boost/container/detail/adaptive_node_pool_impl.hpp>
|
Chris@102
|
31 #include <boost/container/detail/multiallocation_chain.hpp>
|
Chris@102
|
32 #include <boost/container/detail/type_traits.hpp>
|
Chris@102
|
33
|
Chris@102
|
34 #include <cstddef>
|
Chris@102
|
35 #include <cmath>
|
Chris@102
|
36 #include <cassert>
|
Chris@102
|
37
|
Chris@102
|
38
|
Chris@102
|
39 namespace boost {
|
Chris@102
|
40 namespace container {
|
Chris@102
|
41 namespace container_detail {
|
Chris@102
|
42
|
Chris@102
|
43 template<bool AlignOnly>
|
Chris@102
|
44 struct select_private_adaptive_node_pool_impl
|
Chris@102
|
45 {
|
Chris@102
|
46 typedef boost::container::container_detail::
|
Chris@102
|
47 private_adaptive_node_pool_impl
|
Chris@102
|
48 < fake_segment_manager
|
Chris@102
|
49 , unsigned(AlignOnly)*::boost::container::adaptive_pool_flag::align_only
|
Chris@102
|
50 | ::boost::container::adaptive_pool_flag::size_ordered | ::boost::container::adaptive_pool_flag::address_ordered
|
Chris@102
|
51 > type;
|
Chris@102
|
52 };
|
Chris@102
|
53
|
Chris@102
|
54 //!Pooled memory allocator using an smart adaptive pool. Includes
|
Chris@102
|
55 //!a reference count but the class does not delete itself, this is
|
Chris@102
|
56 //!responsibility of user classes. Node size (NodeSize) and the number of
|
Chris@102
|
57 //!nodes allocated per block (NodesPerBlock) are known at compile time.
|
Chris@102
|
58 template< std::size_t NodeSize
|
Chris@102
|
59 , std::size_t NodesPerBlock
|
Chris@102
|
60 , std::size_t MaxFreeBlocks
|
Chris@102
|
61 , std::size_t OverheadPercent
|
Chris@102
|
62 >
|
Chris@102
|
63 class private_adaptive_node_pool
|
Chris@102
|
64 : public select_private_adaptive_node_pool_impl<(OverheadPercent == 0)>::type
|
Chris@102
|
65 {
|
Chris@102
|
66 typedef typename select_private_adaptive_node_pool_impl<OverheadPercent == 0>::type base_t;
|
Chris@102
|
67 //Non-copyable
|
Chris@102
|
68 private_adaptive_node_pool(const private_adaptive_node_pool &);
|
Chris@102
|
69 private_adaptive_node_pool &operator=(const private_adaptive_node_pool &);
|
Chris@102
|
70
|
Chris@102
|
71 public:
|
Chris@102
|
72 typedef typename base_t::multiallocation_chain multiallocation_chain;
|
Chris@102
|
73 static const std::size_t nodes_per_block = NodesPerBlock;
|
Chris@102
|
74
|
Chris@102
|
75 //!Constructor. Never throws
|
Chris@102
|
76 private_adaptive_node_pool()
|
Chris@102
|
77 : base_t(0
|
Chris@102
|
78 , NodeSize
|
Chris@102
|
79 , NodesPerBlock
|
Chris@102
|
80 , MaxFreeBlocks
|
Chris@102
|
81 , (unsigned char)OverheadPercent)
|
Chris@102
|
82 {}
|
Chris@102
|
83 };
|
Chris@102
|
84
|
Chris@102
|
85 //!Pooled memory allocator using adaptive pool. Includes
|
Chris@102
|
86 //!a reference count but the class does not delete itself, this is
|
Chris@102
|
87 //!responsibility of user classes. Node size (NodeSize) and the number of
|
Chris@102
|
88 //!nodes allocated per block (NodesPerBlock) are known at compile time
|
Chris@102
|
89 template< std::size_t NodeSize
|
Chris@102
|
90 , std::size_t NodesPerBlock
|
Chris@102
|
91 , std::size_t MaxFreeBlocks
|
Chris@102
|
92 , std::size_t OverheadPercent
|
Chris@102
|
93 >
|
Chris@102
|
94 class shared_adaptive_node_pool
|
Chris@102
|
95 : public private_adaptive_node_pool
|
Chris@102
|
96 <NodeSize, NodesPerBlock, MaxFreeBlocks, OverheadPercent>
|
Chris@102
|
97 {
|
Chris@102
|
98 private:
|
Chris@102
|
99 typedef private_adaptive_node_pool
|
Chris@102
|
100 <NodeSize, NodesPerBlock, MaxFreeBlocks, OverheadPercent> private_node_allocator_t;
|
Chris@102
|
101 public:
|
Chris@102
|
102 typedef typename private_node_allocator_t::multiallocation_chain multiallocation_chain;
|
Chris@102
|
103
|
Chris@102
|
104 //!Constructor. Never throws
|
Chris@102
|
105 shared_adaptive_node_pool()
|
Chris@102
|
106 : private_node_allocator_t(){}
|
Chris@102
|
107
|
Chris@102
|
108 //!Destructor. Deallocates all allocated blocks. Never throws
|
Chris@102
|
109 ~shared_adaptive_node_pool()
|
Chris@102
|
110 {}
|
Chris@102
|
111
|
Chris@102
|
112 //!Allocates array of count elements. Can throw std::bad_alloc
|
Chris@102
|
113 void *allocate_node()
|
Chris@102
|
114 {
|
Chris@102
|
115 //-----------------------
|
Chris@102
|
116 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
117 //-----------------------
|
Chris@102
|
118 return private_node_allocator_t::allocate_node();
|
Chris@102
|
119 }
|
Chris@102
|
120
|
Chris@102
|
121 //!Deallocates an array pointed by ptr. Never throws
|
Chris@102
|
122 void deallocate_node(void *ptr)
|
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_node(ptr);
|
Chris@102
|
128 }
|
Chris@102
|
129
|
Chris@102
|
130 //!Allocates a singly linked list of n nodes ending in null pointer.
|
Chris@102
|
131 //!can throw std::bad_alloc
|
Chris@102
|
132 void allocate_nodes(const std::size_t n, multiallocation_chain &chain)
|
Chris@102
|
133 {
|
Chris@102
|
134 //-----------------------
|
Chris@102
|
135 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
136 //-----------------------
|
Chris@102
|
137 return private_node_allocator_t::allocate_nodes(n, chain);
|
Chris@102
|
138 }
|
Chris@102
|
139
|
Chris@102
|
140 void deallocate_nodes(multiallocation_chain &chain)
|
Chris@102
|
141 {
|
Chris@102
|
142 //-----------------------
|
Chris@102
|
143 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
144 //-----------------------
|
Chris@102
|
145 private_node_allocator_t::deallocate_nodes(chain);
|
Chris@102
|
146 }
|
Chris@102
|
147
|
Chris@102
|
148 //!Deallocates all the free blocks of memory. Never throws
|
Chris@102
|
149 void deallocate_free_blocks()
|
Chris@102
|
150 {
|
Chris@102
|
151 //-----------------------
|
Chris@102
|
152 scoped_lock<default_mutex> guard(mutex_);
|
Chris@102
|
153 //-----------------------
|
Chris@102
|
154 private_node_allocator_t::deallocate_free_blocks();
|
Chris@102
|
155 }
|
Chris@102
|
156
|
Chris@102
|
157 private:
|
Chris@102
|
158 default_mutex mutex_;
|
Chris@102
|
159 };
|
Chris@102
|
160
|
Chris@102
|
161 } //namespace container_detail {
|
Chris@102
|
162 } //namespace container {
|
Chris@102
|
163 } //namespace boost {
|
Chris@102
|
164
|
Chris@102
|
165 #include <boost/container/detail/config_end.hpp>
|
Chris@102
|
166
|
Chris@102
|
167 #endif //#ifndef BOOST_CONTAINER_DETAIL_ADAPTIVE_NODE_POOL_HPP
|