1 | // |
2 | // detail/handler_alloc_helpers.hpp |
3 | // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
4 | // |
5 | // Copyright (c) 2003-2015 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 BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP |
12 | #define BOOST_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 <boost/asio/detail/config.hpp> |
19 | #include <boost/asio/detail/addressof.hpp> |
20 | #include <boost/asio/detail/noncopyable.hpp> |
21 | #include <boost/asio/handler_alloc_hook.hpp> |
22 | |
23 | #include <boost/asio/detail/push_options.hpp> |
24 | |
25 | // Calls to asio_handler_allocate and asio_handler_deallocate must be made from |
26 | // a namespace that does not contain any overloads of these functions. The |
27 | // boost_asio_handler_alloc_helpers namespace is defined here for that purpose. |
28 | namespace boost_asio_handler_alloc_helpers { |
29 | |
30 | template <typename Handler> |
31 | inline void* allocate(std::size_t s, Handler& h) |
32 | { |
33 | #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS) |
34 | return ::operator new(s); |
35 | #else |
36 | using boost::asio::asio_handler_allocate; |
37 | return asio_handler_allocate(s, boost::asio::detail::addressof(h)); |
38 | #endif |
39 | } |
40 | |
41 | template <typename Handler> |
42 | inline void deallocate(void* p, std::size_t s, Handler& h) |
43 | { |
44 | #if !defined(BOOST_ASIO_HAS_HANDLER_HOOKS) |
45 | ::operator delete(p); |
46 | #else |
47 | using boost::asio::asio_handler_deallocate; |
48 | asio_handler_deallocate(p, s, boost::asio::detail::addressof(h)); |
49 | #endif |
50 | } |
51 | |
52 | } // namespace boost_asio_handler_alloc_helpers |
53 | |
54 | #define BOOST_ASIO_DEFINE_HANDLER_PTR(op) \ |
55 | struct ptr \ |
56 | { \ |
57 | Handler* h; \ |
58 | void* v; \ |
59 | op* p; \ |
60 | ~ptr() \ |
61 | { \ |
62 | reset(); \ |
63 | } \ |
64 | void reset() \ |
65 | { \ |
66 | if (p) \ |
67 | { \ |
68 | p->~op(); \ |
69 | p = 0; \ |
70 | } \ |
71 | if (v) \ |
72 | { \ |
73 | boost_asio_handler_alloc_helpers::deallocate(v, sizeof(op), *h); \ |
74 | v = 0; \ |
75 | } \ |
76 | } \ |
77 | } \ |
78 | /**/ |
79 | |
80 | #include <boost/asio/detail/pop_options.hpp> |
81 | |
82 | #endif // BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP |
83 | |