Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:11

0001 //
0002 // recycling_allocator.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2023 Christopher M. Kohlhoff (chris at kohlhoff dot com)
0006 //
0007 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0009 //
0010 
0011 #ifndef BOOST_ASIO_RECYCLING_ALLOCATOR_HPP
0012 #define BOOST_ASIO_RECYCLING_ALLOCATOR_HPP
0013 
0014 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
0015 # pragma once
0016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
0017 
0018 #include <boost/asio/detail/config.hpp>
0019 #include <boost/asio/detail/recycling_allocator.hpp>
0020 
0021 #include <boost/asio/detail/push_options.hpp>
0022 
0023 namespace boost {
0024 namespace asio {
0025 
0026 /// An allocator that caches memory blocks in thread-local storage for reuse.
0027 /**
0028  * The @recycling_allocator uses a simple strategy where a limited number of
0029  * small memory blocks are cached in thread-local storage, if the current
0030  * thread is running an @c io_context or is part of a @c thread_pool.
0031  */
0032 template <typename T>
0033 class recycling_allocator
0034 {
0035 public:
0036   /// The type of object allocated by the recycling allocator.
0037   typedef T value_type;
0038 
0039   /// Rebind the allocator to another value_type.
0040   template <typename U>
0041   struct rebind
0042   {
0043     /// The rebound @c allocator type.
0044     typedef recycling_allocator<U> other;
0045   };
0046 
0047   /// Default constructor.
0048   constexpr recycling_allocator() noexcept
0049   {
0050   }
0051 
0052   /// Converting constructor.
0053   template <typename U>
0054   constexpr recycling_allocator(
0055       const recycling_allocator<U>&) noexcept
0056   {
0057   }
0058 
0059   /// Equality operator. Always returns true.
0060   constexpr bool operator==(
0061       const recycling_allocator&) const noexcept
0062   {
0063     return true;
0064   }
0065 
0066   /// Inequality operator. Always returns false.
0067   constexpr bool operator!=(
0068       const recycling_allocator&) const noexcept
0069   {
0070     return false;
0071   }
0072 
0073   /// Allocate memory for the specified number of values.
0074   T* allocate(std::size_t n)
0075   {
0076     return detail::recycling_allocator<T>().allocate(n);
0077   }
0078 
0079   /// Deallocate memory for the specified number of values.
0080   void deallocate(T* p, std::size_t n)
0081   {
0082     detail::recycling_allocator<T>().deallocate(p, n);
0083   }
0084 };
0085 
0086 /// A proto-allocator that caches memory blocks in thread-local storage for
0087 /// reuse.
0088 /**
0089  * The @recycling_allocator uses a simple strategy where a limited number of
0090  * small memory blocks are cached in thread-local storage, if the current
0091  * thread is running an @c io_context or is part of a @c thread_pool.
0092  */
0093 template <>
0094 class recycling_allocator<void>
0095 {
0096 public:
0097   /// No values are allocated by a proto-allocator.
0098   typedef void value_type;
0099 
0100   /// Rebind the allocator to another value_type.
0101   template <typename U>
0102   struct rebind
0103   {
0104     /// The rebound @c allocator type.
0105     typedef recycling_allocator<U> other;
0106   };
0107 
0108   /// Default constructor.
0109   constexpr recycling_allocator() noexcept
0110   {
0111   }
0112 
0113   /// Converting constructor.
0114   template <typename U>
0115   constexpr recycling_allocator(
0116       const recycling_allocator<U>&) noexcept
0117   {
0118   }
0119 
0120   /// Equality operator. Always returns true.
0121   constexpr bool operator==(
0122       const recycling_allocator&) const noexcept
0123   {
0124     return true;
0125   }
0126 
0127   /// Inequality operator. Always returns false.
0128   constexpr bool operator!=(
0129       const recycling_allocator&) const noexcept
0130   {
0131     return false;
0132   }
0133 };
0134 
0135 } // namespace asio
0136 } // namespace boost
0137 
0138 #include <boost/asio/detail/pop_options.hpp>
0139 
0140 #endif // BOOST_ASIO_RECYCLING_ALLOCATOR_HPP