2 // detail/recycling_allocator.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2019 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP
12 #define BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/memory.hpp>
20 #include <boost/asio/detail/thread_context.hpp>
21 #include <boost/asio/detail/thread_info_base.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 template <typename T, typename Purpose = thread_info_base::default_tag>
30 class recycling_allocator
38 typedef recycling_allocator<U, Purpose> other;
46 recycling_allocator(const recycling_allocator<U, Purpose>&)
50 T* allocate(std::size_t n)
52 typedef thread_context::thread_call_stack call_stack;
53 void* p = thread_info_base::allocate(Purpose(),
54 call_stack::top(), sizeof(T) * n);
55 return static_cast<T*>(p);
58 void deallocate(T* p, std::size_t n)
60 typedef thread_context::thread_call_stack call_stack;
61 thread_info_base::deallocate(Purpose(),
62 call_stack::top(), p, sizeof(T) * n);
66 template <typename Purpose>
67 class recycling_allocator<void, Purpose>
70 typedef void value_type;
75 typedef recycling_allocator<U, Purpose> other;
83 recycling_allocator(const recycling_allocator<U, Purpose>&)
88 template <typename Allocator, typename Purpose>
89 struct get_recycling_allocator
91 typedef Allocator type;
92 static type get(const Allocator& a) { return a; }
95 template <typename T, typename Purpose>
96 struct get_recycling_allocator<std::allocator<T>, Purpose>
98 typedef recycling_allocator<T, Purpose> type;
99 static type get(const std::allocator<T>&) { return type(); }
102 } // namespace detail
106 #include <boost/asio/detail/pop_options.hpp>
108 #endif // BOOST_ASIO_DETAIL_RECYCLING_ALLOCATOR_HPP