handler_alloc_helpers.hpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // detail/handler_alloc_helpers.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2023 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/detail/recycling_allocator.hpp"
  19. #include "asio/detail/thread_info_base.hpp"
  20. #include "asio/associated_allocator.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. namespace detail {
  24. inline void* default_allocate(std::size_t s,
  25. std::size_t align = ASIO_DEFAULT_ALIGN)
  26. {
  27. #if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  28. return asio::detail::thread_info_base::allocate(
  29. asio::detail::thread_context::top_of_thread_call_stack(),
  30. s, align);
  31. #else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  32. return asio::aligned_new(align, s);
  33. #endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  34. }
  35. inline void default_deallocate(void* p, std::size_t s)
  36. {
  37. #if !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  38. asio::detail::thread_info_base::deallocate(
  39. asio::detail::thread_context::top_of_thread_call_stack(), p, s);
  40. #else // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  41. (void)s;
  42. asio::aligned_delete(p);
  43. #endif // !defined(ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  44. }
  45. template <typename T>
  46. class default_allocator
  47. {
  48. public:
  49. typedef T value_type;
  50. template <typename U>
  51. struct rebind
  52. {
  53. typedef default_allocator<U> other;
  54. };
  55. default_allocator() noexcept
  56. {
  57. }
  58. template <typename U>
  59. default_allocator(const default_allocator<U>&) noexcept
  60. {
  61. }
  62. T* allocate(std::size_t n)
  63. {
  64. return static_cast<T*>(default_allocate(sizeof(T) * n, alignof(T)));
  65. }
  66. void deallocate(T* p, std::size_t n)
  67. {
  68. default_deallocate(p, sizeof(T) * n);
  69. }
  70. };
  71. template <>
  72. class default_allocator<void>
  73. {
  74. public:
  75. typedef void value_type;
  76. template <typename U>
  77. struct rebind
  78. {
  79. typedef default_allocator<U> other;
  80. };
  81. default_allocator() noexcept
  82. {
  83. }
  84. template <typename U>
  85. default_allocator(const default_allocator<U>&) noexcept
  86. {
  87. }
  88. };
  89. template <typename Allocator>
  90. struct get_default_allocator
  91. {
  92. typedef Allocator type;
  93. static type get(const Allocator& a)
  94. {
  95. return a;
  96. }
  97. };
  98. template <typename T>
  99. struct get_default_allocator<std::allocator<T>>
  100. {
  101. typedef default_allocator<T> type;
  102. static type get(const std::allocator<T>&)
  103. {
  104. return type();
  105. }
  106. };
  107. } // namespace detail
  108. } // namespace asio
  109. #define ASIO_DEFINE_HANDLER_PTR(op) \
  110. struct ptr \
  111. { \
  112. Handler* h; \
  113. op* v; \
  114. op* p; \
  115. ~ptr() \
  116. { \
  117. reset(); \
  118. } \
  119. static op* allocate(Handler& handler) \
  120. { \
  121. typedef typename ::asio::associated_allocator< \
  122. Handler>::type associated_allocator_type; \
  123. typedef typename ::asio::detail::get_default_allocator< \
  124. associated_allocator_type>::type default_allocator_type; \
  125. ASIO_REBIND_ALLOC(default_allocator_type, op) a( \
  126. ::asio::detail::get_default_allocator< \
  127. associated_allocator_type>::get( \
  128. ::asio::get_associated_allocator(handler))); \
  129. return a.allocate(1); \
  130. } \
  131. void reset() \
  132. { \
  133. if (p) \
  134. { \
  135. p->~op(); \
  136. p = 0; \
  137. } \
  138. if (v) \
  139. { \
  140. typedef typename ::asio::associated_allocator< \
  141. Handler>::type associated_allocator_type; \
  142. typedef typename ::asio::detail::get_default_allocator< \
  143. associated_allocator_type>::type default_allocator_type; \
  144. ASIO_REBIND_ALLOC(default_allocator_type, op) a( \
  145. ::asio::detail::get_default_allocator< \
  146. associated_allocator_type>::get( \
  147. ::asio::get_associated_allocator(*h))); \
  148. a.deallocate(static_cast<op*>(v), 1); \
  149. v = 0; \
  150. } \
  151. } \
  152. } \
  153. /**/
  154. #define ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR(purpose, op) \
  155. struct ptr \
  156. { \
  157. const Alloc* a; \
  158. void* v; \
  159. op* p; \
  160. ~ptr() \
  161. { \
  162. reset(); \
  163. } \
  164. static op* allocate(const Alloc& a) \
  165. { \
  166. typedef typename ::asio::detail::get_recycling_allocator< \
  167. Alloc, purpose>::type recycling_allocator_type; \
  168. ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
  169. ::asio::detail::get_recycling_allocator< \
  170. Alloc, purpose>::get(a)); \
  171. return a1.allocate(1); \
  172. } \
  173. void reset() \
  174. { \
  175. if (p) \
  176. { \
  177. p->~op(); \
  178. p = 0; \
  179. } \
  180. if (v) \
  181. { \
  182. typedef typename ::asio::detail::get_recycling_allocator< \
  183. Alloc, purpose>::type recycling_allocator_type; \
  184. ASIO_REBIND_ALLOC(recycling_allocator_type, op) a1( \
  185. ::asio::detail::get_recycling_allocator< \
  186. Alloc, purpose>::get(*a)); \
  187. a1.deallocate(static_cast<op*>(v), 1); \
  188. v = 0; \
  189. } \
  190. } \
  191. } \
  192. /**/
  193. #define ASIO_DEFINE_HANDLER_ALLOCATOR_PTR(op) \
  194. ASIO_DEFINE_TAGGED_HANDLER_ALLOCATOR_PTR( \
  195. ::asio::detail::thread_info_base::default_tag, op ) \
  196. /**/
  197. #include "asio/detail/pop_options.hpp"
  198. #endif // ASIO_DETAIL_HANDLER_ALLOC_HELPERS_HPP