Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thread_info_base.hpp 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 ASIO_DETAIL_THREAD_INFO_BASE_HPP
  11. #define 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 "asio/detail/noncopyable.hpp"
  18. #include "asio/detail/push_options.hpp"
  19. namespace asio {
  20. namespace detail {
  21. class thread_info_base
  22. : private noncopyable
  23. {
  24. public:
  25. struct default_tag
  26. {
  27. enum { mem_index = 0 };
  28. };
  29. struct awaitable_frame_tag
  30. {
  31. enum { mem_index = 1 };
  32. };
  33. struct executor_function_tag
  34. {
  35. enum { mem_index = 2 };
  36. };
  37. thread_info_base()
  38. {
  39. for (int i = 0; i < max_mem_index; ++i)
  40. reusable_memory_[i] = 0;
  41. }
  42. ~thread_info_base()
  43. {
  44. for (int i = 0; i < max_mem_index; ++i)
  45. if (reusable_memory_[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. #include "asio/detail/pop_options.hpp"
  103. #endif // ASIO_DETAIL_THREAD_INFO_BASE_HPP