thread_info_base.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // detail/thread_info_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2020 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_THREAD_INFO_BASE_HPP
  11. #define BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include <climits>
  16. #include <cstddef>
  17. #include <boost/asio/detail/noncopyable.hpp>
  18. #include <boost/asio/detail/push_options.hpp>
  19. namespace boost {
  20. namespace asio {
  21. namespace detail {
  22. class thread_info_base
  23. : private noncopyable
  24. {
  25. public:
  26. struct default_tag
  27. {
  28. enum { mem_index = 0 };
  29. };
  30. struct awaitable_frame_tag
  31. {
  32. enum { mem_index = 1 };
  33. };
  34. struct executor_function_tag
  35. {
  36. enum { mem_index = 2 };
  37. };
  38. thread_info_base()
  39. {
  40. for (int i = 0; i < max_mem_index; ++i)
  41. reusable_memory_[i] = 0;
  42. }
  43. ~thread_info_base()
  44. {
  45. for (int i = 0; i < max_mem_index; ++i)
  46. ::operator delete(reusable_memory_[i]);
  47. }
  48. static void* allocate(thread_info_base* this_thread, std::size_t size)
  49. {
  50. return allocate(default_tag(), this_thread, size);
  51. }
  52. static void deallocate(thread_info_base* this_thread,
  53. void* pointer, std::size_t size)
  54. {
  55. deallocate(default_tag(), this_thread, pointer, size);
  56. }
  57. template <typename Purpose>
  58. static void* allocate(Purpose, thread_info_base* this_thread,
  59. std::size_t size)
  60. {
  61. std::size_t chunks = (size + chunk_size - 1) / chunk_size;
  62. if (this_thread && this_thread->reusable_memory_[Purpose::mem_index])
  63. {
  64. void* const pointer = this_thread->reusable_memory_[Purpose::mem_index];
  65. this_thread->reusable_memory_[Purpose::mem_index] = 0;
  66. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  67. if (static_cast<std::size_t>(mem[0]) >= chunks)
  68. {
  69. mem[size] = mem[0];
  70. return pointer;
  71. }
  72. ::operator delete(pointer);
  73. }
  74. void* const pointer = ::operator new(chunks * chunk_size + 1);
  75. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  76. mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
  77. return pointer;
  78. }
  79. template <typename Purpose>
  80. static void deallocate(Purpose, thread_info_base* this_thread,
  81. void* pointer, std::size_t size)
  82. {
  83. if (size <= chunk_size * UCHAR_MAX)
  84. {
  85. if (this_thread && this_thread->reusable_memory_[Purpose::mem_index] == 0)
  86. {
  87. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  88. mem[0] = mem[size];
  89. this_thread->reusable_memory_[Purpose::mem_index] = pointer;
  90. return;
  91. }
  92. }
  93. ::operator delete(pointer);
  94. }
  95. private:
  96. enum { chunk_size = 4 };
  97. enum { max_mem_index = 3 };
  98. void* reusable_memory_[max_mem_index];
  99. };
  100. } // namespace detail
  101. } // namespace asio
  102. } // namespace boost
  103. #include <boost/asio/detail/pop_options.hpp>
  104. #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP