thread_info_base.hpp 3.0 KB

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