recycling_allocator.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. //
  2. // detail/recycling_allocator.hpp
  3. // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2024 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_RECYCLING_ALLOCATOR_HPP
  11. #define BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_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/thread_context.hpp>
  18. #include <boost/asio/detail/thread_info_base.hpp>
  19. #include <boost/asio/detail/push_options.hpp>
  20. namespace boost {
  21. namespace asio {
  22. namespace detail {
  23. template <typename T, typename Purpose = thread_info_base::default_tag>
  24. class recycling_allocator
  25. {
  26. public:
  27. typedef T value_type;
  28. template <typename U>
  29. struct rebind
  30. {
  31. typedef recycling_allocator<U, Purpose> other;
  32. };
  33. recycling_allocator()
  34. {
  35. }
  36. template <typename U>
  37. recycling_allocator(const recycling_allocator<U, Purpose>&)
  38. {
  39. }
  40. T* allocate(std::size_t n)
  41. {
  42. #if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  43. void* p = thread_info_base::allocate(Purpose(),
  44. thread_context::top_of_thread_call_stack(),
  45. sizeof(T) * n, alignof(T));
  46. #else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  47. void* p = boost::asio::aligned_new(align, s);
  48. #endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  49. return static_cast<T*>(p);
  50. }
  51. void deallocate(T* p, std::size_t n)
  52. {
  53. #if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  54. thread_info_base::deallocate(Purpose(),
  55. thread_context::top_of_thread_call_stack(), p, sizeof(T) * n);
  56. #else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  57. (void)n;
  58. boost::asio::aligned_delete(p);
  59. #endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
  60. }
  61. };
  62. template <typename Purpose>
  63. class recycling_allocator<void, Purpose>
  64. {
  65. public:
  66. typedef void value_type;
  67. template <typename U>
  68. struct rebind
  69. {
  70. typedef recycling_allocator<U, Purpose> other;
  71. };
  72. recycling_allocator()
  73. {
  74. }
  75. template <typename U>
  76. recycling_allocator(const recycling_allocator<U, Purpose>&)
  77. {
  78. }
  79. };
  80. template <typename Allocator, typename Purpose>
  81. struct get_recycling_allocator
  82. {
  83. typedef Allocator type;
  84. static type get(const Allocator& a) { return a; }
  85. };
  86. template <typename T, typename Purpose>
  87. struct get_recycling_allocator<std::allocator<T>, Purpose>
  88. {
  89. typedef recycling_allocator<T, Purpose> type;
  90. static type get(const std::allocator<T>&) { return type(); }
  91. };
  92. } // namespace detail
  93. } // namespace asio
  94. } // namespace boost
  95. #include <boost/asio/detail/pop_options.hpp>
  96. #endif // BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP