]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Distributed under the Boost Software License, Version 1.0. (See |
2 | // accompanying file LICENSE_1_0.txt or copy at | |
3 | // http://www.boost.org/LICENSE_1_0.txt) | |
4 | // (C) Copyright 2007 Anthony Williams | |
5 | #ifndef THREAD_HEAP_ALLOC_HPP | |
6 | #define THREAD_HEAP_ALLOC_HPP | |
7 | #include <new> | |
8 | #include <boost/thread/detail/config.hpp> | |
9 | #include <boost/thread/win32/thread_primitives.hpp> | |
10 | #include <stdexcept> | |
11 | #include <boost/assert.hpp> | |
12 | #include <boost/throw_exception.hpp> | |
13 | #include <boost/core/no_exceptions_support.hpp> | |
14 | ||
15 | #if defined( BOOST_USE_WINDOWS_H ) | |
16 | # include <windows.h> | |
17 | ||
18 | namespace boost | |
19 | { | |
20 | namespace detail | |
21 | { | |
22 | namespace win32 | |
23 | { | |
24 | using ::GetProcessHeap; | |
25 | using ::HeapAlloc; | |
26 | using ::HeapFree; | |
27 | } | |
28 | } | |
29 | } | |
30 | ||
31 | #else | |
32 | ||
33 | # ifdef HeapAlloc | |
34 | # undef HeapAlloc | |
35 | # endif | |
36 | ||
37 | namespace boost | |
38 | { | |
39 | namespace detail | |
40 | { | |
41 | namespace win32 | |
42 | { | |
43 | extern "C" | |
44 | { | |
45 | __declspec(dllimport) handle __stdcall GetProcessHeap(); | |
46 | __declspec(dllimport) void* __stdcall HeapAlloc(handle,unsigned long,ulong_ptr); | |
47 | __declspec(dllimport) int __stdcall HeapFree(handle,unsigned long,void*); | |
48 | } | |
49 | } | |
50 | } | |
51 | } | |
52 | ||
53 | #endif | |
54 | ||
55 | #include <boost/config/abi_prefix.hpp> | |
56 | ||
57 | namespace boost | |
58 | { | |
59 | namespace detail | |
60 | { | |
61 | inline void* allocate_raw_heap_memory(unsigned size) | |
62 | { | |
63 | void* const heap_memory=detail::win32::HeapAlloc(detail::win32::GetProcessHeap(),0,size); | |
64 | if(!heap_memory) | |
65 | { | |
66 | boost::throw_exception(std::bad_alloc()); | |
67 | } | |
68 | return heap_memory; | |
69 | } | |
70 | ||
71 | inline void free_raw_heap_memory(void* heap_memory) | |
72 | { | |
73 | BOOST_VERIFY(detail::win32::HeapFree(detail::win32::GetProcessHeap(),0,heap_memory)!=0); | |
74 | } | |
75 | ||
76 | template<typename T> | |
77 | inline T* heap_new() | |
78 | { | |
79 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
80 | BOOST_TRY | |
81 | { | |
82 | T* const data=new (heap_memory) T(); | |
83 | return data; | |
84 | } | |
85 | BOOST_CATCH(...) | |
86 | { | |
87 | free_raw_heap_memory(heap_memory); | |
88 | BOOST_RETHROW | |
89 | } | |
90 | BOOST_CATCH_END | |
91 | } | |
92 | ||
93 | #ifndef BOOST_NO_CXX11_RVALUE_REFERENCES | |
94 | template<typename T,typename A1> | |
95 | inline T* heap_new(A1&& a1) | |
96 | { | |
97 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
98 | BOOST_TRY | |
99 | { | |
100 | T* const data=new (heap_memory) T(static_cast<A1&&>(a1)); | |
101 | return data; | |
102 | } | |
103 | BOOST_CATCH(...) | |
104 | { | |
105 | free_raw_heap_memory(heap_memory); | |
106 | BOOST_RETHROW | |
107 | } | |
108 | BOOST_CATCH_END | |
109 | } | |
110 | template<typename T,typename A1,typename A2> | |
111 | inline T* heap_new(A1&& a1,A2&& a2) | |
112 | { | |
113 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
114 | BOOST_TRY | |
115 | { | |
116 | T* const data=new (heap_memory) T(static_cast<A1&&>(a1),static_cast<A2&&>(a2)); | |
117 | return data; | |
118 | } | |
119 | BOOST_CATCH(...) | |
120 | { | |
121 | free_raw_heap_memory(heap_memory); | |
122 | BOOST_RETHROW | |
123 | } | |
124 | BOOST_CATCH_END | |
125 | } | |
126 | template<typename T,typename A1,typename A2,typename A3> | |
127 | inline T* heap_new(A1&& a1,A2&& a2,A3&& a3) | |
128 | { | |
129 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
130 | BOOST_TRY | |
131 | { | |
132 | T* const data=new (heap_memory) T(static_cast<A1&&>(a1),static_cast<A2&&>(a2), | |
133 | static_cast<A3&&>(a3)); | |
134 | return data; | |
135 | } | |
136 | BOOST_CATCH(...) | |
137 | { | |
138 | free_raw_heap_memory(heap_memory); | |
139 | BOOST_RETHROW | |
140 | } | |
141 | BOOST_CATCH_END | |
142 | } | |
143 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
144 | inline T* heap_new(A1&& a1,A2&& a2,A3&& a3,A4&& a4) | |
145 | { | |
146 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
147 | BOOST_TRY | |
148 | { | |
149 | T* const data=new (heap_memory) T(static_cast<A1&&>(a1),static_cast<A2&&>(a2), | |
150 | static_cast<A3&&>(a3),static_cast<A4&&>(a4)); | |
151 | return data; | |
152 | } | |
153 | BOOST_CATCH(...) | |
154 | { | |
155 | free_raw_heap_memory(heap_memory); | |
156 | BOOST_RETHROW | |
157 | } | |
158 | BOOST_CATCH_END | |
159 | } | |
160 | #else | |
161 | template<typename T,typename A1> | |
162 | inline T* heap_new_impl(A1 a1) | |
163 | { | |
164 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
165 | BOOST_TRY | |
166 | { | |
167 | T* const data=new (heap_memory) T(a1); | |
168 | return data; | |
169 | } | |
170 | BOOST_CATCH(...) | |
171 | { | |
172 | free_raw_heap_memory(heap_memory); | |
173 | BOOST_RETHROW | |
174 | } | |
175 | BOOST_CATCH_END | |
176 | } | |
177 | ||
178 | template<typename T,typename A1,typename A2> | |
179 | inline T* heap_new_impl(A1 a1,A2 a2) | |
180 | { | |
181 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
182 | BOOST_TRY | |
183 | { | |
184 | T* const data=new (heap_memory) T(a1,a2); | |
185 | return data; | |
186 | } | |
187 | BOOST_CATCH(...) | |
188 | { | |
189 | free_raw_heap_memory(heap_memory); | |
190 | BOOST_RETHROW | |
191 | } | |
192 | BOOST_CATCH_END | |
193 | } | |
194 | ||
195 | template<typename T,typename A1,typename A2,typename A3> | |
196 | inline T* heap_new_impl(A1 a1,A2 a2,A3 a3) | |
197 | { | |
198 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
199 | BOOST_TRY | |
200 | { | |
201 | T* const data=new (heap_memory) T(a1,a2,a3); | |
202 | return data; | |
203 | } | |
204 | BOOST_CATCH(...) | |
205 | { | |
206 | free_raw_heap_memory(heap_memory); | |
207 | BOOST_RETHROW | |
208 | } | |
209 | BOOST_CATCH_END | |
210 | } | |
211 | ||
212 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
213 | inline T* heap_new_impl(A1 a1,A2 a2,A3 a3,A4 a4) | |
214 | { | |
215 | void* const heap_memory=allocate_raw_heap_memory(sizeof(T)); | |
216 | BOOST_TRY | |
217 | { | |
218 | T* const data=new (heap_memory) T(a1,a2,a3,a4); | |
219 | return data; | |
220 | } | |
221 | BOOST_CATCH(...) | |
222 | { | |
223 | free_raw_heap_memory(heap_memory); | |
224 | BOOST_RETHROW | |
225 | } | |
226 | BOOST_CATCH_END | |
227 | } | |
228 | ||
229 | ||
230 | template<typename T,typename A1> | |
231 | inline T* heap_new(A1 const& a1) | |
232 | { | |
233 | return heap_new_impl<T,A1 const&>(a1); | |
234 | } | |
235 | template<typename T,typename A1> | |
236 | inline T* heap_new(A1& a1) | |
237 | { | |
238 | return heap_new_impl<T,A1&>(a1); | |
239 | } | |
240 | ||
241 | template<typename T,typename A1,typename A2> | |
242 | inline T* heap_new(A1 const& a1,A2 const& a2) | |
243 | { | |
244 | return heap_new_impl<T,A1 const&,A2 const&>(a1,a2); | |
245 | } | |
246 | template<typename T,typename A1,typename A2> | |
247 | inline T* heap_new(A1& a1,A2 const& a2) | |
248 | { | |
249 | return heap_new_impl<T,A1&,A2 const&>(a1,a2); | |
250 | } | |
251 | template<typename T,typename A1,typename A2> | |
252 | inline T* heap_new(A1 const& a1,A2& a2) | |
253 | { | |
254 | return heap_new_impl<T,A1 const&,A2&>(a1,a2); | |
255 | } | |
256 | template<typename T,typename A1,typename A2> | |
257 | inline T* heap_new(A1& a1,A2& a2) | |
258 | { | |
259 | return heap_new_impl<T,A1&,A2&>(a1,a2); | |
260 | } | |
261 | ||
262 | template<typename T,typename A1,typename A2,typename A3> | |
263 | inline T* heap_new(A1 const& a1,A2 const& a2,A3 const& a3) | |
264 | { | |
265 | return heap_new_impl<T,A1 const&,A2 const&,A3 const&>(a1,a2,a3); | |
266 | } | |
267 | template<typename T,typename A1,typename A2,typename A3> | |
268 | inline T* heap_new(A1& a1,A2 const& a2,A3 const& a3) | |
269 | { | |
270 | return heap_new_impl<T,A1&,A2 const&,A3 const&>(a1,a2,a3); | |
271 | } | |
272 | template<typename T,typename A1,typename A2,typename A3> | |
273 | inline T* heap_new(A1 const& a1,A2& a2,A3 const& a3) | |
274 | { | |
275 | return heap_new_impl<T,A1 const&,A2&,A3 const&>(a1,a2,a3); | |
276 | } | |
277 | template<typename T,typename A1,typename A2,typename A3> | |
278 | inline T* heap_new(A1& a1,A2& a2,A3 const& a3) | |
279 | { | |
280 | return heap_new_impl<T,A1&,A2&,A3 const&>(a1,a2,a3); | |
281 | } | |
282 | ||
283 | template<typename T,typename A1,typename A2,typename A3> | |
284 | inline T* heap_new(A1 const& a1,A2 const& a2,A3& a3) | |
285 | { | |
286 | return heap_new_impl<T,A1 const&,A2 const&,A3&>(a1,a2,a3); | |
287 | } | |
288 | template<typename T,typename A1,typename A2,typename A3> | |
289 | inline T* heap_new(A1& a1,A2 const& a2,A3& a3) | |
290 | { | |
291 | return heap_new_impl<T,A1&,A2 const&,A3&>(a1,a2,a3); | |
292 | } | |
293 | template<typename T,typename A1,typename A2,typename A3> | |
294 | inline T* heap_new(A1 const& a1,A2& a2,A3& a3) | |
295 | { | |
296 | return heap_new_impl<T,A1 const&,A2&,A3&>(a1,a2,a3); | |
297 | } | |
298 | template<typename T,typename A1,typename A2,typename A3> | |
299 | inline T* heap_new(A1& a1,A2& a2,A3& a3) | |
300 | { | |
301 | return heap_new_impl<T,A1&,A2&,A3&>(a1,a2,a3); | |
302 | } | |
303 | ||
304 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
305 | inline T* heap_new(A1 const& a1,A2 const& a2,A3 const& a3,A4 const& a4) | |
306 | { | |
307 | return heap_new_impl<T,A1 const&,A2 const&,A3 const&,A4 const&>(a1,a2,a3,a4); | |
308 | } | |
309 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
310 | inline T* heap_new(A1& a1,A2 const& a2,A3 const& a3,A4 const& a4) | |
311 | { | |
312 | return heap_new_impl<T,A1&,A2 const&,A3 const&,A4 const&>(a1,a2,a3,a4); | |
313 | } | |
314 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
315 | inline T* heap_new(A1 const& a1,A2& a2,A3 const& a3,A4 const& a4) | |
316 | { | |
317 | return heap_new_impl<T,A1 const&,A2&,A3 const&,A4 const&>(a1,a2,a3,a4); | |
318 | } | |
319 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
320 | inline T* heap_new(A1& a1,A2& a2,A3 const& a3,A4 const& a4) | |
321 | { | |
322 | return heap_new_impl<T,A1&,A2&,A3 const&,A4 const&>(a1,a2,a3,a4); | |
323 | } | |
324 | ||
325 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
326 | inline T* heap_new(A1 const& a1,A2 const& a2,A3& a3,A4 const& a4) | |
327 | { | |
328 | return heap_new_impl<T,A1 const&,A2 const&,A3&,A4 const&>(a1,a2,a3,a4); | |
329 | } | |
330 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
331 | inline T* heap_new(A1& a1,A2 const& a2,A3& a3,A4 const& a4) | |
332 | { | |
333 | return heap_new_impl<T,A1&,A2 const&,A3&,A4 const&>(a1,a2,a3,a4); | |
334 | } | |
335 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
336 | inline T* heap_new(A1 const& a1,A2& a2,A3& a3,A4 const& a4) | |
337 | { | |
338 | return heap_new_impl<T,A1 const&,A2&,A3&,A4 const&>(a1,a2,a3,a4); | |
339 | } | |
340 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
341 | inline T* heap_new(A1& a1,A2& a2,A3& a3,A4 const& a4) | |
342 | { | |
343 | return heap_new_impl<T,A1&,A2&,A3&,A4 const&>(a1,a2,a3,a4); | |
344 | } | |
345 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
346 | inline T* heap_new(A1 const& a1,A2 const& a2,A3 const& a3,A4& a4) | |
347 | { | |
348 | return heap_new_impl<T,A1 const&,A2 const&,A3 const&,A4&>(a1,a2,a3,a4); | |
349 | } | |
350 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
351 | inline T* heap_new(A1& a1,A2 const& a2,A3 const& a3,A4& a4) | |
352 | { | |
353 | return heap_new_impl<T,A1&,A2 const&,A3 const&,A4&>(a1,a2,a3,a4); | |
354 | } | |
355 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
356 | inline T* heap_new(A1 const& a1,A2& a2,A3 const& a3,A4& a4) | |
357 | { | |
358 | return heap_new_impl<T,A1 const&,A2&,A3 const&,A4&>(a1,a2,a3,a4); | |
359 | } | |
360 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
361 | inline T* heap_new(A1& a1,A2& a2,A3 const& a3,A4& a4) | |
362 | { | |
363 | return heap_new_impl<T,A1&,A2&,A3 const&,A4&>(a1,a2,a3,a4); | |
364 | } | |
365 | ||
366 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
367 | inline T* heap_new(A1 const& a1,A2 const& a2,A3& a3,A4& a4) | |
368 | { | |
369 | return heap_new_impl<T,A1 const&,A2 const&,A3&,A4&>(a1,a2,a3,a4); | |
370 | } | |
371 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
372 | inline T* heap_new(A1& a1,A2 const& a2,A3& a3,A4& a4) | |
373 | { | |
374 | return heap_new_impl<T,A1&,A2 const&,A3&,A4&>(a1,a2,a3,a4); | |
375 | } | |
376 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
377 | inline T* heap_new(A1 const& a1,A2& a2,A3& a3,A4& a4) | |
378 | { | |
379 | return heap_new_impl<T,A1 const&,A2&,A3&,A4&>(a1,a2,a3,a4); | |
380 | } | |
381 | template<typename T,typename A1,typename A2,typename A3,typename A4> | |
382 | inline T* heap_new(A1& a1,A2& a2,A3& a3,A4& a4) | |
383 | { | |
384 | return heap_new_impl<T,A1&,A2&,A3&,A4&>(a1,a2,a3,a4); | |
385 | } | |
386 | ||
387 | #endif | |
388 | template<typename T> | |
389 | inline void heap_delete(T* data) | |
390 | { | |
391 | data->~T(); | |
392 | free_raw_heap_memory(data); | |
393 | } | |
394 | ||
395 | template<typename T> | |
396 | struct do_heap_delete | |
397 | { | |
398 | void operator()(T* data) const | |
399 | { | |
400 | detail::heap_delete(data); | |
401 | } | |
402 | }; | |
403 | } | |
404 | } | |
405 | ||
406 | #include <boost/config/abi_suffix.hpp> | |
407 | ||
408 | ||
409 | #endif |