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.

116 lines
3.1KB

  1. /* Copyright 2016, Ableton AG, Berlin. All rights reserved.
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. *
  16. * If you would like to incorporate Link into a proprietary software application,
  17. * please contact <link-devs@ableton.com>.
  18. */
  19. #pragma once
  20. #include <ableton/platforms/asio/AsioWrapper.hpp>
  21. #include <ableton/util/Injected.hpp>
  22. namespace ableton
  23. {
  24. namespace platforms
  25. {
  26. namespace asio
  27. {
  28. template <typename IoContext,
  29. std::size_t MaxNumHandlers = 32,
  30. std::size_t MaxHandlerSize = 128>
  31. struct PooledHandlerContext
  32. {
  33. PooledHandlerContext(util::Injected<IoContext> io)
  34. : mIo(std::move(io))
  35. {
  36. // initialize the handler free store
  37. mFreeStack.reserve(MaxNumHandlers);
  38. for (std::size_t i = 0; i < MaxNumHandlers; ++i)
  39. {
  40. mFreeStack.push_back(reinterpret_cast<void*>(mRaw + i));
  41. }
  42. }
  43. template <typename Handler>
  44. void async(Handler handler)
  45. {
  46. try
  47. {
  48. mIo->async(HandlerWrapper<Handler>{*this, std::move(handler)});
  49. }
  50. catch (std::bad_alloc)
  51. {
  52. warning(mIo->log()) << "Handler dropped due to low memory pool";
  53. }
  54. }
  55. template <typename Handler>
  56. struct HandlerWrapper
  57. {
  58. HandlerWrapper(PooledHandlerContext& context, Handler handler)
  59. : mContext(context)
  60. , mHandler(std::move(handler))
  61. {
  62. }
  63. void operator()()
  64. {
  65. mHandler();
  66. }
  67. // Use pooled allocation so that posting handlers will not cause
  68. // system allocation
  69. friend void* asio_handler_allocate(
  70. const std::size_t size, HandlerWrapper* const pHandler)
  71. {
  72. if (size > MaxHandlerSize || pHandler->mContext.mFreeStack.empty())
  73. {
  74. // Going over the max handler size is a programming error, as
  75. // this is not a dynamically variable value.
  76. assert(size <= MaxHandlerSize);
  77. throw std::bad_alloc();
  78. }
  79. else
  80. {
  81. const auto p = pHandler->mContext.mFreeStack.back();
  82. pHandler->mContext.mFreeStack.pop_back();
  83. return p;
  84. }
  85. }
  86. friend void asio_handler_deallocate(
  87. void* const p, std::size_t, HandlerWrapper* const pHandler)
  88. {
  89. pHandler->mContext.mFreeStack.push_back(p);
  90. }
  91. PooledHandlerContext& mContext;
  92. Handler mHandler;
  93. };
  94. using MemChunk = typename std::aligned_storage<MaxHandlerSize,
  95. std::alignment_of<max_align_t>::value>::type;
  96. MemChunk mRaw[MaxNumHandlers];
  97. std::vector<void*> mFreeStack;
  98. util::Injected<IoContext> mIo;
  99. };
  100. } // namespace asio
  101. } // namespace platforms
  102. } // namespace ableton