recycling_allocator.hpp 3.3 KB

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