cancellation_signal.ipp 2.6 KB

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