2 // detail/handler_alloc_helpers.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2022 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 BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
12 #define BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/memory.hpp>
20 #include <boost/asio/detail/noncopyable.hpp>
21 #include <boost/asio/detail/recycling_allocator.hpp>
22 #include <boost/asio/detail/thread_info_base.hpp>
23 #include <boost/asio/associated_allocator.hpp>
24 #include <boost/asio/handler_alloc_hook.hpp>
26 #include <boost/asio/detail/push_options.hpp>
28 // Calls to asio_handler_allocate and asio_handler_deallocate must be made from
29 // a namespace that does not contain any overloads of these functions. The
30 // boost_asio_handler_alloc_helpers namespace is defined here for that purpose.
31 namespace boost_asio_handler_alloc_helpers {
33 #if defined(BOOST_ASIO_NO_DEPRECATED)
34 template <typename Handler>
35 inline void error_if_hooks_are_defined(Handler& h)
37 using boost::asio::asio_handler_allocate;
38 // If you get an error here it is because some of your handlers still
39 // overload asio_handler_allocate, but this hook is no longer used.
40 (void)static_cast<boost::asio::asio_handler_allocate_is_no_longer_used>(
41 asio_handler_allocate(static_cast<std::size_t>(0),
42 boost::asio::detail::addressof(h)));
44 using boost::asio::asio_handler_deallocate;
45 // If you get an error here it is because some of your handlers still
46 // overload asio_handler_deallocate, but this hook is no longer used.
47 (void)static_cast<boost::asio::asio_handler_deallocate_is_no_longer_used>(
48 asio_handler_deallocate(static_cast<void*>(0),
49 static_cast<std::size_t>(0), boost::asio::detail::addressof(h)));
51 #endif // defined(BOOST_ASIO_NO_DEPRECATED)
53 template <typename Handler>
54 inline void* allocate(std::size_t s, Handler& h,
55 std::size_t align = BOOST_ASIO_DEFAULT_ALIGN)
57 #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS)
58 return aligned_new(align, s);
59 #elif defined(BOOST_ASIO_NO_DEPRECATED)
60 // The asio_handler_allocate hook is no longer used to obtain memory.
61 (void)&error_if_hooks_are_defined<Handler>;
63 # if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
64 return boost::asio::detail::thread_info_base::allocate(
65 boost::asio::detail::thread_context::top_of_thread_call_stack(),
67 # else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
68 return aligned_new(align, s);
69 # endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
72 using boost::asio::asio_handler_allocate;
73 return asio_handler_allocate(s, boost::asio::detail::addressof(h));
77 template <typename Handler>
78 inline void deallocate(void* p, std::size_t s, Handler& h)
80 #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS)
82 #elif defined(BOOST_ASIO_NO_DEPRECATED)
83 // The asio_handler_allocate hook is no longer used to obtain memory.
84 (void)&error_if_hooks_are_defined<Handler>;
86 #if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
87 boost::asio::detail::thread_info_base::deallocate(
88 boost::asio::detail::thread_context::top_of_thread_call_stack(), p, s);
89 #else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
92 #endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
94 using boost::asio::asio_handler_deallocate;
95 asio_handler_deallocate(p, s, boost::asio::detail::addressof(h));
99 } // namespace boost_asio_handler_alloc_helpers
105 template <typename Handler, typename T>
109 typedef T value_type;
111 template <typename U>
114 typedef hook_allocator<Handler, U> other;
117 explicit hook_allocator(Handler& h)
122 template <typename U>
123 hook_allocator(const hook_allocator<Handler, U>& a)
124 : handler_(a.handler_)
128 T* allocate(std::size_t n)
130 return static_cast<T*>(
131 boost_asio_handler_alloc_helpers::allocate(
132 sizeof(T) * n, handler_, BOOST_ASIO_ALIGNOF(T)));
135 void deallocate(T* p, std::size_t n)
137 boost_asio_handler_alloc_helpers::deallocate(p, sizeof(T) * n, handler_);
144 template <typename Handler>
145 class hook_allocator<Handler, void>
148 typedef void value_type;
150 template <typename U>
153 typedef hook_allocator<Handler, U> other;
156 explicit hook_allocator(Handler& h)
161 template <typename U>
162 hook_allocator(const hook_allocator<Handler, U>& a)
163 : handler_(a.handler_)
171 template <typename Handler, typename Allocator>
172 struct get_hook_allocator
174 typedef Allocator type;
176 static type get(Handler&, const Allocator& a)
182 template <typename Handler, typename T>
183 struct get_hook_allocator<Handler, std::allocator<T> >
185 typedef hook_allocator<Handler, T> type;
187 static type get(Handler& handler, const std::allocator<T>&)
189 return type(handler);
193 } // namespace detail
197 #define BOOST_ASIO_DEFINE_HANDLER_PTR(op) \
207 static op* allocate(Handler& handler) \
209 typedef typename ::boost::asio::associated_allocator< \
210 Handler>::type associated_allocator_type; \
211 typedef typename ::boost::asio::detail::get_hook_allocator< \
212 Handler, associated_allocator_type>::type hook_allocator_type; \
213 BOOST_ASIO_REBIND_ALLOC(hook_allocator_type, op) a( \
214 ::boost::asio::detail::get_hook_allocator< \
215 Handler, associated_allocator_type>::get( \
216 handler, ::boost::asio::get_associated_allocator(handler))); \
217 return a.allocate(1); \
228 typedef typename ::boost::asio::associated_allocator< \
229 Handler>::type associated_allocator_type; \
230 typedef typename ::boost::asio::detail::get_hook_allocator< \
231 Handler, associated_allocator_type>::type hook_allocator_type; \
232 BOOST_ASIO_REBIND_ALLOC(hook_allocator_type, op) a( \
233 ::boost::asio::detail::get_hook_allocator< \
234 Handler, associated_allocator_type>::get( \
235 *h, ::boost::asio::get_associated_allocator(*h))); \
236 a.deallocate(static_cast<op*>(v), 1); \
243 #define BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(purpose, op) \
253 static op* allocate(const Alloc& a) \
255 typedef typename ::boost::asio::detail::get_recycling_allocator< \
256 Alloc, purpose>::type recycling_allocator_type; \
257 BOOST_ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
258 ::boost::asio::detail::get_recycling_allocator< \
259 Alloc, purpose>::get(a)); \
260 return a1.allocate(1); \
271 typedef typename ::boost::asio::detail::get_recycling_allocator< \
272 Alloc, purpose>::type recycling_allocator_type; \
273 BOOST_ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
274 ::boost::asio::detail::get_recycling_allocator< \
275 Alloc, purpose>::get(*a)); \
276 a1.deallocate(static_cast<op*>(v), 1); \
283 #define BOOST_ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(op) \
284 BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR( \
285 ::boost::asio::detail::thread_info_base::default_tag, op ) \
288 #include <boost/asio/detail/pop_options.hpp>
290 #endif // BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP