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