include/boost/capy/ex/frame_alloc_mixin.hpp
100.0% Lines (26 / 26)
100.0% Functions (4 / 4)
Functions (4)
Function
Calls
Lines
Blocks
boost::capy::frame_alloc_mixin::operator new(unsigned long)
:74
109366x
100.0%
100.0%
boost::capy::frame_alloc_mixin::operator delete(void*, unsigned long)
:106
109366x
100.0%
100.0%
boost::capy::detail::process_frame_mixin::operator new(unsigned long)
:128
471x
100.0%
100.0%
boost::capy::detail::process_frame_mixin::operator delete(void*, unsigned long)
:135
471x
100.0%
100.0%
| Line | TLA | Hits | Source Code |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2026 Michael Vandeberg | ||
| 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_EX_FRAME_ALLOC_MIXIN_HPP | ||
| 11 | #define BOOST_CAPY_EX_FRAME_ALLOC_MIXIN_HPP | ||
| 12 | |||
| 13 | #include <boost/capy/detail/config.hpp> | ||
| 14 | #include <boost/capy/detail/frame_memory_resource.hpp> | ||
| 15 | #include <boost/capy/ex/frame_allocator.hpp> | ||
| 16 | #include <boost/capy/ex/recycling_memory_resource.hpp> | ||
| 17 | |||
| 18 | #include <cstddef> | ||
| 19 | #include <cstring> | ||
| 20 | #include <memory_resource> | ||
| 21 | |||
| 22 | namespace boost { | ||
| 23 | namespace capy { | ||
| 24 | |||
| 25 | /** Allocate a promise's coroutine frames from the current frame allocator. | ||
| 26 | |||
| 27 | Inherit from this class in any coroutine promise type to opt into | ||
| 28 | TLS-based frame allocation. The mixin provides `operator new` and | ||
| 29 | `operator delete` that: | ||
| 30 | |||
| 31 | 1. Allocate from the current frame allocator (see | ||
| 32 | @ref get_current_frame_allocator), or from the default memory | ||
| 33 | resource if none is set. | ||
| 34 | 2. Deallocate through the same resource that allocated the frame, | ||
| 35 | even if the current frame allocator changed in between. | ||
| 36 | |||
| 37 | This is the same allocation strategy used by @ref | ||
| 38 | context_awaitable_promise_base. Use this mixin directly when your | ||
| 39 | promise type does not need the full environment and continuation | ||
| 40 | support that `context_awaitable_promise_base` provides. | ||
| 41 | |||
| 42 | @pre The resource that allocated a frame outlives that frame. Setting | ||
| 43 | a different current frame allocator does not extend this lifetime. | ||
| 44 | |||
| 45 | @par Thread Safety | ||
| 46 | Allocation and deallocation are as thread-safe as the memory | ||
| 47 | resource they use. The @ref recycling_memory_resource is safe to | ||
| 48 | use from any thread. | ||
| 49 | |||
| 50 | @par Example | ||
| 51 | @par !example example | ||
| 52 | |||
| 53 | |||
| 54 | @see @ref context_awaitable_promise_base, @ref recycling_memory_resource | ||
| 55 | */ | ||
| 56 | struct frame_alloc_mixin | ||
| 57 | { | ||
| 58 | /** Allocate a coroutine frame. | ||
| 59 | |||
| 60 | Allocates from the current frame allocator, or from the | ||
| 61 | default memory resource if none is set. Deallocation later | ||
| 62 | uses the same resource, even if the current frame allocator | ||
| 63 | changed. | ||
| 64 | |||
| 65 | @par Exception Safety | ||
| 66 | Propagates any exception thrown by the underlying memory | ||
| 67 | resource's `allocate`, for example `std::bad_alloc` from | ||
| 68 | `::operator new`. | ||
| 69 | |||
| 70 | @param size The size, in bytes, of the coroutine frame. | ||
| 71 | |||
| 72 | @return A pointer to storage for the frame. | ||
| 73 | */ | ||
| 74 | 109366x | static void* operator new(std::size_t size) | |
| 75 | { | ||
| 76 | // Calling the recycling resource directly skips virtual dispatch | ||
| 77 | 109366x | static auto* const rmr = get_recycling_memory_resource(); | |
| 78 | |||
| 79 | 109366x | auto* mr = get_current_frame_allocator(); | |
| 80 | 109366x | if(!mr) | |
| 81 | 2176x | mr = std::pmr::get_default_resource(); | |
| 82 | |||
| 83 | 109366x | auto total = size + sizeof(std::pmr::memory_resource*); | |
| 84 | void* raw; | ||
| 85 | 109366x | if(mr == rmr) | |
| 86 | raw = static_cast<recycling_memory_resource*>(mr) | ||
| 87 | 103398x | ->allocate_fast(total, detail::frame_alignment); | |
| 88 | else | ||
| 89 | 5968x | raw = mr->allocate(total, detail::frame_alignment); | |
| 90 | // The trailing slot lets delete find the resource; memcpy avoids | ||
| 91 | // an alignment requirement on it | ||
| 92 | 109366x | std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr)); | |
| 93 | 109366x | return raw; | |
| 94 | } | ||
| 95 | |||
| 96 | /** Deallocate a coroutine frame. | ||
| 97 | |||
| 98 | Returns the storage to the resource that allocated it, | ||
| 99 | regardless of the current frame allocator. | ||
| 100 | |||
| 101 | @param ptr The frame storage returned by `operator new`. | ||
| 102 | |||
| 103 | @param size The size, in bytes, that was passed to `operator new`. | ||
| 104 | This value must match. | ||
| 105 | */ | ||
| 106 | 109366x | static void operator delete(void* ptr, std::size_t size) noexcept | |
| 107 | { | ||
| 108 | 109366x | static auto* const rmr = get_recycling_memory_resource(); | |
| 109 | |||
| 110 | std::pmr::memory_resource* mr; | ||
| 111 | 109366x | std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr)); | |
| 112 | 109366x | auto total = size + sizeof(std::pmr::memory_resource*); | |
| 113 | 109366x | if(mr == rmr) | |
| 114 | static_cast<recycling_memory_resource*>(mr) | ||
| 115 | 103398x | ->deallocate_fast(ptr, total, detail::frame_alignment); | |
| 116 | else | ||
| 117 | 5968x | mr->deallocate(ptr, total, detail::frame_alignment); | |
| 118 | 109366x | } | |
| 119 | }; | ||
| 120 | |||
| 121 | namespace detail { | ||
| 122 | |||
| 123 | // For capy's own frames whose teardown can release the chain that owns | ||
| 124 | // the frame allocator: they are never allocated from it, so freeing them | ||
| 125 | // afterwards never reaches a resource that died with that chain. | ||
| 126 | struct process_frame_mixin | ||
| 127 | { | ||
| 128 | 471x | static void* operator new(std::size_t size) | |
| 129 | { | ||
| 130 | return static_cast<recycling_memory_resource*>( | ||
| 131 | 471x | get_recycling_memory_resource()) | |
| 132 | 471x | ->allocate_fast(size, detail::frame_alignment); | |
| 133 | } | ||
| 134 | |||
| 135 | 471x | static void operator delete(void* ptr, std::size_t size) noexcept | |
| 136 | { | ||
| 137 | static_cast<recycling_memory_resource*>( | ||
| 138 | 471x | get_recycling_memory_resource()) | |
| 139 | 471x | ->deallocate_fast(ptr, size, detail::frame_alignment); | |
| 140 | 471x | } | |
| 141 | }; | ||
| 142 | |||
| 143 | } // namespace detail | ||
| 144 | |||
| 145 | } // namespace capy | ||
| 146 | } // namespace boost | ||
| 147 | |||
| 148 | #endif | ||
| 149 |