include/boost/capy/ex/recycling_memory_resource.hpp

80.4% Lines (41/51) 90.0% List of functions (9/10) 73.1% Branches (19/26)
f(x) Functions (10)
Line Branch TLA Hits Source Code
1 //
2 // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
3 //
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // Official repository: https://github.com/cppalliance/capy
8 //
9
10 #ifndef BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP
11 #define BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP
12
13 #include <boost/capy/detail/config.hpp>
14
15 #include <bit>
16 #include <cstddef>
17 #include <memory_resource>
18 #include <mutex>
19
20 namespace boost {
21 namespace capy {
22
23 /** Recycling memory resource with size-class buckets.
24
25 This memory resource recycles memory blocks using power-of-two
26 size classes for O(1) allocation lookup. It maintains a thread-local
27 pool for fast lock-free access and a global pool for cross-thread
28 block sharing.
29
30 Size classes: 64, 128, 256, 512, 1024, 2048 bytes.
31 Allocations larger than 2048 bytes bypass the pools entirely.
32
33 This is the default allocator used by run_async when no allocator
34 is specified.
35
36 @par Thread Safety
37 Thread-safe. The thread-local pool requires no synchronization.
38 The global pool uses a mutex for cross-thread access.
39
40 @par Example
41 @code
42 auto* mr = get_recycling_memory_resource();
43 run_async(ex, mr)(my_task());
44 @endcode
45
46 @see get_recycling_memory_resource
47 @see run_async
48 */
49 BOOST_CAPY_MSVC_WARNING_PUSH
50 BOOST_CAPY_MSVC_WARNING_DISABLE(4275) // non dll-interface base class
51 class BOOST_CAPY_DECL recycling_memory_resource : public std::pmr::memory_resource
52 {
53 static constexpr std::size_t num_classes = 6;
54 static constexpr std::size_t min_class_size = 64; // 2^6
55 static constexpr std::size_t max_class_size = 2048; // 2^11
56 static constexpr std::size_t bucket_capacity = 16;
57
58 static std::size_t
59 9930x round_up_pow2(std::size_t n) noexcept
60 {
61
1/2
✓ Branch 0 taken 9930 times.
✗ Branch 1 not taken.
9930x return n <= min_class_size ? min_class_size : std::bit_ceil(n);
62 }
63
64 static std::size_t
65 9930x get_class_index(std::size_t rounded) noexcept
66 {
67 9930x std::size_t idx = std::countr_zero(rounded) - 6; // 64 = 2^6
68
1/2
✓ Branch 0 taken 9930 times.
✗ Branch 1 not taken.
9930x return idx < num_classes ? idx : num_classes;
69 }
70
71 struct bucket
72 {
73 std::size_t count = 0;
74 void* ptrs[bucket_capacity];
75
76 5072x void* pop() noexcept
77 {
78
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 4965 times.
5072x if(count == 0)
79 107x return nullptr;
80 4965x return ptrs[--count];
81 }
82
83 // Peter Dimov's idea
84 107x void* pop(bucket& b) noexcept
85 {
86
1/2
✓ Branch 0 taken 107 times.
✗ Branch 1 not taken.
107x if(count == 0)
87 107x return nullptr;
88 for(std::size_t i = 0; i < count; ++i)
89 b.ptrs[i] = ptrs[i];
90 b.count = count - 1;
91 count = 0;
92 return b.ptrs[b.count];
93 }
94
95 4969x bool push(void* p) noexcept
96 {
97
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 4965 times.
4969x if(count >= bucket_capacity)
98 4x return false;
99 4965x ptrs[count++] = p;
100 4965x return true;
101 }
102 };
103
104 struct pool
105 {
106 bucket buckets[num_classes];
107
108 67x ~pool()
109 {
110
2/2
✓ Branch 0 taken 402 times.
✓ Branch 1 taken 67 times.
469x for(auto& b : buckets)
111
2/2
✓ Branch 0 taken 107 times.
✓ Branch 1 taken 402 times.
509x while(b.count > 0)
112 107x ::operator delete(b.pop());
113 67x }
114 };
115
116 10037x static pool& local() noexcept
117 {
118
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 10003 times.
10037x static thread_local pool p;
119 10037x return p;
120 }
121
122 static pool& global() noexcept;
123 static std::mutex& global_mutex() noexcept;
124
125 void* allocate_slow(std::size_t rounded, std::size_t idx);
126 void deallocate_slow(void* p, std::size_t idx);
127
128 public:
129 ~recycling_memory_resource();
130
131 /** Allocate without virtual dispatch.
132
133 Handles the fast path inline (thread-local bucket pop)
134 and falls through to the slow path for global pool or
135 heap allocation.
136 */
137 void*
138 4965x allocate_fast(std::size_t bytes, std::size_t)
139 {
140 4965x std::size_t rounded = round_up_pow2(bytes);
141 4965x std::size_t idx = get_class_index(rounded);
142
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4965 times.
4965x if(idx >= num_classes)
143 return ::operator new(bytes);
144 4965x auto& lp = local();
145
2/2
✓ Branch 1 taken 4858 times.
✓ Branch 2 taken 107 times.
4965x if(auto* p = lp.buckets[idx].pop())
146 4858x return p;
147 107x return allocate_slow(rounded, idx);
148 }
149
150 /** Deallocate without virtual dispatch.
151
152 Handles the fast path inline (thread-local bucket push)
153 and falls through to the slow path for global pool or
154 heap deallocation.
155 */
156 void
157 4965x deallocate_fast(void* p, std::size_t bytes, std::size_t)
158 {
159 4965x std::size_t rounded = round_up_pow2(bytes);
160 4965x std::size_t idx = get_class_index(rounded);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4965 times.
4965x if(idx >= num_classes)
162 {
163 ::operator delete(p);
164 return;
165 }
166 4965x auto& lp = local();
167
2/2
✓ Branch 1 taken 4961 times.
✓ Branch 2 taken 4 times.
4965x if(lp.buckets[idx].push(p))
168 4961x return;
169 4x deallocate_slow(p, idx);
170 }
171
172 protected:
173 void*
174 do_allocate(std::size_t bytes, std::size_t) override;
175
176 void
177 do_deallocate(void* p, std::size_t bytes, std::size_t) override;
178
179 bool
180 do_is_equal(const memory_resource& other) const noexcept override
181 {
182 return this == &other;
183 }
184 };
185 BOOST_CAPY_MSVC_WARNING_POP
186
187 /** Returns pointer to the default recycling memory resource.
188
189 The returned pointer is valid for the lifetime of the program.
190 This is the default allocator used by run_async.
191
192 @return Pointer to the recycling memory resource.
193
194 @see recycling_memory_resource
195 @see run_async
196 */
197 BOOST_CAPY_DECL
198 std::pmr::memory_resource*
199 get_recycling_memory_resource() noexcept;
200
201 } // namespace capy
202 } // namespace boost
203
204 #endif
205