handler_alloc_helpers.hpp 5.9 KB

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