Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-16 09:43:10

0001 //
0002 // detail/recycling_allocator.hpp
0003 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
0004 //
0005 // Copyright (c) 2003-2025 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_DETAIL_RECYCLING_ALLOCATOR_HPP
0012 #define BOOST_ASIO_DETAIL_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/memory.hpp>
0020 #include <boost/asio/detail/thread_context.hpp>
0021 #include <boost/asio/detail/thread_info_base.hpp>
0022 
0023 #include <boost/asio/detail/push_options.hpp>
0024 
0025 namespace boost {
0026 namespace asio {
0027 namespace detail {
0028 
0029 template <typename T, typename Purpose = thread_info_base::default_tag>
0030 class recycling_allocator
0031 {
0032 public:
0033   typedef T value_type;
0034 
0035   template <typename U>
0036   struct rebind
0037   {
0038     typedef recycling_allocator<U, Purpose> other;
0039   };
0040 
0041   recycling_allocator()
0042   {
0043   }
0044 
0045   template <typename U>
0046   recycling_allocator(const recycling_allocator<U, Purpose>&)
0047   {
0048   }
0049 
0050   T* allocate(std::size_t n)
0051   {
0052 #if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0053     void* p = thread_info_base::allocate(Purpose(),
0054         thread_context::top_of_thread_call_stack(),
0055         sizeof(T) * n, alignof(T));
0056 #else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0057     void* p = boost::asio::aligned_new(alignof(T), sizeof(T) * n);
0058 #endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0059     return static_cast<T*>(p);
0060   }
0061 
0062   void deallocate(T* p, std::size_t n)
0063   {
0064 #if !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0065     thread_info_base::deallocate(Purpose(),
0066         thread_context::top_of_thread_call_stack(), p, sizeof(T) * n);
0067 #else // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0068     (void)n;
0069     boost::asio::aligned_delete(p);
0070 #endif // !defined(BOOST_ASIO_DISABLE_SMALL_BLOCK_RECYCLING)
0071   }
0072 };
0073 
0074 template <typename Purpose>
0075 class recycling_allocator<void, Purpose>
0076 {
0077 public:
0078   typedef void value_type;
0079 
0080   template <typename U>
0081   struct rebind
0082   {
0083     typedef recycling_allocator<U, Purpose> other;
0084   };
0085 
0086   recycling_allocator()
0087   {
0088   }
0089 
0090   template <typename U>
0091   recycling_allocator(const recycling_allocator<U, Purpose>&)
0092   {
0093   }
0094 };
0095 
0096 template <typename Allocator, typename Purpose>
0097 struct get_recycling_allocator
0098 {
0099   typedef Allocator type;
0100   static type get(const Allocator& a) { return a; }
0101 };
0102 
0103 template <typename T, typename Purpose>
0104 struct get_recycling_allocator<std::allocator<T>, Purpose>
0105 {
0106   typedef recycling_allocator<T, Purpose> type;
0107   static type get(const std::allocator<T>&) { return type(); }
0108 };
0109 
0110 } // namespace detail
0111 } // namespace asio
0112 } // namespace boost
0113 
0114 #include <boost/asio/detail/pop_options.hpp>
0115 
0116 #endif // BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP