handler_alloc_helpers.hpp 2.1 KB

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