cancellation_signal.ipp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // impl/cancellation_signal.ipp
  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_IMPL_CANCELLATION_SIGNAL_IPP
  11. #define BOOST_ASIO_IMPL_CANCELLATION_SIGNAL_IPP
  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/cancellation_signal.hpp>
  17. #include <boost/asio/detail/thread_context.hpp>
  18. #include <boost/asio/detail/thread_info_base.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. cancellation_signal::~cancellation_signal()
  23. {
  24. if (handler_)
  25. {
  26. std::pair<void*, std::size_t> mem = handler_->destroy();
  27. detail::thread_info_base::deallocate(
  28. detail::thread_info_base::cancellation_signal_tag(),
  29. detail::thread_context::top_of_thread_call_stack(),
  30. mem.first, mem.second);
  31. }
  32. }
  33. void cancellation_slot::clear()
  34. {
  35. if (handler_ != 0 && *handler_ != 0)
  36. {
  37. std::pair<void*, std::size_t> mem = (*handler_)->destroy();
  38. detail::thread_info_base::deallocate(
  39. detail::thread_info_base::cancellation_signal_tag(),
  40. detail::thread_context::top_of_thread_call_stack(),
  41. mem.first, mem.second);
  42. *handler_ = 0;
  43. }
  44. }
  45. std::pair<void*, std::size_t> cancellation_slot::prepare_memory(
  46. std::size_t size, std::size_t align)
  47. {
  48. assert(handler_);
  49. std::pair<void*, std::size_t> mem;
  50. if (*handler_)
  51. {
  52. mem = (*handler_)->destroy();
  53. *handler_ = 0;
  54. }
  55. if (size > mem.second
  56. || reinterpret_cast<std::size_t>(mem.first) % align != 0)
  57. {
  58. if (mem.first)
  59. {
  60. detail::thread_info_base::deallocate(
  61. detail::thread_info_base::cancellation_signal_tag(),
  62. detail::thread_context::top_of_thread_call_stack(),
  63. mem.first, mem.second);
  64. }
  65. mem.first = detail::thread_info_base::allocate(
  66. detail::thread_info_base::cancellation_signal_tag(),
  67. detail::thread_context::top_of_thread_call_stack(),
  68. size, align);
  69. mem.second = size;
  70. }
  71. return mem;
  72. }
  73. cancellation_slot::auto_delete_helper::~auto_delete_helper()
  74. {
  75. if (mem.first)
  76. {
  77. detail::thread_info_base::deallocate(
  78. detail::thread_info_base::cancellation_signal_tag(),
  79. detail::thread_context::top_of_thread_call_stack(),
  80. mem.first, mem.second);
  81. }
  82. }
  83. } // namespace asio
  84. } // namespace boost
  85. #include <boost/asio/detail/pop_options.hpp>
  86. #endif // BOOST_ASIO_IMPL_CANCELLATION_SIGNAL_IPP