100.00% Lines (26/26) 100.00% Functions (4/4)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2026 Michael Vandeberg 2   // Copyright (c) 2026 Michael Vandeberg
3   // 3   //
4   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 5   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6   // 6   //
7   // Official repository: https://github.com/cppalliance/capy 7   // Official repository: https://github.com/cppalliance/capy
8   // 8   //
9   9  
10   #ifndef BOOST_CAPY_EX_FRAME_ALLOC_MIXIN_HPP 10   #ifndef BOOST_CAPY_EX_FRAME_ALLOC_MIXIN_HPP
11   #define BOOST_CAPY_EX_FRAME_ALLOC_MIXIN_HPP 11   #define BOOST_CAPY_EX_FRAME_ALLOC_MIXIN_HPP
12   12  
13   #include <boost/capy/detail/config.hpp> 13   #include <boost/capy/detail/config.hpp>
14   #include <boost/capy/detail/frame_memory_resource.hpp> 14   #include <boost/capy/detail/frame_memory_resource.hpp>
15   #include <boost/capy/ex/frame_allocator.hpp> 15   #include <boost/capy/ex/frame_allocator.hpp>
16   #include <boost/capy/ex/recycling_memory_resource.hpp> 16   #include <boost/capy/ex/recycling_memory_resource.hpp>
17   17  
18   #include <cstddef> 18   #include <cstddef>
19   #include <cstring> 19   #include <cstring>
20   #include <memory_resource> 20   #include <memory_resource>
21   21  
22   namespace boost { 22   namespace boost {
23   namespace capy { 23   namespace capy {
24   24  
25 - /** Mixin that adds frame-allocator-aware allocation to a promise type. 25 + /** Allocate a promise's coroutine frames from the current frame allocator.
26   26  
27   Inherit from this class in any coroutine promise type to opt into 27   Inherit from this class in any coroutine promise type to opt into
28 - TLS-based frame allocation with the recycling memory resource 28 + TLS-based frame allocation. The mixin provides `operator new` and
29 - fast path. The mixin provides `operator new` and `operator delete` 29 + `operator delete` that:
30 - that:  
31   30  
32 - 1. Read the thread-local frame allocator set by `run_async` or `run`. 31 + 1. Allocate from the current frame allocator (see
33 - 2. Bypass virtual dispatch when the allocator is the default 32 + @ref get_current_frame_allocator), or from the default memory
34 - recycling memory resource. 33 + resource if none is set.
35 - 3. Store the allocator pointer at the end of each frame for 34 + 2. Deallocate through the same resource that allocated the frame,
36 - correct deallocation even when TLS changes between allocation 35 + even if the current frame allocator changed in between.
37 - and deallocation.  
38   36  
39   This is the same allocation strategy used by @ref 37   This is the same allocation strategy used by @ref
40   context_awaitable_promise_base. Use this mixin directly when your 38   context_awaitable_promise_base. Use this mixin directly when your
41   promise type does not need the full environment and continuation 39   promise type does not need the full environment and continuation
42   support that `context_awaitable_promise_base` provides. 40   support that `context_awaitable_promise_base` provides.
43   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 +
44   @par Example 50   @par Example
45   @par !example example 51   @par !example example
46   52  
47 - @par Thread Safety  
48 - The allocation fast path uses thread-local storage and requires  
49 - no synchronization. The global pool fallback is mutex-protected.  
50 -  
51   53  
52   @see @ref context_awaitable_promise_base, @ref recycling_memory_resource 54   @see @ref context_awaitable_promise_base, @ref recycling_memory_resource
53   */ 55   */
54   struct frame_alloc_mixin 56   struct frame_alloc_mixin
55   { 57   {
56   /** Allocate a coroutine frame. 58   /** Allocate a coroutine frame.
57   59  
58 - Uses the thread-local frame allocator set by run_async. 60 + Allocates from the current frame allocator, or from the
59 - Falls back to default memory resource if not set. 61 + default memory resource if none is set. Deallocation later
60 - Stores the allocator pointer at the end of each frame for 62 + uses the same resource, even if the current frame allocator
61 - correct deallocation even when TLS changes. Uses memcpy 63 + changed.
62 - to avoid alignment requirements on the trailing pointer.  
63 - Bypasses virtual dispatch for the recycling allocator.  
64 -  
65 - @param size The size, in bytes, of the coroutine frame.  
66 -  
67 - @return A pointer to storage for the frame.  
68   64  
69   @par Exception Safety 65   @par Exception Safety
70   Propagates any exception thrown by the underlying memory 66   Propagates any exception thrown by the underlying memory
71   resource's `allocate`, for example `std::bad_alloc` from 67   resource's `allocate`, for example `std::bad_alloc` from
72   `::operator new`. 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   */ 73   */
HITCBC 74   109270 static void* operator new(std::size_t size) 74   109366 static void* operator new(std::size_t size)
75   { 75   {
  76 + // Calling the recycling resource directly skips virtual dispatch
HITCBC 76   109270 static auto* const rmr = get_recycling_memory_resource(); 77   109366 static auto* const rmr = get_recycling_memory_resource();
77   78  
HITCBC 78   109270 auto* mr = get_current_frame_allocator(); 79   109366 auto* mr = get_current_frame_allocator();
HITCBC 79   109270 if(!mr) 80   109366 if(!mr)
HITCBC 80   2175 mr = std::pmr::get_default_resource(); 81   2176 mr = std::pmr::get_default_resource();
81   82  
HITCBC 82   109270 auto total = size + sizeof(std::pmr::memory_resource*); 83   109366 auto total = size + sizeof(std::pmr::memory_resource*);
83   void* raw; 84   void* raw;
HITCBC 84   109270 if(mr == rmr) 85   109366 if(mr == rmr)
85   raw = static_cast<recycling_memory_resource*>(mr) 86   raw = static_cast<recycling_memory_resource*>(mr)
HITCBC 86   103311 ->allocate_fast(total, detail::frame_alignment); 87   103398 ->allocate_fast(total, detail::frame_alignment);
87   else 88   else
HITCBC 88   5959 raw = mr->allocate(total, detail::frame_alignment); 89   5968 raw = mr->allocate(total, detail::frame_alignment);
  90 + // The trailing slot lets delete find the resource; memcpy avoids
  91 + // an alignment requirement on it
HITCBC 89   109270 std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr)); 92   109366 std::memcpy(static_cast<char*>(raw) + size, &mr, sizeof(mr));
HITCBC 90   109270 return raw; 93   109366 return raw;
91   } 94   }
92   95  
93   /** Deallocate a coroutine frame. 96   /** Deallocate a coroutine frame.
94   97  
95 - Reads the allocator pointer stored at the end of the frame 98 + Returns the storage to the resource that allocated it,
96 - to ensure correct deallocation regardless of current TLS. 99 + regardless of the current frame allocator.
97 - Bypasses virtual dispatch for the recycling allocator.  
98   100  
99   @param ptr The frame storage returned by `operator new`. 101   @param ptr The frame storage returned by `operator new`.
100   102  
101   @param size The size, in bytes, that was passed to `operator new`. 103   @param size The size, in bytes, that was passed to `operator new`.
102 - The allocator pointer is read from `ptr + size`, which is where 104 + This value must match.
103 - `operator new` wrote it, so this value must match.  
104   */ 105   */
HITCBC 105   109270 static void operator delete(void* ptr, std::size_t size) noexcept 106   109366 static void operator delete(void* ptr, std::size_t size) noexcept
106   { 107   {
HITCBC 107   109270 static auto* const rmr = get_recycling_memory_resource(); 108   109366 static auto* const rmr = get_recycling_memory_resource();
108   109  
109   std::pmr::memory_resource* mr; 110   std::pmr::memory_resource* mr;
HITCBC 110   109270 std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr)); 111   109366 std::memcpy(&mr, static_cast<char*>(ptr) + size, sizeof(mr));
HITCBC 111   109270 auto total = size + sizeof(std::pmr::memory_resource*); 112   109366 auto total = size + sizeof(std::pmr::memory_resource*);
HITCBC 112   109270 if(mr == rmr) 113   109366 if(mr == rmr)
113   static_cast<recycling_memory_resource*>(mr) 114   static_cast<recycling_memory_resource*>(mr)
HITCBC 114   103311 ->deallocate_fast(ptr, total, detail::frame_alignment); 115   103398 ->deallocate_fast(ptr, total, detail::frame_alignment);
115   else 116   else
HITCBC 116   5959 mr->deallocate(ptr, total, detail::frame_alignment); 117   5968 mr->deallocate(ptr, total, detail::frame_alignment);
HITCBC 117   109270 } 118   109366 }
118   }; 119   };
119   120  
120   namespace detail { 121   namespace detail {
121   122  
122   // For capy's own frames whose teardown can release the chain that owns 123   // For capy's own frames whose teardown can release the chain that owns
123   // the frame allocator: they are never allocated from it, so freeing them 124   // the frame allocator: they are never allocated from it, so freeing them
124   // afterwards never reaches a resource that died with that chain. 125   // afterwards never reaches a resource that died with that chain.
125   struct process_frame_mixin 126   struct process_frame_mixin
126   { 127   {
HITCBC 127   420 static void* operator new(std::size_t size) 128   471 static void* operator new(std::size_t size)
128   { 129   {
129   return static_cast<recycling_memory_resource*>( 130   return static_cast<recycling_memory_resource*>(
HITCBC 130   420 get_recycling_memory_resource()) 131   471 get_recycling_memory_resource())
HITCBC 131   420 ->allocate_fast(size, detail::frame_alignment); 132   471 ->allocate_fast(size, detail::frame_alignment);
132   } 133   }
133   134  
HITCBC 134   420 static void operator delete(void* ptr, std::size_t size) noexcept 135   471 static void operator delete(void* ptr, std::size_t size) noexcept
135   { 136   {
136   static_cast<recycling_memory_resource*>( 137   static_cast<recycling_memory_resource*>(
HITCBC 137   420 get_recycling_memory_resource()) 138   471 get_recycling_memory_resource())
HITCBC 138   420 ->deallocate_fast(ptr, size, detail::frame_alignment); 139   471 ->deallocate_fast(ptr, size, detail::frame_alignment);
HITCBC 139   420 } 140   471 }
140   }; 141   };
141   142  
142   } // namespace detail 143   } // namespace detail
143   144  
144   } // namespace capy 145   } // namespace capy
145   } // namespace boost 146   } // namespace boost
146   147  
147   #endif 148   #endif