2
// handler_alloc_helpers.hpp
3
// ~~~~~~~~~~~~~~~~~~~~~~~~~
5
// Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com)
7
// Distributed under the Boost Software License, Version 1.0. (See accompanying
8
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
11
#ifndef ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
12
#define ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
14
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18
#include "asio/detail/push_options.hpp"
20
#include "asio/detail/push_options.hpp"
21
#include <boost/detail/workaround.hpp>
22
#include "asio/detail/pop_options.hpp"
24
#include "asio/handler_alloc_hook.hpp"
25
#include "asio/detail/noncopyable.hpp"
27
// Calls to asio_handler_allocate and asio_handler_deallocate must be made from
28
// a namespace that does not contain any overloads of these functions. The
29
// asio_handler_alloc_helpers namespace is defined here for that purpose.
30
namespace asio_handler_alloc_helpers {
32
template <typename Handler>
33
inline void* allocate(std::size_t s, Handler* h)
35
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
36
return ::operator new(s);
39
return asio_handler_allocate(s, h);
43
template <typename Handler>
44
inline void deallocate(void* p, std::size_t s, Handler* h)
46
#if BOOST_WORKAROUND(__BORLANDC__, BOOST_TESTED_AT(0x564))
50
asio_handler_deallocate(p, s, h);
54
} // namespace asio_handler_alloc_helpers
59
// Traits for handler allocation.
60
template <typename Handler, typename Object>
61
struct handler_alloc_traits
63
typedef Handler handler_type;
64
typedef Object value_type;
65
typedef Object* pointer_type;
66
BOOST_STATIC_CONSTANT(std::size_t, value_size = sizeof(Object));
69
template <typename Alloc_Traits>
72
// Helper class to provide RAII on uninitialised handler memory.
73
template <typename Alloc_Traits>
78
typedef typename Alloc_Traits::handler_type handler_type;
79
typedef typename Alloc_Traits::value_type value_type;
80
typedef typename Alloc_Traits::pointer_type pointer_type;
81
BOOST_STATIC_CONSTANT(std::size_t, value_size = Alloc_Traits::value_size);
83
// Constructor allocates the memory.
84
raw_handler_ptr(handler_type& handler)
86
pointer_(static_cast<pointer_type>(
87
asio_handler_alloc_helpers::allocate(value_size, &handler_)))
91
// Destructor automatically deallocates memory, unless it has been stolen by
92
// a handler_ptr object.
96
asio_handler_alloc_helpers::deallocate(
97
pointer_, value_size, &handler_);
101
friend class handler_ptr<Alloc_Traits>;
102
handler_type& handler_;
103
pointer_type pointer_;
106
// Helper class to provide RAII on uninitialised handler memory.
107
template <typename Alloc_Traits>
109
: private noncopyable
112
typedef typename Alloc_Traits::handler_type handler_type;
113
typedef typename Alloc_Traits::value_type value_type;
114
typedef typename Alloc_Traits::pointer_type pointer_type;
115
BOOST_STATIC_CONSTANT(std::size_t, value_size = Alloc_Traits::value_size);
116
typedef raw_handler_ptr<Alloc_Traits> raw_ptr_type;
118
// Take ownership of existing memory.
119
handler_ptr(handler_type& handler, pointer_type pointer)
125
// Construct object in raw memory and take ownership if construction succeeds.
126
handler_ptr(raw_ptr_type& raw_ptr)
127
: handler_(raw_ptr.handler_),
128
pointer_(new (raw_ptr.pointer_) value_type)
130
raw_ptr.pointer_ = 0;
133
// Construct object in raw memory and take ownership if construction succeeds.
134
template <typename Arg1>
135
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1)
136
: handler_(raw_ptr.handler_),
137
pointer_(new (raw_ptr.pointer_) value_type(a1))
139
raw_ptr.pointer_ = 0;
142
// Construct object in raw memory and take ownership if construction succeeds.
143
template <typename Arg1, typename Arg2>
144
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2)
145
: handler_(raw_ptr.handler_),
146
pointer_(new (raw_ptr.pointer_) value_type(a1, a2))
148
raw_ptr.pointer_ = 0;
151
// Construct object in raw memory and take ownership if construction succeeds.
152
template <typename Arg1, typename Arg2, typename Arg3>
153
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3)
154
: handler_(raw_ptr.handler_),
155
pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3))
157
raw_ptr.pointer_ = 0;
160
// Construct object in raw memory and take ownership if construction succeeds.
161
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4>
162
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4)
163
: handler_(raw_ptr.handler_),
164
pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4))
166
raw_ptr.pointer_ = 0;
169
// Construct object in raw memory and take ownership if construction succeeds.
170
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
172
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
174
: handler_(raw_ptr.handler_),
175
pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5))
177
raw_ptr.pointer_ = 0;
180
// Construct object in raw memory and take ownership if construction succeeds.
181
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
182
typename Arg5, typename Arg6>
183
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
185
: handler_(raw_ptr.handler_),
186
pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5, a6))
188
raw_ptr.pointer_ = 0;
191
// Construct object in raw memory and take ownership if construction succeeds.
192
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
193
typename Arg5, typename Arg6, typename Arg7>
194
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
195
Arg5& a5, Arg6& a6, Arg7& a7)
196
: handler_(raw_ptr.handler_),
197
pointer_(new (raw_ptr.pointer_) value_type(a1, a2, a3, a4, a5, a6, a7))
199
raw_ptr.pointer_ = 0;
202
// Construct object in raw memory and take ownership if construction succeeds.
203
template <typename Arg1, typename Arg2, typename Arg3, typename Arg4,
204
typename Arg5, typename Arg6, typename Arg7, typename Arg8>
205
handler_ptr(raw_ptr_type& raw_ptr, Arg1& a1, Arg2& a2, Arg3& a3, Arg4& a4,
206
Arg5& a5, Arg6& a6, Arg7& a7, Arg8& a8)
207
: handler_(raw_ptr.handler_),
208
pointer_(new (raw_ptr.pointer_) value_type(
209
a1, a2, a3, a4, a5, a6, a7, a8))
211
raw_ptr.pointer_ = 0;
214
// Destructor automatically deallocates memory, unless it has been released.
221
pointer_type get() const
226
// Release ownership of the memory.
227
pointer_type release()
229
pointer_type tmp = pointer_;
234
// Explicitly destroy and deallocate the memory.
239
pointer_->value_type::~value_type();
240
asio_handler_alloc_helpers::deallocate(
241
pointer_, value_size, &handler_);
247
handler_type& handler_;
248
pointer_type pointer_;
251
} // namespace detail
254
#include "asio/detail/pop_options.hpp"
256
#endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP