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

source code of boost/libs/asio/include/boost/asio/recycling_allocator.hpp