1 //
2 // detail/handler_alloc_helpers.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
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)
9 //
10
11 #ifndef ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
12 #define ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include "asio/detail/config.hpp"
19 #include "asio/detail/memory.hpp"
20 #include "asio/detail/noncopyable.hpp"
21 #include "asio/detail/recycling_allocator.hpp"
22 #include "asio/associated_allocator.hpp"
23 #include "asio/handler_alloc_hook.hpp"
24
25 #include "asio/detail/push_options.hpp"
26
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 {
31
32 template <typename Handler>
allocate(std::size_t s,Handler & h)33 inline void* allocate(std::size_t s, Handler& h)
34 {
35 #if !defined(ASIO_HAS_HANDLER_HOOKS)
36 return ::operator new(s);
37 #else
38 using asio::asio_handler_allocate;
39 return asio_handler_allocate(s, asio::detail::addressof(h));
40 #endif
41 }
42
43 template <typename Handler>
deallocate(void * p,std::size_t s,Handler & h)44 inline void deallocate(void* p, std::size_t s, Handler& h)
45 {
46 #if !defined(ASIO_HAS_HANDLER_HOOKS)
47 ::operator delete(p);
48 #else
49 using asio::asio_handler_deallocate;
50 asio_handler_deallocate(p, s, asio::detail::addressof(h));
51 #endif
52 }
53
54 } // namespace asio_handler_alloc_helpers
55
56 namespace asio {
57 namespace detail {
58
59 template <typename Handler, typename T>
60 class hook_allocator
61 {
62 public:
63 typedef T value_type;
64
65 template <typename U>
66 struct rebind
67 {
68 typedef hook_allocator<Handler, U> other;
69 };
70
hook_allocator(Handler & h)71 explicit hook_allocator(Handler& h)
72 : handler_(h)
73 {
74 }
75
76 template <typename U>
hook_allocator(const hook_allocator<Handler,U> & a)77 hook_allocator(const hook_allocator<Handler, U>& a)
78 : handler_(a.handler_)
79 {
80 }
81
allocate(std::size_t n)82 T* allocate(std::size_t n)
83 {
84 return static_cast<T*>(
85 asio_handler_alloc_helpers::allocate(sizeof(T) * n, handler_));
86 }
87
deallocate(T * p,std::size_t n)88 void deallocate(T* p, std::size_t n)
89 {
90 asio_handler_alloc_helpers::deallocate(p, sizeof(T) * n, handler_);
91 }
92
93 //private:
94 Handler& handler_;
95 };
96
97 template <typename Handler>
98 class hook_allocator<Handler, void>
99 {
100 public:
101 typedef void value_type;
102
103 template <typename U>
104 struct rebind
105 {
106 typedef hook_allocator<Handler, U> other;
107 };
108
hook_allocator(Handler & h)109 explicit hook_allocator(Handler& h)
110 : handler_(h)
111 {
112 }
113
114 template <typename U>
hook_allocator(const hook_allocator<Handler,U> & a)115 hook_allocator(const hook_allocator<Handler, U>& a)
116 : handler_(a.handler_)
117 {
118 }
119
120 //private:
121 Handler& handler_;
122 };
123
124 template <typename Handler, typename Allocator>
125 struct get_hook_allocator
126 {
127 typedef Allocator type;
128
getasio::detail::get_hook_allocator129 static type get(Handler&, const Allocator& a)
130 {
131 return a;
132 }
133 };
134
135 template <typename Handler, typename T>
136 struct get_hook_allocator<Handler, std::allocator<T> >
137 {
138 typedef hook_allocator<Handler, T> type;
139
getasio::detail::get_hook_allocator140 static type get(Handler& handler, const std::allocator<T>&)
141 {
142 return type(handler);
143 }
144 };
145
146 } // namespace detail
147 } // namespace asio
148
149 #define ASIO_DEFINE_HANDLER_PTR(op) \
150 struct ptr \
151 { \
152 Handler* h; \
153 op* v; \
154 op* p; \
155 ~ptr() \
156 { \
157 reset(); \
158 } \
159 static op* allocate(Handler& handler) \
160 { \
161 typedef typename ::asio::associated_allocator< \
162 Handler>::type associated_allocator_type; \
163 typedef typename ::asio::detail::get_hook_allocator< \
164 Handler, associated_allocator_type>::type hook_allocator_type; \
165 ASIO_REBIND_ALLOC(hook_allocator_type, op) a( \
166 ::asio::detail::get_hook_allocator< \
167 Handler, associated_allocator_type>::get( \
168 handler, ::asio::get_associated_allocator(handler))); \
169 return a.allocate(1); \
170 } \
171 void reset() \
172 { \
173 if (p) \
174 { \
175 p->~op(); \
176 p = 0; \
177 } \
178 if (v) \
179 { \
180 typedef typename ::asio::associated_allocator< \
181 Handler>::type associated_allocator_type; \
182 typedef typename ::asio::detail::get_hook_allocator< \
183 Handler, associated_allocator_type>::type hook_allocator_type; \
184 ASIO_REBIND_ALLOC(hook_allocator_type, op) a( \
185 ::asio::detail::get_hook_allocator< \
186 Handler, associated_allocator_type>::get( \
187 *h, ::asio::get_associated_allocator(*h))); \
188 a.deallocate(static_cast<op*>(v), 1); \
189 v = 0; \
190 } \
191 } \
192 } \
193 /**/
194
195 #define ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(purpose, op) \
196 struct ptr \
197 { \
198 const Alloc* a; \
199 void* v; \
200 op* p; \
201 ~ptr() \
202 { \
203 reset(); \
204 } \
205 static op* allocate(const Alloc& a) \
206 { \
207 typedef typename ::asio::detail::get_recycling_allocator< \
208 Alloc, purpose>::type recycling_allocator_type; \
209 ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
210 ::asio::detail::get_recycling_allocator< \
211 Alloc, purpose>::get(a)); \
212 return a1.allocate(1); \
213 } \
214 void reset() \
215 { \
216 if (p) \
217 { \
218 p->~op(); \
219 p = 0; \
220 } \
221 if (v) \
222 { \
223 typedef typename ::asio::detail::get_recycling_allocator< \
224 Alloc, purpose>::type recycling_allocator_type; \
225 ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
226 ::asio::detail::get_recycling_allocator< \
227 Alloc, purpose>::get(*a)); \
228 a1.deallocate(static_cast<op*>(v), 1); \
229 v = 0; \
230 } \
231 } \
232 } \
233 /**/
234
235 #define ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(op) \
236 ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR( \
237 ::asio::detail::thread_info_base::default_tag, op ) \
238 /**/
239
240 #include "asio/detail/pop_options.hpp"
241
242 #endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
243