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.

190 lines
4.1KB

  1. //
  2. // detail/handler_alloc_helpers.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_HANDLER_ALLOC_HELPERS_HPP
  11. #define ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/config.hpp"
  16. #include "asio/detail/memory.hpp"
  17. #include "asio/detail/noncopyable.hpp"
  18. #include "asio/associated_allocator.hpp"
  19. #include "asio/handler_alloc_hook.hpp"
  20. #include "asio/detail/push_options.hpp"
  21. // Calls to asio_handler_allocate and asio_handler_deallocate must be made from
  22. // a namespace that does not contain any overloads of these functions. The
  23. // asio_handler_alloc_helpers namespace is defined here for that purpose.
  24. namespace asio_handler_alloc_helpers {
  25. template <typename Handler>
  26. inline void* allocate(std::size_t s, Handler& h)
  27. {
  28. #if !defined(ASIO_HAS_HANDLER_HOOKS)
  29. return ::operator new(s);
  30. #else
  31. using asio::asio_handler_allocate;
  32. return asio_handler_allocate(s, asio::detail::addressof(h));
  33. #endif
  34. }
  35. template <typename Handler>
  36. inline void deallocate(void* p, std::size_t s, Handler& h)
  37. {
  38. #if !defined(ASIO_HAS_HANDLER_HOOKS)
  39. ::operator delete(p);
  40. #else
  41. using asio::asio_handler_deallocate;
  42. asio_handler_deallocate(p, s, asio::detail::addressof(h));
  43. #endif
  44. }
  45. } // namespace asio_handler_alloc_helpers
  46. namespace asio {
  47. namespace detail {
  48. template <typename Handler, typename T>
  49. class hook_allocator
  50. {
  51. public:
  52. template <typename U>
  53. struct rebind
  54. {
  55. typedef hook_allocator<Handler, U> other;
  56. };
  57. explicit hook_allocator(Handler& h)
  58. : handler_(h)
  59. {
  60. }
  61. template <typename U>
  62. hook_allocator(const hook_allocator<Handler, U>& a)
  63. : handler_(a.handler_)
  64. {
  65. }
  66. T* allocate(std::size_t n)
  67. {
  68. return static_cast<T*>(
  69. asio_handler_alloc_helpers::allocate(sizeof(T) * n, handler_));
  70. }
  71. void deallocate(T* p, std::size_t n)
  72. {
  73. asio_handler_alloc_helpers::deallocate(p, sizeof(T) * n, handler_);
  74. }
  75. //private:
  76. Handler& handler_;
  77. };
  78. template <typename Handler>
  79. class hook_allocator<Handler, void>
  80. {
  81. public:
  82. template <typename U>
  83. struct rebind
  84. {
  85. typedef hook_allocator<Handler, U> other;
  86. };
  87. explicit hook_allocator(Handler& h)
  88. : handler_(h)
  89. {
  90. }
  91. template <typename U>
  92. hook_allocator(const hook_allocator<Handler, U>& a)
  93. : handler_(a.handler_)
  94. {
  95. }
  96. //private:
  97. Handler& handler_;
  98. };
  99. } // namespace detail
  100. } // namespace asio
  101. #define ASIO_DEFINE_HANDLER_PTR(op) \
  102. struct ptr \
  103. { \
  104. typedef typename ::asio::associated_allocator<Handler, \
  105. ::asio::detail::hook_allocator<Handler, \
  106. void> >::type::template rebind<op>::other allocator_type; \
  107. Handler* h; \
  108. op* v; \
  109. op* p; \
  110. ~ptr() \
  111. { \
  112. reset(); \
  113. } \
  114. static op* allocate(Handler& handler) \
  115. { \
  116. allocator_type a(::asio::associated_allocator<Handler, \
  117. ::asio::detail::hook_allocator<Handler, void> >::get(handler, \
  118. ::asio::detail::hook_allocator<Handler, void>(handler))); \
  119. return a.allocate(1); \
  120. } \
  121. void reset() \
  122. { \
  123. allocator_type a(::asio::associated_allocator<Handler, \
  124. ::asio::detail::hook_allocator<Handler, void> >::get(*h, \
  125. ::asio::detail::hook_allocator<Handler, void>(*h))); \
  126. if (p) \
  127. { \
  128. p->~op(); \
  129. p = 0; \
  130. } \
  131. if (v) \
  132. { \
  133. a.deallocate(static_cast<op*>(v), 1); \
  134. v = 0; \
  135. } \
  136. } \
  137. } \
  138. /**/
  139. #define ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(op, alloc) \
  140. struct ptr \
  141. { \
  142. typename alloc::template rebind<op>::other a; \
  143. void* v; \
  144. op* p; \
  145. ~ptr() \
  146. { \
  147. reset(); \
  148. } \
  149. void reset() \
  150. { \
  151. if (p) \
  152. { \
  153. p->~op(); \
  154. p = 0; \
  155. } \
  156. if (v) \
  157. { \
  158. a.deallocate(static_cast<op*>(v), 1); \
  159. v = 0; \
  160. } \
  161. } \
  162. } \
  163. /**/
  164. #include "asio/detail/pop_options.hpp"
  165. #endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP