thread_info_base.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. //
  2. // detail/thread_info_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2019 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. if (reusable_memory_[i])
  47. ::operator delete(reusable_memory_[i]);
  48. }
  49. static void* allocate(thread_info_base* this_thread, std::size_t size)
  50. {
  51. return allocate(default_tag(), this_thread, size);
  52. }
  53. static void deallocate(thread_info_base* this_thread,
  54. void* pointer, std::size_t size)
  55. {
  56. deallocate(default_tag(), this_thread, pointer, size);
  57. }
  58. template <typename Purpose>
  59. static void* allocate(Purpose, thread_info_base* this_thread,
  60. std::size_t size)
  61. {
  62. std::size_t chunks = (size + chunk_size - 1) / chunk_size;
  63. if (this_thread && this_thread->reusable_memory_[Purpose::mem_index])
  64. {
  65. void* const pointer = this_thread->reusable_memory_[Purpose::mem_index];
  66. this_thread->reusable_memory_[Purpose::mem_index] = 0;
  67. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  68. if (static_cast<std::size_t>(mem[0]) >= chunks)
  69. {
  70. mem[size] = mem[0];
  71. return pointer;
  72. }
  73. ::operator delete(pointer);
  74. }
  75. void* const pointer = ::operator new(chunks * chunk_size + 1);
  76. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  77. mem[size] = (chunks <= UCHAR_MAX) ? static_cast<unsigned char>(chunks) : 0;
  78. return pointer;
  79. }
  80. template <typename Purpose>
  81. static void deallocate(Purpose, thread_info_base* this_thread,
  82. void* pointer, std::size_t size)
  83. {
  84. if (size <= chunk_size * UCHAR_MAX)
  85. {
  86. if (this_thread && this_thread->reusable_memory_[Purpose::mem_index] == 0)
  87. {
  88. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  89. mem[0] = mem[size];
  90. this_thread->reusable_memory_[Purpose::mem_index] = pointer;
  91. return;
  92. }
  93. }
  94. ::operator delete(pointer);
  95. }
  96. private:
  97. enum { chunk_size = 4 };
  98. enum { max_mem_index = 3 };
  99. void* reusable_memory_[max_mem_index];
  100. };
  101. } // namespace detail
  102. } // namespace asio
  103. } // namespace boost
  104. #include <boost/asio/detail/pop_options.hpp>
  105. #endif // BOOST_ASIO_DETAIL_THREAD_INFO_BASE_HPP