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 ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
12 #define ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
13 
14 
15 #include "asio/detail/config.hpp"
16 #include "asio/detail/addressof.hpp"
17 #include "asio/detail/noncopyable.hpp"
18 #include "asio/handler_alloc_hook.hpp"
19 
20 #include "asio/detail/push_options.hpp"
21 
22 // Calls to asio_handler_allocate and asio_handler_deallocate must be made from
23 // a namespace that does not contain any overloads of these functions. The
24 // asio_handler_alloc_helpers namespace is defined here for that purpose.
25 namespace asio_handler_alloc_helpers {
26 
27 template <typename Handler>
allocate(std::size_t s,Handler & h)28 inline void* allocate(std::size_t s, Handler& h)
29 {
30 #if !defined(ASIO_HAS_HANDLER_HOOKS)
31   return ::operator new(s);
32 #else
33   using asio::asio_handler_allocate;
34   return asio_handler_allocate(s, asio::detail::addressof(h));
35 #endif
36 }
37 
38 template <typename Handler>
deallocate(void * p,std::size_t s,Handler & h)39 inline void deallocate(void* p, std::size_t s, Handler& h)
40 {
41 #if !defined(ASIO_HAS_HANDLER_HOOKS)
42   ::operator delete(p);
43 #else
44   using asio::asio_handler_deallocate;
45   asio_handler_deallocate(p, s, asio::detail::addressof(h));
46 #endif
47 }
48 
49 } // namespace asio_handler_alloc_helpers
50 
51 #define ASIO_DEFINE_HANDLER_PTR(op)    struct ptr    {      Handler* h;      void* v;      op* p;      ~ptr()      {        reset();      }      void reset()      {        if (p)        {          p->~op();          p = 0;        }        if (v)        {          asio_handler_alloc_helpers::deallocate(v, sizeof(op), *h);          v = 0;        }      }    }    /**/
52 
53 #include "asio/detail/pop_options.hpp"
54 
55 #endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
56