recycling_allocator.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // 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_RECYCLING_ALLOCATOR_HPP
  11. #define BOOST_ASIO_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/recycling_allocator.hpp>
  17. #include <boost/asio/detail/push_options.hpp>
  18. namespace boost {
  19. namespace asio {
  20. /// An allocator that caches memory blocks in thread-local storage for reuse.
  21. /**
  22. * The @recycling_allocator uses a simple strategy where a limited number of
  23. * small memory blocks are cached in thread-local storage, if the current
  24. * thread is running an @c io_context or is part of a @c thread_pool.
  25. */
  26. template <typename T>
  27. class recycling_allocator
  28. {
  29. public:
  30. /// The type of object allocated by the recycling allocator.
  31. typedef T value_type;
  32. /// Rebind the allocator to another value_type.
  33. template <typename U>
  34. struct rebind
  35. {
  36. /// The rebound @c allocator type.
  37. typedef recycling_allocator<U> other;
  38. };
  39. /// Default constructor.
  40. constexpr recycling_allocator() noexcept
  41. {
  42. }
  43. /// Converting constructor.
  44. template <typename U>
  45. constexpr recycling_allocator(
  46. const recycling_allocator<U>&) noexcept
  47. {
  48. }
  49. /// Equality operator. Always returns true.
  50. constexpr bool operator==(
  51. const recycling_allocator&) const noexcept
  52. {
  53. return true;
  54. }
  55. /// Inequality operator. Always returns false.
  56. constexpr bool operator!=(
  57. const recycling_allocator&) const noexcept
  58. {
  59. return false;
  60. }
  61. /// Allocate memory for the specified number of values.
  62. T* allocate(std::size_t n)
  63. {
  64. return detail::recycling_allocator<T>().allocate(n);
  65. }
  66. /// Deallocate memory for the specified number of values.
  67. void deallocate(T* p, std::size_t n)
  68. {
  69. detail::recycling_allocator<T>().deallocate(p, n);
  70. }
  71. };
  72. /// A proto-allocator that caches memory blocks in thread-local storage for
  73. /// reuse.
  74. /**
  75. * The @recycling_allocator uses a simple strategy where a limited number of
  76. * small memory blocks are cached in thread-local storage, if the current
  77. * thread is running an @c io_context or is part of a @c thread_pool.
  78. */
  79. template <>
  80. class recycling_allocator<void>
  81. {
  82. public:
  83. /// No values are allocated by a proto-allocator.
  84. typedef void value_type;
  85. /// Rebind the allocator to another value_type.
  86. template <typename U>
  87. struct rebind
  88. {
  89. /// The rebound @c allocator type.
  90. typedef recycling_allocator<U> other;
  91. };
  92. /// Default constructor.
  93. constexpr recycling_allocator() noexcept
  94. {
  95. }
  96. /// Converting constructor.
  97. template <typename U>
  98. constexpr recycling_allocator(
  99. const recycling_allocator<U>&) noexcept
  100. {
  101. }
  102. /// Equality operator. Always returns true.
  103. constexpr bool operator==(
  104. const recycling_allocator&) const noexcept
  105. {
  106. return true;
  107. }
  108. /// Inequality operator. Always returns false.
  109. constexpr bool operator!=(
  110. const recycling_allocator&) const noexcept
  111. {
  112. return false;
  113. }
  114. };
  115. } // namespace asio
  116. } // namespace boost
  117. #include <boost/asio/detail/pop_options.hpp>
  118. #endif // BOOST_ASIO_RECYCLING_ALLOCATOR_HPP