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.

92 lines
2.1KB

  1. //
  2. // detail/thread_info_base.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2015 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. thread_info_base()
  26. : reusable_memory_(0)
  27. {
  28. }
  29. ~thread_info_base()
  30. {
  31. if (reusable_memory_)
  32. ::operator delete(reusable_memory_);
  33. }
  34. static void* allocate(thread_info_base* this_thread, std::size_t size)
  35. {
  36. if (this_thread && this_thread->reusable_memory_)
  37. {
  38. void* const pointer = this_thread->reusable_memory_;
  39. this_thread->reusable_memory_ = 0;
  40. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  41. if (static_cast<std::size_t>(mem[0]) >= size)
  42. {
  43. mem[size] = mem[0];
  44. return pointer;
  45. }
  46. ::operator delete(pointer);
  47. }
  48. void* const pointer = ::operator new(size + 1);
  49. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  50. mem[size] = (size <= UCHAR_MAX) ? static_cast<unsigned char>(size) : 0;
  51. return pointer;
  52. }
  53. static void deallocate(thread_info_base* this_thread,
  54. void* pointer, std::size_t size)
  55. {
  56. if (size <= UCHAR_MAX)
  57. {
  58. if (this_thread && this_thread->reusable_memory_ == 0)
  59. {
  60. unsigned char* const mem = static_cast<unsigned char*>(pointer);
  61. mem[0] = mem[size];
  62. this_thread->reusable_memory_ = pointer;
  63. return;
  64. }
  65. }
  66. ::operator delete(pointer);
  67. }
  68. private:
  69. void* reusable_memory_;
  70. };
  71. } // namespace detail
  72. } // namespace asio
  73. #include "asio/detail/pop_options.hpp"
  74. #endif // ASIO_DETAIL_THREAD_INFO_BASE_HPP