1 | // Copyright (C) 2000, 2001 Stephen Cleary |
---|
2 | // |
---|
3 | // Distributed under the Boost Software License, Version 1.0. (See |
---|
4 | // accompanying file LICENSE_1_0.txt or copy at |
---|
5 | // http://www.boost.org/LICENSE_1_0.txt) |
---|
6 | // |
---|
7 | // See http://www.boost.org for updates, documentation, and revision history. |
---|
8 | |
---|
9 | #ifndef BOOST_POOL_ALLOC_HPP |
---|
10 | #define BOOST_POOL_ALLOC_HPP |
---|
11 | |
---|
12 | // std::numeric_limits |
---|
13 | #include <boost/limits.hpp> |
---|
14 | // new, std::bad_alloc |
---|
15 | #include <new> |
---|
16 | |
---|
17 | #include <boost/pool/poolfwd.hpp> |
---|
18 | |
---|
19 | // boost::singleton_pool |
---|
20 | #include <boost/pool/singleton_pool.hpp> |
---|
21 | |
---|
22 | #include <boost/detail/workaround.hpp> |
---|
23 | |
---|
24 | // The following code will be put into Boost.Config in a later revision |
---|
25 | #if defined(_RWSTD_VER) || defined(__SGI_STL_PORT) || \ |
---|
26 | BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x582)) |
---|
27 | #define BOOST_NO_PROPER_STL_DEALLOCATE |
---|
28 | #endif |
---|
29 | |
---|
30 | namespace boost { |
---|
31 | |
---|
32 | struct pool_allocator_tag { }; |
---|
33 | |
---|
34 | template <typename T, |
---|
35 | typename UserAllocator, |
---|
36 | typename Mutex, |
---|
37 | unsigned NextSize> |
---|
38 | class pool_allocator |
---|
39 | { |
---|
40 | public: |
---|
41 | typedef T value_type; |
---|
42 | typedef UserAllocator user_allocator; |
---|
43 | typedef Mutex mutex; |
---|
44 | BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); |
---|
45 | |
---|
46 | typedef value_type * pointer; |
---|
47 | typedef const value_type * const_pointer; |
---|
48 | typedef value_type & reference; |
---|
49 | typedef const value_type & const_reference; |
---|
50 | typedef typename pool<UserAllocator>::size_type size_type; |
---|
51 | typedef typename pool<UserAllocator>::difference_type difference_type; |
---|
52 | |
---|
53 | template <typename U> |
---|
54 | struct rebind |
---|
55 | { |
---|
56 | typedef pool_allocator<U, UserAllocator, Mutex, NextSize> other; |
---|
57 | }; |
---|
58 | |
---|
59 | public: |
---|
60 | pool_allocator() { } |
---|
61 | |
---|
62 | // default copy constructor |
---|
63 | |
---|
64 | // default assignment operator |
---|
65 | |
---|
66 | // not explicit, mimicking std::allocator [20.4.1] |
---|
67 | template <typename U> |
---|
68 | pool_allocator(const pool_allocator<U, UserAllocator, Mutex, NextSize> &) |
---|
69 | { } |
---|
70 | |
---|
71 | // default destructor |
---|
72 | |
---|
73 | static pointer address(reference r) |
---|
74 | { return &r; } |
---|
75 | static const_pointer address(const_reference s) |
---|
76 | { return &s; } |
---|
77 | static size_type max_size() |
---|
78 | { return (std::numeric_limits<size_type>::max)(); } |
---|
79 | static void construct(const pointer ptr, const value_type & t) |
---|
80 | { new (ptr) T(t); } |
---|
81 | static void destroy(const pointer ptr) |
---|
82 | { |
---|
83 | ptr->~T(); |
---|
84 | (void) ptr; // avoid unused variable warning |
---|
85 | } |
---|
86 | |
---|
87 | bool operator==(const pool_allocator &) const |
---|
88 | { return true; } |
---|
89 | bool operator!=(const pool_allocator &) const |
---|
90 | { return false; } |
---|
91 | |
---|
92 | static pointer allocate(const size_type n) |
---|
93 | { |
---|
94 | const pointer ret = static_cast<pointer>( |
---|
95 | singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, |
---|
96 | NextSize>::ordered_malloc(n) ); |
---|
97 | if (ret == 0) |
---|
98 | throw std::bad_alloc(); |
---|
99 | return ret; |
---|
100 | } |
---|
101 | static pointer allocate(const size_type n, const void * const) |
---|
102 | { return allocate(n); } |
---|
103 | static void deallocate(const pointer ptr, const size_type n) |
---|
104 | { |
---|
105 | #ifdef BOOST_NO_PROPER_STL_DEALLOCATE |
---|
106 | if (ptr == 0 || n == 0) |
---|
107 | return; |
---|
108 | #endif |
---|
109 | singleton_pool<pool_allocator_tag, sizeof(T), UserAllocator, Mutex, |
---|
110 | NextSize>::ordered_free(ptr, n); |
---|
111 | } |
---|
112 | }; |
---|
113 | |
---|
114 | struct fast_pool_allocator_tag { }; |
---|
115 | |
---|
116 | template <typename T, |
---|
117 | typename UserAllocator, |
---|
118 | typename Mutex, |
---|
119 | unsigned NextSize> |
---|
120 | class fast_pool_allocator |
---|
121 | { |
---|
122 | public: |
---|
123 | typedef T value_type; |
---|
124 | typedef UserAllocator user_allocator; |
---|
125 | typedef Mutex mutex; |
---|
126 | BOOST_STATIC_CONSTANT(unsigned, next_size = NextSize); |
---|
127 | |
---|
128 | typedef value_type * pointer; |
---|
129 | typedef const value_type * const_pointer; |
---|
130 | typedef value_type & reference; |
---|
131 | typedef const value_type & const_reference; |
---|
132 | typedef typename pool<UserAllocator>::size_type size_type; |
---|
133 | typedef typename pool<UserAllocator>::difference_type difference_type; |
---|
134 | |
---|
135 | template <typename U> |
---|
136 | struct rebind |
---|
137 | { |
---|
138 | typedef fast_pool_allocator<U, UserAllocator, Mutex, NextSize> other; |
---|
139 | }; |
---|
140 | |
---|
141 | public: |
---|
142 | fast_pool_allocator() { } |
---|
143 | |
---|
144 | // default copy constructor |
---|
145 | |
---|
146 | // default assignment operator |
---|
147 | |
---|
148 | // not explicit, mimicking std::allocator [20.4.1] |
---|
149 | template <typename U> |
---|
150 | fast_pool_allocator( |
---|
151 | const fast_pool_allocator<U, UserAllocator, Mutex, NextSize> &) |
---|
152 | { } |
---|
153 | |
---|
154 | // default destructor |
---|
155 | |
---|
156 | static pointer address(reference r) |
---|
157 | { return &r; } |
---|
158 | static const_pointer address(const_reference s) |
---|
159 | { return &s; } |
---|
160 | static size_type max_size() |
---|
161 | { return (std::numeric_limits<size_type>::max)(); } |
---|
162 | void construct(const pointer ptr, const value_type & t) |
---|
163 | { new (ptr) T(t); } |
---|
164 | void destroy(const pointer ptr) |
---|
165 | { |
---|
166 | ptr->~T(); |
---|
167 | (void) ptr; // avoid unused variable warning |
---|
168 | } |
---|
169 | |
---|
170 | bool operator==(const fast_pool_allocator &) const |
---|
171 | { return true; } |
---|
172 | bool operator!=(const fast_pool_allocator &) const |
---|
173 | { return false; } |
---|
174 | |
---|
175 | static pointer allocate(const size_type n) |
---|
176 | { |
---|
177 | const pointer ret = (n == 1) ? |
---|
178 | static_cast<pointer>( |
---|
179 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
180 | UserAllocator, Mutex, NextSize>::malloc() ) : |
---|
181 | static_cast<pointer>( |
---|
182 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
183 | UserAllocator, Mutex, NextSize>::ordered_malloc(n) ); |
---|
184 | if (ret == 0) |
---|
185 | throw std::bad_alloc(); |
---|
186 | return ret; |
---|
187 | } |
---|
188 | static pointer allocate(const size_type n, const void * const) |
---|
189 | { return allocate(n); } |
---|
190 | static pointer allocate() |
---|
191 | { |
---|
192 | const pointer ret = static_cast<pointer>( |
---|
193 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
194 | UserAllocator, Mutex, NextSize>::malloc() ); |
---|
195 | if (ret == 0) |
---|
196 | throw std::bad_alloc(); |
---|
197 | return ret; |
---|
198 | } |
---|
199 | static void deallocate(const pointer ptr, const size_type n) |
---|
200 | { |
---|
201 | #ifdef BOOST_NO_PROPER_STL_DEALLOCATE |
---|
202 | if (ptr == 0 || n == 0) |
---|
203 | return; |
---|
204 | #endif |
---|
205 | if (n == 1) |
---|
206 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
207 | UserAllocator, Mutex, NextSize>::free(ptr); |
---|
208 | else |
---|
209 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
210 | UserAllocator, Mutex, NextSize>::free(ptr, n); |
---|
211 | } |
---|
212 | static void deallocate(const pointer ptr) |
---|
213 | { |
---|
214 | singleton_pool<fast_pool_allocator_tag, sizeof(T), |
---|
215 | UserAllocator, Mutex, NextSize>::free(ptr); |
---|
216 | } |
---|
217 | }; |
---|
218 | |
---|
219 | } // namespace boost |
---|
220 | |
---|
221 | #endif |
---|