94.92% Lines (56/59)
100.00% Functions (9/9)
| TLA | Baseline | Branch | ||||||
|---|---|---|---|---|---|---|---|---|
| Line | Hits | Code | Line | Hits | Code | |||
| 1 | // | 1 | // | |||||
| 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | 2 | // Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com) | |||||
| 3 | // Copyright (c) 2026 Michael Vandeberg | 3 | // Copyright (c) 2026 Michael Vandeberg | |||||
| 4 | // | 4 | // | |||||
| 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | 5 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |||||
| 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | 6 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |||||
| 7 | // | 7 | // | |||||
| 8 | // Official repository: https://github.com/cppalliance/capy | 8 | // Official repository: https://github.com/cppalliance/capy | |||||
| 9 | // | 9 | // | |||||
| 10 | 10 | |||||||
| 11 | #ifndef BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP | 11 | #ifndef BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP | |||||
| 12 | #define BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP | 12 | #define BOOST_CAPY_RECYCLING_MEMORY_RESOURCE_HPP | |||||
| 13 | 13 | |||||||
| 14 | #include <boost/capy/detail/config.hpp> | 14 | #include <boost/capy/detail/config.hpp> | |||||
| 15 | 15 | |||||||
| 16 | #include <bit> | 16 | #include <bit> | |||||
| 17 | #include <cstddef> | 17 | #include <cstddef> | |||||
| 18 | #include <memory_resource> | 18 | #include <memory_resource> | |||||
| 19 | #include <mutex> | 19 | #include <mutex> | |||||
| 20 | #include <new> | 20 | #include <new> | |||||
| 21 | 21 | |||||||
| 22 | namespace boost { | 22 | namespace boost { | |||||
| 23 | namespace capy { | 23 | namespace capy { | |||||
| 24 | 24 | |||||||
| 25 | /** Recycles freed blocks through per-thread pools, with a shared pool for cross-thread reuse. | 25 | /** Recycles freed blocks through per-thread pools, with a shared pool for cross-thread reuse. | |||||
| 26 | 26 | |||||||
| 27 | This memory resource recycles memory blocks using power-of-two | 27 | This memory resource recycles memory blocks using power-of-two | |||||
| 28 | size classes for O(1) allocation lookup. It maintains a thread-local | 28 | size classes for O(1) allocation lookup. It maintains a thread-local | |||||
| 29 | pool for fast lock-free access and a global pool for cross-thread | 29 | pool for fast lock-free access and a global pool for cross-thread | |||||
| 30 | block sharing. | 30 | block sharing. | |||||
| 31 | 31 | |||||||
| 32 | Size classes: 64, 128, 256, 512, 1024, 2048 bytes. | 32 | Size classes: 64, 128, 256, 512, 1024, 2048 bytes. | |||||
| 33 | Allocations larger than 2048 bytes bypass the pools entirely. | 33 | Allocations larger than 2048 bytes bypass the pools entirely. | |||||
| 34 | 34 | |||||||
| 35 | Each pool caches at most 256 KiB of blocks per size class: | 35 | Each pool caches at most 256 KiB of blocks per size class: | |||||
| 36 | 4096 blocks of the smallest class, 128 of the largest. A freed | 36 | 4096 blocks of the smallest class, 128 of the largest. A freed | |||||
| 37 | block that fits in neither the thread's pool nor the global pool | 37 | block that fits in neither the thread's pool nor the global pool | |||||
| 38 | goes back to `::operator delete`. | 38 | goes back to `::operator delete`. | |||||
| 39 | 39 | |||||||
| 40 | Blocks a thread caches but stops using drain back to the global | 40 | Blocks a thread caches but stops using drain back to the global | |||||
| 41 | pool. The drain runs as the thread goes on freeing blocks or | 41 | pool. The drain runs as the thread goes on freeing blocks or | |||||
| 42 | refilling from the global pool. Other threads can then reuse what a | 42 | refilling from the global pool. Other threads can then reuse what a | |||||
| 43 | burst leaves behind. Blocks the global pool has no room for go back | 43 | burst leaves behind. Blocks the global pool has no room for go back | |||||
| 44 | to `::operator delete`. | 44 | to `::operator delete`. | |||||
| 45 | 45 | |||||||
| 46 | - | This is the default allocator used by run_async when no allocator | 46 | + | This is the default frame allocator of an @ref execution_context. | |||
| 47 | - | is specified. | 47 | + | @ref run_async uses the execution context's frame allocator when no | |||
| 48 | + | allocator is given. Some execution contexts, such as | ||||||
| 49 | + | @ref thread_pool, replace that default. | ||||||
| 48 | 50 | |||||||
| 49 | @par Thread Safety | 51 | @par Thread Safety | |||||
| 50 | Thread-safe. The thread-local pool requires no synchronization. | 52 | Thread-safe. The thread-local pool requires no synchronization. | |||||
| 51 | The global pool uses a mutex for cross-thread access. | 53 | The global pool uses a mutex for cross-thread access. | |||||
| 52 | 54 | |||||||
| 53 | @par Example | 55 | @par Example | |||||
| 54 | @par !example example | 56 | @par !example example | |||||
| 55 | 57 | |||||||
| 56 | 58 | |||||||
| 57 | @see @ref get_recycling_memory_resource | 59 | @see @ref get_recycling_memory_resource | |||||
| 58 | @see @ref run_async | 60 | @see @ref run_async | |||||
| 59 | */ | 61 | */ | |||||
| 60 | BOOST_CAPY_MSVC_WARNING_PUSH | 62 | BOOST_CAPY_MSVC_WARNING_PUSH | |||||
| 61 | BOOST_CAPY_MSVC_WARNING_DISABLE(4275) // non dll-interface base class | 63 | BOOST_CAPY_MSVC_WARNING_DISABLE(4275) // non dll-interface base class | |||||
| 62 | class BOOST_CAPY_DECL recycling_memory_resource : public std::pmr::memory_resource | 64 | class BOOST_CAPY_DECL recycling_memory_resource : public std::pmr::memory_resource | |||||
| 63 | { | 65 | { | |||||
| 64 | static constexpr std::size_t num_classes = 6; | 66 | static constexpr std::size_t num_classes = 6; | |||||
| 65 | static constexpr std::size_t min_class_size = 64; // 2^6 | 67 | static constexpr std::size_t min_class_size = 64; // 2^6 | |||||
| 66 | static constexpr std::size_t max_class_size = 2048; // 2^11 | 68 | static constexpr std::size_t max_class_size = 2048; // 2^11 | |||||
| 67 | static constexpr int min_class_shift = std::countr_zero(min_class_size); | 69 | static constexpr int min_class_shift = std::countr_zero(min_class_size); | |||||
| 68 | static constexpr std::size_t max_cached_bytes = 256 * 1024; | 70 | static constexpr std::size_t max_cached_bytes = 256 * 1024; | |||||
| 69 | 71 | |||||||
| 70 | // Frees on one thread, plus blocks it refills from the global pool, | 72 | // Frees on one thread, plus blocks it refills from the global pool, | |||||
| 71 | // between decay passes. Allocations served from the thread's pool | 73 | // between decay passes. Allocations served from the thread's pool | |||||
| 72 | // aren't counted, which keeps the counter off that path. | 74 | // aren't counted, which keeps the counter off that path. | |||||
| 73 | static constexpr std::size_t decay_period = 4096; | 75 | static constexpr std::size_t decay_period = 4096; | |||||
| 74 | 76 | |||||||
| 75 | static std::size_t | 77 | static std::size_t | |||||
| HITCBC | 76 | 2287960 | round_up_pow2(std::size_t n) noexcept | 78 | 2288238 | round_up_pow2(std::size_t n) noexcept | ||
| 77 | { | 79 | { | |||||
| HITCBC | 78 | 2287960 | return n <= min_class_size ? min_class_size : std::bit_ceil(n); | 80 | 2288238 | return n <= min_class_size ? min_class_size : std::bit_ceil(n); | ||
| 79 | } | 81 | } | |||||
| 80 | 82 | |||||||
| 81 | static std::size_t | 83 | static std::size_t | |||||
| HITCBC | 82 | 2287960 | get_class_index(std::size_t rounded) noexcept | 84 | 2288238 | get_class_index(std::size_t rounded) noexcept | ||
| 83 | { | 85 | { | |||||
| 84 | auto const idx = static_cast<std::size_t>( | 86 | auto const idx = static_cast<std::size_t>( | |||||
| HITCBC | 85 | 2287960 | std::countr_zero(rounded) - min_class_shift); | 87 | 2288238 | std::countr_zero(rounded) - min_class_shift); | ||
| HITCBC | 86 | 2287960 | return idx < num_classes ? idx : num_classes; | 88 | 2288238 | return idx < num_classes ? idx : num_classes; | ||
| 87 | } | 89 | } | |||||
| 88 | 90 | |||||||
| 89 | static constexpr std::size_t | 91 | static constexpr std::size_t | |||||
| HITCBC | 90 | 1410482 | bucket_capacity(std::size_t idx) noexcept | 92 | 1410621 | bucket_capacity(std::size_t idx) noexcept | ||
| 91 | { | 93 | { | |||||
| HITCBC | 92 | 1410482 | return max_cached_bytes >> (idx + min_class_shift); | 94 | 1410621 | return max_cached_bytes >> (idx + min_class_shift); | ||
| 93 | } | 95 | } | |||||
| 94 | 96 | |||||||
| 95 | // Cached blocks are linked through their own storage, so a bucket | 97 | // Cached blocks are linked through their own storage, so a bucket | |||||
| 96 | // holds any number of them without a side array. | 98 | // holds any number of them without a side array. | |||||
| 97 | struct node | 99 | struct node | |||||
| 98 | { | 100 | { | |||||
| 99 | node* next; | 101 | node* next; | |||||
| 100 | }; | 102 | }; | |||||
| 101 | 103 | |||||||
| 102 | struct bucket | 104 | struct bucket | |||||
| 103 | { | 105 | { | |||||
| 104 | node* head = nullptr; | 106 | node* head = nullptr; | |||||
| 105 | std::size_t count = 0; | 107 | std::size_t count = 0; | |||||
| 106 | 108 | |||||||
| 107 | // Fewest blocks cached since the last decay pass: that many sat | 109 | // Fewest blocks cached since the last decay pass: that many sat | |||||
| 108 | // unused through the whole period. | 110 | // unused through the whole period. | |||||
| 109 | std::size_t low = 0; | 111 | std::size_t low = 0; | |||||
| 110 | 112 | |||||||
| HITCBC | 111 | 1199439 | void* pop() noexcept | 113 | 1199578 | void* pop() noexcept | ||
| 112 | { | 114 | { | |||||
| HITCBC | 113 | 1199439 | node* n = head; | 115 | 1199578 | node* n = head; | ||
| HITCBC | 114 | 1199439 | if(n == nullptr) | 116 | 1199578 | if(n == nullptr) | ||
| HITCBC | 115 | 302445 | return nullptr; | 117 | 302445 | return nullptr; | ||
| HITCBC | 116 | 896994 | head = n->next; | 118 | 897133 | head = n->next; | ||
| HITCBC | 117 | 896994 | --count; | 119 | 897133 | --count; | ||
| HITCBC | 118 | 896994 | if(count < low) | 120 | 897133 | if(count < low) | ||
| HITCBC | 119 | 53349 | low = count; | 121 | 53349 | low = count; | ||
| HITCBC | 120 | 896994 | return n; | 122 | 897133 | return n; | ||
| 121 | } | 123 | } | |||||
| 122 | 124 | |||||||
| 123 | // Peter Dimov's idea | 125 | // Peter Dimov's idea | |||||
| 124 | // `b` must be empty: its chain is overwritten. | 126 | // `b` must be empty: its chain is overwritten. | |||||
| HITCBC | 125 | 302443 | void* pop(bucket& b) noexcept | 127 | 302443 | void* pop(bucket& b) noexcept | ||
| 126 | { | 128 | { | |||||
| HITCBC | 127 | 302443 | if(head == nullptr) | 129 | 302443 | if(head == nullptr) | ||
| HITCBC | 128 | 302414 | return nullptr; | 130 | 302414 | return nullptr; | ||
| HITCBC | 129 | 29 | b.head = head; | 131 | 29 | b.head = head; | ||
| HITCBC | 130 | 29 | b.count = count; | 132 | 29 | b.count = count; | ||
| HITCBC | 131 | 29 | head = nullptr; | 133 | 29 | head = nullptr; | ||
| HITCBC | 132 | 29 | count = 0; | 134 | 29 | count = 0; | ||
| HITCBC | 133 | 29 | low = 0; | 135 | 29 | low = 0; | ||
| HITCBC | 134 | 29 | return b.pop(); | 136 | 29 | return b.pop(); | ||
| 135 | } | 137 | } | |||||
| 136 | 138 | |||||||
| HITCBC | 137 | 1410360 | bool push(void* p, std::size_t capacity) noexcept | 139 | 1410499 | bool push(void* p, std::size_t capacity) noexcept | ||
| 138 | { | 140 | { | |||||
| HITCBC | 139 | 1410360 | if(count >= capacity) | 141 | 1410499 | if(count >= capacity) | ||
| HITCBC | 140 | 473705 | return false; | 142 | 473705 | return false; | ||
| HITCBC | 141 | 936655 | head = ::new(p) node{head}; | 143 | 936794 | head = ::new(p) node{head}; | ||
| HITCBC | 142 | 936655 | ++count; | 144 | 936794 | ++count; | ||
| HITCBC | 143 | 936655 | return true; | 145 | 936794 | return true; | ||
| 144 | } | 146 | } | |||||
| 145 | }; | 147 | }; | |||||
| 146 | 148 | |||||||
| 147 | struct pool | 149 | struct pool | |||||
| 148 | { | 150 | { | |||||
| 149 | bucket buckets[num_classes]; | 151 | bucket buckets[num_classes]; | |||||
| 150 | std::size_t arrivals = 0; | 152 | std::size_t arrivals = 0; | |||||
| 151 | 153 | |||||||
| 152 | // No destructor: a non-trivial dtor forces a guard variable on the | 154 | // No destructor: a non-trivial dtor forces a guard variable on the | |||||
| 153 | // thread_local in local(), checked on every alloc/free. Constant | 155 | // thread_local in local(), checked on every alloc/free. Constant | |||||
| 154 | // initialization plus a trivial dtor makes that access a bare TLS | 156 | // initialization plus a trivial dtor makes that access a bare TLS | |||||
| 155 | // load. Cached blocks are instead reclaimed explicitly: per-thread | 157 | // load. Cached blocks are instead reclaimed explicitly: per-thread | |||||
| 156 | // by arm_thread_cleanup() at thread exit, and the global pool by | 158 | // by arm_thread_cleanup() at thread exit, and the global pool by | |||||
| 157 | // global()'s holder destructor at process exit. | 159 | // global()'s holder destructor at process exit. | |||||
| 158 | }; | 160 | }; | |||||
| 159 | 161 | |||||||
| 160 | #if (defined(BOOST_CAPY_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && \ | 162 | #if (defined(BOOST_CAPY_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && \ | |||||
| 161 | !defined(BOOST_CAPY_STATIC_LINK) | 163 | !defined(BOOST_CAPY_STATIC_LINK) | |||||
| 162 | // A shared build defines this thread's pool, and the flag below, in | 164 | // A shared build defines this thread's pool, and the flag below, in | |||||
| 163 | // the library: inline, each module would cache in a pool of its own, | 165 | // the library: inline, each module would cache in a pool of its own, | |||||
| 164 | // which the library's thread-exit cleanup never drains. | 166 | // which the library's thread-exit cleanup never drains. | |||||
| 165 | static pool& local() noexcept; | 167 | static pool& local() noexcept; | |||||
| 166 | #else | 168 | #else | |||||
| 167 | static pool& local() noexcept | 169 | static pool& local() noexcept | |||||
| 168 | { | 170 | { | |||||
| 169 | static thread_local pool p; | 171 | static thread_local pool p; | |||||
| 170 | return p; | 172 | return p; | |||||
| 171 | } | 173 | } | |||||
| 172 | #endif | 174 | #endif | |||||
| 173 | 175 | |||||||
| 174 | static pool& global() noexcept; | 176 | static pool& global() noexcept; | |||||
| 175 | static std::mutex& global_mutex() noexcept; | 177 | static std::mutex& global_mutex() noexcept; | |||||
| 176 | 178 | |||||||
| 177 | void* allocate_slow(std::size_t rounded, std::size_t idx); | 179 | void* allocate_slow(std::size_t rounded, std::size_t idx); | |||||
| 178 | void deallocate_slow(void* p, std::size_t idx); | 180 | void deallocate_slow(void* p, std::size_t idx); | |||||
| 179 | 181 | |||||||
| 180 | // Move half of each bucket's idle blocks to the global pool, or | 182 | // Move half of each bucket's idle blocks to the global pool, or | |||||
| 181 | // release them when it has no room. Halving, rather than emptying, | 183 | // release them when it has no room. Halving, rather than emptying, | |||||
| 182 | // leaves a burst that skips one period half its blocks. | 184 | // leaves a burst that skips one period half its blocks. | |||||
| 183 | static void decay(pool& lp) noexcept; | 185 | static void decay(pool& lp) noexcept; | |||||
| 184 | 186 | |||||||
| 185 | // Register a thread-exit callback that drains this thread's local | 187 | // Register a thread-exit callback that drains this thread's local | |||||
| 186 | // pool back to the OS. Called only off the hot path: unconditionally | 188 | // pool back to the OS. Called only off the hot path: unconditionally | |||||
| 187 | // from the slow paths, and from deallocate_fast while the flag below | 189 | // from the slow paths, and from deallocate_fast while the flag below | |||||
| 188 | // is clear. Returns false once that callback has run: nothing drains | 190 | // is clear. Returns false once that callback has run: nothing drains | |||||
| 189 | // the local pool after it, so frees must bypass it. | 191 | // the local pool after it, so frees must bypass it. | |||||
| 190 | static bool arm_thread_cleanup() noexcept; | 192 | static bool arm_thread_cleanup() noexcept; | |||||
| 191 | 193 | |||||||
| 192 | // Guard-free flag (constinit bool, trivial dtor): set once cleanup is | 194 | // Guard-free flag (constinit bool, trivial dtor): set once cleanup is | |||||
| 193 | // armed, and cleared again when the cleanup has run. | 195 | // armed, and cleared again when the cleanup has run. | |||||
| 194 | #if (defined(BOOST_CAPY_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && \ | 196 | #if (defined(BOOST_CAPY_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && \ | |||||
| 195 | !defined(BOOST_CAPY_STATIC_LINK) | 197 | !defined(BOOST_CAPY_STATIC_LINK) | |||||
| 196 | static bool& thread_armed() noexcept; | 198 | static bool& thread_armed() noexcept; | |||||
| 197 | #else | 199 | #else | |||||
| 198 | static bool& thread_armed() noexcept | 200 | static bool& thread_armed() noexcept | |||||
| 199 | { | 201 | { | |||||
| 200 | static thread_local bool armed = false; | 202 | static thread_local bool armed = false; | |||||
| 201 | return armed; | 203 | return armed; | |||||
| 202 | } | 204 | } | |||||
| 203 | #endif | 205 | #endif | |||||
| 204 | 206 | |||||||
| 205 | public: | 207 | public: | |||||
| 206 | /** Destroy the resource. | 208 | /** Destroy the resource. | |||||
| 207 | 209 | |||||||
| 208 | No cached block is released here. Every pool is static, so an | 210 | No cached block is released here. Every pool is static, so an | |||||
| 209 | instance holds no state of its own. The thread-local pool is | 211 | instance holds no state of its own. The thread-local pool is | |||||
| 210 | drained at thread exit, and the global pool at process exit. | 212 | drained at thread exit, and the global pool at process exit. | |||||
| 211 | */ | 213 | */ | |||||
| 212 | ~recycling_memory_resource(); | 214 | ~recycling_memory_resource(); | |||||
| 213 | 215 | |||||||
| 214 | /** Allocate without virtual dispatch. | 216 | /** Allocate without virtual dispatch. | |||||
| 215 | - | Handles the fast path inline (thread-local bucket pop) | ||||||
| 216 | - | and falls through to the slow path for global pool or | ||||||
| 217 | - | heap allocation. | ||||||
| 218 | - | |||||||
| 219 | 217 | |||||||
| 220 | A request larger than the largest size class (2048 bytes) | 218 | A request larger than the largest size class (2048 bytes) | |||||
| 221 | bypasses the pools and goes straight to `::operator new`. | 219 | bypasses the pools and goes straight to `::operator new`. | |||||
| 222 | 220 | |||||||
| 223 | The second parameter is the requested alignment, and it is ignored. | 221 | The second parameter is the requested alignment, and it is ignored. | |||||
| 224 | Every block comes from `::operator new`, so blocks carry the | 222 | Every block comes from `::operator new`, so blocks carry the | |||||
| 225 | implementation's default new alignment and no more. | 223 | implementation's default new alignment and no more. | |||||
| 226 | 224 | |||||||
| 227 | @par Complexity | 225 | @par Complexity | |||||
| 228 | Amortized constant. Now and then a call also hands this thread's | 226 | Amortized constant. Now and then a call also hands this thread's | |||||
| 229 | idle cached blocks to the global pool, or releases them when it | 227 | idle cached blocks to the global pool, or releases them when it | |||||
| 230 | has no room. | 228 | has no room. | |||||
| 231 | 229 | |||||||
| 232 | - | @param bytes The number of bytes to allocate. | 230 | + | A pooled block is rounded up to its size class, so it may be | |||
| 233 | - | 231 | + | larger than requested. | ||||
| 234 | - | @return A pointer to a block of at least `bytes` bytes. A pooled | ||||||
| 235 | - | block is rounded up to its size class, so it may be larger than | ||||||
| 236 | - | requested. | ||||||
| 237 | 232 | |||||||
| 238 | @throws std::bad_alloc If the underlying `::operator new` fails. | 233 | @throws std::bad_alloc If the underlying `::operator new` fails. | |||||
| 234 | + | |||||||
| 235 | + | @param bytes The number of bytes to allocate. | ||||||
| 236 | + | |||||||
| 237 | + | @return A pointer to a block of at least `bytes` bytes. | ||||||
| 239 | */ | 238 | */ | |||||
| 240 | void* | 239 | void* | |||||
| HITCBC | 241 | 1143980 | allocate_fast(std::size_t bytes, std::size_t) | 240 | 1144119 | allocate_fast(std::size_t bytes, std::size_t) | ||
| 242 | { | 241 | { | |||||
| HITCBC | 243 | 1143980 | std::size_t rounded = round_up_pow2(bytes); | 242 | 1144119 | std::size_t rounded = round_up_pow2(bytes); | ||
| HITCBC | 244 | 1143980 | std::size_t idx = get_class_index(rounded); | 243 | 1144119 | std::size_t idx = get_class_index(rounded); | ||
| HITCBC | 245 | 1143980 | if(idx >= num_classes) | 244 | 1144119 | if(idx >= num_classes) | ||
| MISUBC | 246 | ✗ | return ::operator new(bytes); | 245 | ✗ | return ::operator new(bytes); | ||
| HITCBC | 247 | 1143980 | if(auto* p = local().buckets[idx].pop()) | 246 | 1144119 | if(auto* p = local().buckets[idx].pop()) | ||
| HITCBC | 248 | 841536 | return p; | 247 | 841675 | return p; | ||
| HITCBC | 249 | 302444 | return allocate_slow(rounded, idx); | 248 | 302444 | return allocate_slow(rounded, idx); | ||
| 250 | } | 249 | } | |||||
| 251 | 250 | |||||||
| 252 | /** Deallocate without virtual dispatch. | 251 | /** Deallocate without virtual dispatch. | |||||
| 253 | - | Handles the fast path inline (thread-local bucket push) | ||||||
| 254 | - | and falls through to the slow path for global pool or | ||||||
| 255 | - | heap deallocation. | ||||||
| 256 | - | |||||||
| 257 | 252 | |||||||
| 258 | The block is cached in the pool of the thread that frees it, not | 253 | The block is cached in the pool of the thread that frees it, not | |||||
| 259 | the thread that allocated it. | 254 | the thread that allocated it. | |||||
| 260 | 255 | |||||||
| 261 | The third parameter is the alignment the block was allocated with, | 256 | The third parameter is the alignment the block was allocated with, | |||||
| 262 | and it is ignored, as it is on allocation. | 257 | and it is ignored, as it is on allocation. | |||||
| 263 | 258 | |||||||
| 264 | @par Complexity | 259 | @par Complexity | |||||
| 265 | Amortized constant. Now and then a call also hands this thread's | 260 | Amortized constant. Now and then a call also hands this thread's | |||||
| 266 | idle cached blocks to the global pool, or releases them when it | 261 | idle cached blocks to the global pool, or releases them when it | |||||
| 267 | has no room. | 262 | has no room. | |||||
| 268 | 263 | |||||||
| 269 | @param p The block to return. It must have come from | 264 | @param p The block to return. It must have come from | |||||
| 270 | @ref allocate_fast or @ref do_allocate on this resource. | 265 | @ref allocate_fast or @ref do_allocate on this resource. | |||||
| 271 | 266 | |||||||
| 272 | @param bytes The size the block was allocated with. The size class | 267 | @param bytes The size the block was allocated with. The size class | |||||
| 273 | is recomputed from it, so passing a different value puts the block | 268 | is recomputed from it, so passing a different value puts the block | |||||
| 274 | in the wrong bucket. | 269 | in the wrong bucket. | |||||
| 275 | */ | 270 | */ | |||||
| 276 | void | 271 | void | |||||
| HITCBC | 277 | 1143980 | deallocate_fast(void* p, std::size_t bytes, std::size_t) | 272 | 1144119 | deallocate_fast(void* p, std::size_t bytes, std::size_t) | ||
| 278 | { | 273 | { | |||||
| HITCBC | 279 | 1143980 | std::size_t rounded = round_up_pow2(bytes); | 274 | 1144119 | std::size_t rounded = round_up_pow2(bytes); | ||
| HITCBC | 280 | 1143980 | std::size_t idx = get_class_index(rounded); | 275 | 1144119 | std::size_t idx = get_class_index(rounded); | ||
| HITCBC | 281 | 1143980 | if(idx >= num_classes) | 276 | 1144119 | if(idx >= num_classes) | ||
| 282 | { | 277 | { | |||||
| MISUBC | 283 | ✗ | ::operator delete(p); | 278 | ✗ | ::operator delete(p); | ||
| MISUBC | 284 | ✗ | return; | 279 | ✗ | return; | ||
| 285 | } | 280 | } | |||||
| 286 | // Arms thread-exit cleanup for any thread that caches via | 281 | // Arms thread-exit cleanup for any thread that caches via | |||||
| 287 | // deallocate, including consumer threads that never hit a slow | 282 | // deallocate, including consumer threads that never hit a slow | |||||
| 288 | // path; after that cleanup, the block bypasses the local pool. | 283 | // path; after that cleanup, the block bypasses the local pool. | |||||
| HITCBC | 289 | 1143980 | auto& armed = thread_armed(); | 284 | 1144119 | auto& armed = thread_armed(); | ||
| HITCBC | 290 | 1143980 | if(!armed) | 285 | 1144119 | if(!armed) | ||
| 291 | { | 286 | { | |||||
| HITCBC | 292 | 375 | if(!arm_thread_cleanup()) | 287 | 375 | if(!arm_thread_cleanup()) | ||
| 293 | { | 288 | { | |||||
| HITCBC | 294 | 4 | deallocate_slow(p, idx); | 289 | 4 | deallocate_slow(p, idx); | ||
| HITCBC | 295 | 4 | return; | 290 | 4 | return; | ||
| 296 | } | 291 | } | |||||
| HITCBC | 297 | 371 | armed = true; | 292 | 371 | armed = true; | ||
| 298 | } | 293 | } | |||||
| HITCBC | 299 | 1143976 | auto& lp = local(); | 294 | 1144115 | auto& lp = local(); | ||
| HITCBC | 300 | 1143976 | if(++lp.arrivals == decay_period) | 295 | 1144115 | if(++lp.arrivals == decay_period) | ||
| HITCBC | 301 | 272 | decay(lp); | 296 | 272 | decay(lp); | ||
| HITCBC | 302 | 1143976 | if(lp.buckets[idx].push(p, bucket_capacity(idx))) | 297 | 1144115 | if(lp.buckets[idx].push(p, bucket_capacity(idx))) | ||
| HITCBC | 303 | 877596 | return; | 298 | 877735 | return; | ||
| HITCBC | 304 | 266380 | deallocate_slow(p, idx); | 299 | 266380 | deallocate_slow(p, idx); | ||
| 305 | } | 300 | } | |||||
| 306 | 301 | |||||||
| 307 | protected: | 302 | protected: | |||||
| 308 | /** Allocate through the `std::pmr::memory_resource` interface. | 303 | /** Allocate through the `std::pmr::memory_resource` interface. | |||||
| 309 | 304 | |||||||
| 310 | Forwards to @ref allocate_fast, so it has that function's contract. | 305 | Forwards to @ref allocate_fast, so it has that function's contract. | |||||
| 311 | Call `allocate_fast` directly to skip the virtual dispatch. | 306 | Call `allocate_fast` directly to skip the virtual dispatch. | |||||
| 312 | 307 | |||||||
| 308 | + | @throws std::bad_alloc If the underlying `::operator new` fails. | ||||||
| 309 | + | |||||||
| 313 | @param bytes The number of bytes to allocate. | 310 | @param bytes The number of bytes to allocate. | |||||
| 314 | 311 | |||||||
| 315 | @param alignment The requested alignment. It is ignored. | 312 | @param alignment The requested alignment. It is ignored. | |||||
| 316 | 313 | |||||||
| 317 | - | |||||||
| 318 | - | @throws std::bad_alloc If the underlying `::operator new` fails. | ||||||
| 319 | @return A pointer to a block of at least `bytes` bytes. | 314 | @return A pointer to a block of at least `bytes` bytes. | |||||
| 320 | */ | 315 | */ | |||||
| 321 | void* | 316 | void* | |||||
| 322 | do_allocate(std::size_t bytes, std::size_t alignment) override; | 317 | do_allocate(std::size_t bytes, std::size_t alignment) override; | |||||
| 323 | 318 | |||||||
| 324 | /** Deallocate through the `std::pmr::memory_resource` interface. | 319 | /** Deallocate through the `std::pmr::memory_resource` interface. | |||||
| 325 | 320 | |||||||
| 326 | Forwards to @ref deallocate_fast, so it has that function's | 321 | Forwards to @ref deallocate_fast, so it has that function's | |||||
| 327 | contract. | 322 | contract. | |||||
| 328 | 323 | |||||||
| 329 | @param p The block to return, as obtained from this resource. | 324 | @param p The block to return, as obtained from this resource. | |||||
| 330 | 325 | |||||||
| 331 | @param bytes The size the block was allocated with. | 326 | @param bytes The size the block was allocated with. | |||||
| 332 | 327 | |||||||
| 333 | @param alignment The alignment the block was allocated with. It is | 328 | @param alignment The alignment the block was allocated with. It is | |||||
| 334 | ignored. | 329 | ignored. | |||||
| 335 | */ | 330 | */ | |||||
| 336 | void | 331 | void | |||||
| 337 | do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override; | 332 | do_deallocate(void* p, std::size_t bytes, std::size_t alignment) override; | |||||
| 338 | 333 | |||||||
| 339 | /** Compare this resource with another for equality. | 334 | /** Compare this resource with another for equality. | |||||
| 340 | 335 | |||||||
| 341 | Equality is object identity: two distinct | 336 | Equality is object identity: two distinct | |||||
| 342 | `recycling_memory_resource` objects compare unequal, even though the | 337 | `recycling_memory_resource` objects compare unequal, even though the | |||||
| 343 | pools they draw from are static and therefore shared. | 338 | pools they draw from are static and therefore shared. | |||||
| 344 | 339 | |||||||
| 345 | @param other The resource to compare against. | 340 | @param other The resource to compare against. | |||||
| 346 | 341 | |||||||
| 347 | @return `true` if `other` is the same object as `*this`; otherwise | 342 | @return `true` if `other` is the same object as `*this`; otherwise | |||||
| 348 | `false`. | 343 | `false`. | |||||
| 349 | */ | 344 | */ | |||||
| 350 | bool | 345 | bool | |||||
| HITCBC | 351 | 2 | do_is_equal(const memory_resource& other) const noexcept override | 346 | 2 | do_is_equal(const memory_resource& other) const noexcept override | ||
| 352 | { | 347 | { | |||||
| HITCBC | 353 | 2 | return this == &other; | 348 | 2 | return this == &other; | ||
| 354 | } | 349 | } | |||||
| 355 | }; | 350 | }; | |||||
| 356 | BOOST_CAPY_MSVC_WARNING_POP | 351 | BOOST_CAPY_MSVC_WARNING_POP | |||||
| 357 | 352 | |||||||
| 358 | - | /** Returns pointer to the default recycling memory resource. | 353 | + | /** Return a pointer to the process-wide recycling memory resource. | |||
| 359 | 354 | |||||||
| 360 | The returned pointer is valid for the lifetime of the program. | 355 | The returned pointer is valid for the lifetime of the program. | |||||
| 361 | - | This is the default allocator used by run_async. | 356 | + | This is the default frame allocator of an @ref execution_context. | |||
| 362 | 357 | |||||||
| 363 | @return Pointer to the recycling memory resource. | 358 | @return Pointer to the recycling memory resource. | |||||
| 364 | 359 | |||||||
| 365 | @see @ref recycling_memory_resource | 360 | @see @ref recycling_memory_resource | |||||
| 366 | @see @ref run_async | 361 | @see @ref run_async | |||||
| 367 | */ | 362 | */ | |||||
| 368 | BOOST_CAPY_DECL | 363 | BOOST_CAPY_DECL | |||||
| 369 | std::pmr::memory_resource* | 364 | std::pmr::memory_resource* | |||||
| 370 | get_recycling_memory_resource() noexcept; | 365 | get_recycling_memory_resource() noexcept; | |||||
| 371 | 366 | |||||||
| 372 | } // namespace capy | 367 | } // namespace capy | |||||
| 373 | } // namespace boost | 368 | } // namespace boost | |||||
| 374 | 369 | |||||||
| 375 | #endif | 370 | #endif | |||||