handler_alloc_helpers.hpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // detail/handler_alloc_helpers.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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/memory.hpp>
  17. #include <boost/asio/detail/recycling_allocator.hpp>
  18. #include <boost/asio/associated_allocator.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. #define BOOST_ASIO_DEFINE_TAGGED_HANDLER_PTR(purpose, op) \
  21. struct ptr \
  22. { \
  23. Handler* h; \
  24. op* v; \
  25. op* p; \
  26. ~ptr() \
  27. { \
  28. reset(); \
  29. } \
  30. static op* allocate(Handler& handler) \
  31. { \
  32. typedef typename ::boost::asio::associated_allocator< \
  33. Handler>::type associated_allocator_type; \
  34. typedef typename ::boost::asio::detail::get_recycling_allocator< \
  35. associated_allocator_type, purpose>::type default_allocator_type; \
  36. BOOST_ASIO_REBIND_ALLOC(default_allocator_type, op) a( \
  37. ::boost::asio::detail::get_recycling_allocator< \
  38. associated_allocator_type, purpose>::get( \
  39. ::boost::asio::get_associated_allocator(handler))); \
  40. return a.allocate(1); \
  41. } \
  42. void reset() \
  43. { \
  44. if (p) \
  45. { \
  46. p->~op(); \
  47. p = 0; \
  48. } \
  49. if (v) \
  50. { \
  51. typedef typename ::boost::asio::associated_allocator< \
  52. Handler>::type associated_allocator_type; \
  53. typedef typename ::boost::asio::detail::get_recycling_allocator< \
  54. associated_allocator_type, purpose>::type default_allocator_type; \
  55. BOOST_ASIO_REBIND_ALLOC(default_allocator_type, op) a( \
  56. ::boost::asio::detail::get_recycling_allocator< \
  57. associated_allocator_type, purpose>::get( \
  58. ::boost::asio::get_associated_allocator(*h))); \
  59. a.deallocate(static_cast<op*>(v), 1); \
  60. v = 0; \
  61. } \
  62. } \
  63. } \
  64. /**/
  65. #define BOOST_ASIO_DEFINE_HANDLER_PTR(op) \
  66. BOOST_ASIO_DEFINE_TAGGED_HANDLER_PTR( \
  67. ::boost::asio::detail::thread_info_base::default_tag, op ) \
  68. /**/
  69. #define BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(purpose, op) \
  70. struct ptr \
  71. { \
  72. const Alloc* a; \
  73. void* v; \
  74. op* p; \
  75. ~ptr() \
  76. { \
  77. reset(); \
  78. } \
  79. static op* allocate(const Alloc& a) \
  80. { \
  81. typedef typename ::boost::asio::detail::get_recycling_allocator< \
  82. Alloc, purpose>::type recycling_allocator_type; \
  83. BOOST_ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
  84. ::boost::asio::detail::get_recycling_allocator< \
  85. Alloc, purpose>::get(a)); \
  86. return a1.allocate(1); \
  87. } \
  88. void reset() \
  89. { \
  90. if (p) \
  91. { \
  92. p->~op(); \
  93. p = 0; \
  94. } \
  95. if (v) \
  96. { \
  97. typedef typename ::boost::asio::detail::get_recycling_allocator< \
  98. Alloc, purpose>::type recycling_allocator_type; \
  99. BOOST_ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
  100. ::boost::asio::detail::get_recycling_allocator< \
  101. Alloc, purpose>::get(*a)); \
  102. a1.deallocate(static_cast<op*>(v), 1); \
  103. v = 0; \
  104. } \
  105. } \
  106. } \
  107. /**/
  108. #define BOOST_ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(op) \
  109. BOOST_ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR( \
  110. ::boost::asio::detail::thread_info_base::default_tag, op ) \
  111. /**/
  112. #include <boost/asio/detail/pop_options.hpp>
  113. #endif // BOOST_ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP