// // recycling_allocator.hpp // ~~~~~~~~~~~~~~~~~~~~~~~ // // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com) // // Distributed under the Boost Software License, Version 1.0. (See accompanying // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) // #ifndef ASIO_RECYCLING_ALLOCATOR_HPP #define ASIO_RECYCLING_ALLOCATOR_HPP #if defined(_MSC_VER) && (_MSC_VER >= 1200) # pragma once #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) #include "asio/detail/config.hpp" #include "asio/detail/recycling_allocator.hpp" #include "asio/detail/push_options.hpp" namespace asio { /// An allocator that caches memory blocks in thread-local storage for reuse. /** * The @recycling_allocator uses a simple strategy where a limited number of * small memory blocks are cached in thread-local storage, if the current * thread is running an @c io_context or is part of a @c thread_pool. */ template class recycling_allocator { public: /// The type of object allocated by the recycling allocator. typedef T value_type; /// Rebind the allocator to another value_type. template struct rebind { /// The rebound @c allocator type. typedef recycling_allocator other; }; /// Default constructor. ASIO_CONSTEXPR recycling_allocator() ASIO_NOEXCEPT { } /// Converting constructor. template ASIO_CONSTEXPR recycling_allocator( const recycling_allocator&) ASIO_NOEXCEPT { } /// Equality operator. Always returns true. ASIO_CONSTEXPR bool operator==( const recycling_allocator&) const ASIO_NOEXCEPT { return true; } /// Inequality operator. Always returns false. ASIO_CONSTEXPR bool operator!=( const recycling_allocator&) const ASIO_NOEXCEPT { return false; } /// Allocate memory for the specified number of values. T* allocate(std::size_t n) { return detail::recycling_allocator().allocate(n); } /// Deallocate memory for the specified number of values. void deallocate(T* p, std::size_t n) { detail::recycling_allocator().deallocate(p, n); } }; /// A proto-allocator that caches memory blocks in thread-local storage for /// reuse. /** * The @recycling_allocator uses a simple strategy where a limited number of * small memory blocks are cached in thread-local storage, if the current * thread is running an @c io_context or is part of a @c thread_pool. */ template <> class recycling_allocator { public: /// No values are allocated by a proto-allocator. typedef void value_type; /// Rebind the allocator to another value_type. template struct rebind { /// The rebound @c allocator type. typedef recycling_allocator other; }; /// Default constructor. ASIO_CONSTEXPR recycling_allocator() ASIO_NOEXCEPT { } /// Converting constructor. template ASIO_CONSTEXPR recycling_allocator( const recycling_allocator&) ASIO_NOEXCEPT { } /// Equality operator. Always returns true. ASIO_CONSTEXPR bool operator==( const recycling_allocator&) const ASIO_NOEXCEPT { return true; } /// Inequality operator. Always returns false. ASIO_CONSTEXPR bool operator!=( const recycling_allocator&) const ASIO_NOEXCEPT { return false; } }; } // namespace asio #include "asio/detail/pop_options.hpp" #endif // ASIO_RECYCLING_ALLOCATOR_HPP