100.00% Lines (130/130) 100.00% Functions (25/25)
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_IO_ANY_WRITE_STREAM_HPP 11   #ifndef BOOST_CAPY_IO_ANY_WRITE_STREAM_HPP
12   #define BOOST_CAPY_IO_ANY_WRITE_STREAM_HPP 12   #define BOOST_CAPY_IO_ANY_WRITE_STREAM_HPP
13   13  
14   #include <boost/capy/detail/config.hpp> 14   #include <boost/capy/detail/config.hpp>
15   #include <boost/capy/detail/await_suspend_helper.hpp> 15   #include <boost/capy/detail/await_suspend_helper.hpp>
16   #include <boost/capy/buffers.hpp> 16   #include <boost/capy/buffers.hpp>
17   #include <boost/capy/detail/buffer_array.hpp> 17   #include <boost/capy/detail/buffer_array.hpp>
18   #include <boost/capy/concept/context_awaitable.hpp> 18   #include <boost/capy/concept/context_awaitable.hpp>
19   #include <boost/capy/concept/write_stream.hpp> 19   #include <boost/capy/concept/write_stream.hpp>
20   #include <coroutine> 20   #include <coroutine>
21   #include <boost/capy/ex/await_context.hpp> 21   #include <boost/capy/ex/await_context.hpp>
22   #include <boost/capy/results.hpp> 22   #include <boost/capy/results.hpp>
23   23  
24   #include <concepts> 24   #include <concepts>
25   #include <coroutine> 25   #include <coroutine>
26   #include <cstddef> 26   #include <cstddef>
27   #include <exception> 27   #include <exception>
28   #include <new> 28   #include <new>
29   #include <span> 29   #include <span>
30   #include <stop_token> 30   #include <stop_token>
31   #include <system_error> 31   #include <system_error>
32   #include <utility> 32   #include <utility>
33   33  
34   namespace boost { 34   namespace boost {
35   namespace capy { 35   namespace capy {
36   36  
37   /** Dispatches `write_some` through a type-erased vtable, using preallocated awaitable storage. 37   /** Dispatches `write_some` through a type-erased vtable, using preallocated awaitable storage.
38   38  
39   This class provides type erasure for any type satisfying the 39   This class provides type erasure for any type satisfying the
40   @ref WriteStream concept, enabling runtime polymorphism for 40   @ref WriteStream concept, enabling runtime polymorphism for
41   write operations. It uses cached awaitable storage to achieve 41   write operations. It uses cached awaitable storage to achieve
42   zero steady-state allocation after construction. 42   zero steady-state allocation after construction.
43   43  
44   The wrapper supports two construction modes: 44   The wrapper supports two construction modes:
45   - **Owning**: Pass by value to transfer ownership. The wrapper 45   - **Owning**: Pass by value to transfer ownership. The wrapper
46   allocates storage and owns the stream. 46   allocates storage and owns the stream.
47   - **Reference**: Pass a pointer to wrap without ownership. The 47   - **Reference**: Pass a pointer to wrap without ownership. The
48   pointed-to stream must outlive this wrapper. 48   pointed-to stream must outlive this wrapper.
49   49  
50   @par Awaitable Preallocation 50   @par Awaitable Preallocation
51 - The constructor preallocates storage for the type-erased awaitable. 51 + The constructor allocates the storage for the type-erased awaitable
52 - This reserves all virtual address space at server startup 52 + once, so operations do not allocate it piecemeal as traffic
53 - so memory usage can be measured up front, rather than 53 + arrives.
54 - allocating piecemeal as traffic arrives.  
55   54  
56   @par Immediate Completion 55   @par Immediate Completion
57   Operations complete immediately without suspending when the 56   Operations complete immediately without suspending when the
58   buffer sequence is empty, or when the underlying stream's 57   buffer sequence is empty, or when the underlying stream's
59   awaitable reports readiness via `await_ready`. 58   awaitable reports readiness via `await_ready`.
60   59  
61   @par Thread Safety 60   @par Thread Safety
62   Not thread-safe. Concurrent operations on the same wrapper 61   Not thread-safe. Concurrent operations on the same wrapper
63   are undefined behavior. 62   are undefined behavior.
64   63  
65   @par Example 64   @par Example
66   @par !example example 65   @par !example example
67   66  
68   67  
69   @see @ref any_read_stream, @ref any_stream, @ref WriteStream 68   @see @ref any_read_stream, @ref any_stream, @ref WriteStream
70   */ 69   */
71   class any_write_stream 70   class any_write_stream
72   { 71   {
73   struct vtable; 72   struct vtable;
74   73  
75   template<WriteStream S> 74   template<WriteStream S>
76   struct vtable_for_impl; 75   struct vtable_for_impl;
77   76  
78   // ordered for cache line coherence 77   // ordered for cache line coherence
79   void* stream_ = nullptr; 78   void* stream_ = nullptr;
80   vtable const* vt_ = nullptr; 79   vtable const* vt_ = nullptr;
81   void* cached_awaitable_ = nullptr; 80   void* cached_awaitable_ = nullptr;
82   void* storage_ = nullptr; 81   void* storage_ = nullptr;
83   bool awaitable_active_ = false; 82   bool awaitable_active_ = false;
84   // The flag of the awaitable whose operation is active, cleared 83   // The flag of the awaitable whose operation is active, cleared
85   // when the stream ends that operation itself. 84   // when the stream ends that operation itself.
86   bool* owner_ = nullptr; 85   bool* owner_ = nullptr;
87   86  
88   void 87   void
HITCBC 89   208 disown() noexcept 88   208 disown() noexcept
90   { 89   {
HITCBC 91   208 if(owner_) 90   208 if(owner_)
HITCBC 92   6 *std::exchange(owner_, nullptr) = false; 91   6 *std::exchange(owner_, nullptr) = false;
HITCBC 93   208 } 92   208 }
94   93  
95   public: 94   public:
96 - /** Destructor. 95 + /** Destroy the wrapper.
97   96  
98   Ends any operation in flight, then destroys the owned stream 97   Ends any operation in flight, then destroys the owned stream
99   (if any) and releases the cached awaitable storage. 98   (if any) and releases the cached awaitable storage.
100   */ 99   */
101   ~any_write_stream(); 100   ~any_write_stream();
102   101  
103   /** Construct a default instance. 102   /** Construct a default instance.
104   103  
105   Constructs an empty wrapper. @ref has_value and `operator bool` 104   Constructs an empty wrapper. @ref has_value and `operator bool`
106   report the empty state; calling @ref write_some before the 105   report the empty state; calling @ref write_some before the
107   wrapper holds a stream is undefined behavior. 106   wrapper holds a stream is undefined behavior.
108   */ 107   */
HITCBC 109   10 any_write_stream() = default; 108   10 any_write_stream() = default;
110   109  
111 - /** Non-copyable. 110 + /** Copy construction is disabled.
112   111  
113   The awaitable cache is per-instance and cannot be shared. 112   The awaitable cache is per-instance and cannot be shared.
114   113  
115   @param other The wrapper that would be copied. 114   @param other The wrapper that would be copied.
116   */ 115   */
117   any_write_stream(any_write_stream const& other) = delete; 116   any_write_stream(any_write_stream const& other) = delete;
118   117  
119   /** Copy assignment is disabled. 118   /** Copy assignment is disabled.
120   119  
121   The awaitable cache is per-instance and cannot be shared. 120   The awaitable cache is per-instance and cannot be shared.
122   121  
123   @param other The wrapper that would be assigned from. 122   @param other The wrapper that would be assigned from.
124   123  
125   @return A reference to `*this`. 124   @return A reference to `*this`.
126   */ 125   */
127   any_write_stream& operator=(any_write_stream const& other) = delete; 126   any_write_stream& operator=(any_write_stream const& other) = delete;
128   127  
129   /** Construct by moving. 128   /** Construct by moving.
130   129  
131   Transfers ownership of the wrapped stream (if owned) and 130   Transfers ownership of the wrapped stream (if owned) and
132   cached awaitable storage from `other`. After the move, `other` is 131   cached awaitable storage from `other`. After the move, `other` is
133   in a default-constructed state. 132   in a default-constructed state.
134   133  
135   @param other The wrapper to move from. 134   @param other The wrapper to move from.
136   */ 135   */
HITCBC 137   2 any_write_stream(any_write_stream&& other) noexcept 136   2 any_write_stream(any_write_stream&& other) noexcept
HITCBC 138   2 : stream_(std::exchange(other.stream_, nullptr)) 137   2 : stream_(std::exchange(other.stream_, nullptr))
HITCBC 139   2 , vt_(std::exchange(other.vt_, nullptr)) 138   2 , vt_(std::exchange(other.vt_, nullptr))
HITCBC 140   2 , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr)) 139   2 , cached_awaitable_(std::exchange(other.cached_awaitable_, nullptr))
HITCBC 141   2 , storage_(std::exchange(other.storage_, nullptr)) 140   2 , storage_(std::exchange(other.storage_, nullptr))
HITCBC 142   2 , awaitable_active_(std::exchange(other.awaitable_active_, false)) 141   2 , awaitable_active_(std::exchange(other.awaitable_active_, false))
143   { 142   {
HITCBC 144   2 other.disown(); 143   2 other.disown();
HITCBC 145   2 } 144   2 }
146   145  
147   /** Assign by moving. 146   /** Assign by moving.
148   147  
149   Destroys any owned stream and releases existing resources, 148   Destroys any owned stream and releases existing resources,
150   then transfers ownership from `other`. 149   then transfers ownership from `other`.
151   150  
152   @param other The wrapper to move from. 151   @param other The wrapper to move from.
153   @return Reference to this wrapper. 152   @return Reference to this wrapper.
154   */ 153   */
155   any_write_stream& 154   any_write_stream&
156   operator=(any_write_stream&& other) noexcept; 155   operator=(any_write_stream&& other) noexcept;
157   156  
158   /** Construct by taking ownership of a WriteStream. 157   /** Construct by taking ownership of a WriteStream.
159   158  
160   Allocates storage and moves the stream into this wrapper. 159   Allocates storage and moves the stream into this wrapper.
161   The wrapper owns the stream and destroys it. 160   The wrapper owns the stream and destroys it.
162   161  
  162 + @par Exception Safety
  163 + Strong guarantee. If an allocation throws, nothing is leaked.
  164 +
  165 + @throws std::bad_alloc If allocation fails.
  166 + @throws Any exception thrown by moving `s`.
  167 +
163   @param s The stream to take ownership of. 168   @param s The stream to take ownership of.
164   */ 169   */
165   template<WriteStream S> 170   template<WriteStream S>
166   requires (!std::same_as<std::decay_t<S>, any_write_stream>) 171   requires (!std::same_as<std::decay_t<S>, any_write_stream>)
167   any_write_stream(S s); 172   any_write_stream(S s);
168   173  
169   /** Construct by wrapping a WriteStream without ownership. 174   /** Construct by wrapping a WriteStream without ownership.
170   175  
171   Wraps the given stream by pointer. The stream must remain 176   Wraps the given stream by pointer. The stream must remain
172   valid for the lifetime of this wrapper. 177   valid for the lifetime of this wrapper.
173   178  
  179 + @throws std::bad_alloc If allocating the awaitable storage fails.
  180 +
174   @param s Pointer to the stream to wrap. 181   @param s Pointer to the stream to wrap.
175   */ 182   */
176   template<WriteStream S> 183   template<WriteStream S>
177   any_write_stream(S* s); 184   any_write_stream(S* s);
178   185  
179   /** Check if the wrapper contains a valid stream. 186   /** Check if the wrapper contains a valid stream.
180   187  
181   @return `true` if wrapping a stream, `false` if default-constructed 188   @return `true` if wrapping a stream, `false` if default-constructed
182   or moved-from. 189   or moved-from.
183   */ 190   */
184   bool 191   bool
HITCBC 185   26 has_value() const noexcept 192   26 has_value() const noexcept
186   { 193   {
HITCBC 187   26 return stream_ != nullptr; 194   26 return stream_ != nullptr;
188   } 195   }
189   196  
190   /** Check if the wrapper contains a valid stream. 197   /** Check if the wrapper contains a valid stream.
191   198  
192   @return `true` if wrapping a stream, `false` if default-constructed 199   @return `true` if wrapping a stream, `false` if default-constructed
193   or moved-from. 200   or moved-from.
194   */ 201   */
195   explicit 202   explicit
HITCBC 196   3 operator bool() const noexcept 203   3 operator bool() const noexcept
197   { 204   {
HITCBC 198   3 return has_value(); 205   3 return has_value();
199   } 206   }
200   207  
201 - /** Initiate an asynchronous write operation. 208 + /** Asynchronously write some data from the buffer sequence.
202   209  
203   Writes data from the provided buffer sequence. The operation 210   Writes data from the provided buffer sequence. The operation
204   completes when at least one byte is written, or an error 211   completes when at least one byte is written, or an error
205   occurs. 212   occurs.
206   213  
207 - @param buffers The buffer sequence containing data to write. 214 + @pre The wrapper must contain a valid stream (`has_value() == true`).
208 - Passed by value to ensure the sequence lives in the  
209 - coroutine frame across suspension points.  
210 -  
211 - @return An awaitable that await-returns `(error_code,std::size_t)`.  
212   215  
213   @par Immediate Completion 216   @par Immediate Completion
214   The operation completes immediately without suspending 217   The operation completes immediately without suspending
215   the calling coroutine when: 218   the calling coroutine when:
216   @li The buffer sequence is empty, returning `{error_code{}, 0}`. 219   @li The buffer sequence is empty, returning `{error_code{}, 0}`.
217   @li The underlying stream's awaitable reports immediate 220   @li The underlying stream's awaitable reports immediate
218   readiness via `await_ready`. 221   readiness via `await_ready`.
219   222  
  223 + @par After an Error
  224 + A subsequent call is permitted. The wrapper forwards directly
  225 + to the underlying stream, imposing no stricter rule than
  226 + @ref WriteStream.
  227 +
220   @note This is a partial operation and may not process the 228   @note This is a partial operation and may not process the
221   entire buffer sequence. Use the composed @ref write algorithm 229   entire buffer sequence. Use the composed @ref write algorithm
222   for guaranteed complete transfer. 230   for guaranteed complete transfer.
223   231  
224 - @par Preconditions 232 + @param buffers The buffer sequence containing data to write.
225 - The wrapper must contain a valid stream (`has_value() == true`). 233 + Passed by value to ensure the sequence lives in the
  234 + coroutine frame across suspension points.
226   235  
227 - @par After an Error 236 + @return An awaitable that await-returns `(error_code,std::size_t)`.
228 - A subsequent call is permitted. The wrapper forwards directly  
229 - to the underlying stream, imposing no stricter rule than  
230 - @ref WriteStream.  
231   */ 237   */
232   template<ConstBufferSequence CB> 238   template<ConstBufferSequence CB>
233   auto 239   auto
234   write_some(CB buffers); 240   write_some(CB buffers);
235   241  
236   protected: 242   protected:
237   /** End the operation in flight, if any. 243   /** End the operation in flight, if any.
238   244  
239   Destroys the wrapped stream's active operation, which may refer 245   Destroys the wrapped stream's active operation, which may refer
240   to the stream, so an owner must call this before destroying the 246   to the stream, so an owner must call this before destroying the
241   stream. The awaitable that started the operation no longer ends 247   stream. The awaitable that started the operation no longer ends
242   it. 248   it.
243   */ 249   */
244   void 250   void
245   end_operation() noexcept; 251   end_operation() noexcept;
246   252  
247   /** Rebind to a new stream after move. 253   /** Rebind to a new stream after move.
248   254  
249 - Updates the internal pointer to reference a new stream object. 255 + Updates the internal pointer to reference a new stream object,
250 - Used by owning wrappers after move assignment when the owned 256 + for use when the stream object has moved to a new location.
251 - object has moved to a new location.  
252   257  
253   @param new_stream The new stream to bind to. Must be the same 258   @param new_stream The new stream to bind to. Must be the same
254   type as the original stream. 259   type as the original stream.
255   260  
256   @note Terminates if called with a stream of different type 261   @note Terminates if called with a stream of different type
257   than the original. 262   than the original.
258   */ 263   */
259   template<WriteStream S> 264   template<WriteStream S>
260   void 265   void
261   rebind(S& new_stream) noexcept 266   rebind(S& new_stream) noexcept
262   { 267   {
263   if(vt_ != &vtable_for_impl<S>::value) 268   if(vt_ != &vtable_for_impl<S>::value)
264   std::terminate(); 269   std::terminate();
265   stream_ = &new_stream; 270   stream_ = &new_stream;
266   } 271   }
267   }; 272   };
268   273  
269   struct any_write_stream::vtable 274   struct any_write_stream::vtable
270   { 275   {
271   // ordered by call frequency for cache line coherence 276   // ordered by call frequency for cache line coherence
272   void (*construct_awaitable)( 277   void (*construct_awaitable)(
273   void* stream, 278   void* stream,
274   void* storage, 279   void* storage,
275   std::span<const_buffer const> buffers); 280   std::span<const_buffer const> buffers);
276   bool (*await_ready)(void*); 281   bool (*await_ready)(void*);
277   std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, await_context const*); 282   std::coroutine_handle<> (*await_suspend)(void*, std::coroutine_handle<>, await_context const*);
278   results<std::size_t> (*await_resume)(void*); 283   results<std::size_t> (*await_resume)(void*);
279   void (*destroy_awaitable)(void*) noexcept; 284   void (*destroy_awaitable)(void*) noexcept;
280   std::size_t awaitable_size; 285   std::size_t awaitable_size;
281   std::size_t awaitable_align; 286   std::size_t awaitable_align;
282   void (*destroy)(void*) noexcept; 287   void (*destroy)(void*) noexcept;
283   }; 288   };
284   289  
285   template<WriteStream S> 290   template<WriteStream S>
286   struct any_write_stream::vtable_for_impl 291   struct any_write_stream::vtable_for_impl
287   { 292   {
288   using Awaitable = decltype(std::declval<S&>().write_some( 293   using Awaitable = decltype(std::declval<S&>().write_some(
289   std::span<const_buffer const>{})); 294   std::span<const_buffer const>{}));
290   295  
291   static void 296   static void
HITCBC 292   3 do_destroy_impl(void* stream) noexcept 297   3 do_destroy_impl(void* stream) noexcept
293   { 298   {
HITCBC 294   3 static_cast<S*>(stream)->~S(); 299   3 static_cast<S*>(stream)->~S();
HITCBC 295   3 } 300   3 }
296   301  
297   static void 302   static void
HITCBC 298   86 construct_awaitable_impl( 303   86 construct_awaitable_impl(
299   void* stream, 304   void* stream,
300   void* storage, 305   void* storage,
301   std::span<const_buffer const> buffers) 306   std::span<const_buffer const> buffers)
302   { 307   {
HITCBC 303   86 auto& s = *static_cast<S*>(stream); 308   86 auto& s = *static_cast<S*>(stream);
HITCBC 304   86 ::new(storage) Awaitable(s.write_some(buffers)); 309   86 ::new(storage) Awaitable(s.write_some(buffers));
HITCBC 305   86 } 310   86 }
306   311  
307   static constexpr vtable value = { 312   static constexpr vtable value = {
308   &construct_awaitable_impl, 313   &construct_awaitable_impl,
HITCBC 309   86 +[](void* p) { 314   86 +[](void* p) {
HITCBC 310   86 return static_cast<Awaitable*>(p)->await_ready(); 315   86 return static_cast<Awaitable*>(p)->await_ready();
311   }, 316   },
HITCBC 312   74 +[](void* p, std::coroutine_handle<> h, await_context const* env) { 317   74 +[](void* p, std::coroutine_handle<> h, await_context const* env) {
HITCBC 313   74 return detail::call_await_suspend( 318   74 return detail::call_await_suspend(
HITCBC 314   74 static_cast<Awaitable*>(p), h, env); 319   74 static_cast<Awaitable*>(p), h, env);
315   }, 320   },
HITCBC 316   77 +[](void* p) { 321   77 +[](void* p) {
HITCBC 317   77 return static_cast<Awaitable*>(p)->await_resume(); 322   77 return static_cast<Awaitable*>(p)->await_resume();
318   }, 323   },
HITCBC 319   95 +[](void* p) noexcept { 324   95 +[](void* p) noexcept {
HITCBC 320   12 static_cast<Awaitable*>(p)->~Awaitable(); 325   12 static_cast<Awaitable*>(p)->~Awaitable();
321   }, 326   },
322   sizeof(Awaitable), 327   sizeof(Awaitable),
323   alignof(Awaitable), 328   alignof(Awaitable),
324   &do_destroy_impl 329   &do_destroy_impl
325   }; 330   };
326   }; 331   };
327   332  
328   inline void 333   inline void
HITCBC 329   187 any_write_stream::end_operation() noexcept 334   187 any_write_stream::end_operation() noexcept
330   { 335   {
HITCBC 331   187 disown(); 336   187 disown();
HITCBC 332   187 if(awaitable_active_) 337   187 if(awaitable_active_)
333   { 338   {
HITCBC 334   6 vt_->destroy_awaitable(cached_awaitable_); 339   6 vt_->destroy_awaitable(cached_awaitable_);
HITCBC 335   6 awaitable_active_ = false; 340   6 awaitable_active_ = false;
336   } 341   }
HITCBC 337   187 } 342   187 }
338   343  
339   inline 344   inline
HITCBC 340   125 any_write_stream::~any_write_stream() 345   125 any_write_stream::~any_write_stream()
341   { 346   {
HITCBC 342   125 end_operation(); 347   125 end_operation();
HITCBC 343   125 if(cached_awaitable_) 348   125 if(cached_awaitable_)
HITCBC 344   101 ::operator delete(cached_awaitable_); 349   101 ::operator delete(cached_awaitable_);
HITCBC 345   125 if(storage_) 350   125 if(storage_)
346   { 351   {
HITCBC 347   2 vt_->destroy(stream_); 352   2 vt_->destroy(stream_);
HITCBC 348   2 ::operator delete(storage_); 353   2 ::operator delete(storage_);
349   } 354   }
HITCBC 350   125 } 355   125 }
351   356  
352   inline any_write_stream& 357   inline any_write_stream&
HITCBC 353   19 any_write_stream::operator=(any_write_stream&& other) noexcept 358   19 any_write_stream::operator=(any_write_stream&& other) noexcept
354   { 359   {
HITCBC 355   19 if(this != &other) 360   19 if(this != &other)
356   { 361   {
HITCBC 357   19 end_operation(); 362   19 end_operation();
HITCBC 358   19 other.disown(); 363   19 other.disown();
HITCBC 359   19 if(cached_awaitable_) 364   19 if(cached_awaitable_)
HITCBC 360   7 ::operator delete(cached_awaitable_); 365   7 ::operator delete(cached_awaitable_);
HITCBC 361   19 if(storage_) 366   19 if(storage_)
362   { 367   {
HITCBC 363   1 vt_->destroy(stream_); 368   1 vt_->destroy(stream_);
HITCBC 364   1 ::operator delete(storage_); 369   1 ::operator delete(storage_);
365   } 370   }
HITCBC 366   19 stream_ = std::exchange(other.stream_, nullptr); 371   19 stream_ = std::exchange(other.stream_, nullptr);
HITCBC 367   19 vt_ = std::exchange(other.vt_, nullptr); 372   19 vt_ = std::exchange(other.vt_, nullptr);
HITCBC 368   19 cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr); 373   19 cached_awaitable_ = std::exchange(other.cached_awaitable_, nullptr);
HITCBC 369   19 storage_ = std::exchange(other.storage_, nullptr); 374   19 storage_ = std::exchange(other.storage_, nullptr);
HITCBC 370   19 awaitable_active_ = std::exchange(other.awaitable_active_, false); 375   19 awaitable_active_ = std::exchange(other.awaitable_active_, false);
371   } 376   }
HITCBC 372   19 return *this; 377   19 return *this;
373   } 378   }
374   379  
375   template<WriteStream S> 380   template<WriteStream S>
376   requires (!std::same_as<std::decay_t<S>, any_write_stream>) 381   requires (!std::same_as<std::decay_t<S>, any_write_stream>)
HITCBC 377   4 any_write_stream::any_write_stream(S s) 382   4 any_write_stream::any_write_stream(S s)
HITCBC 378   4 : vt_(&vtable_for_impl<S>::value) 383   4 : vt_(&vtable_for_impl<S>::value)
379   { 384   {
380   struct guard { 385   struct guard {
381   any_write_stream* self; 386   any_write_stream* self;
382   bool committed = false; 387   bool committed = false;
HITCBC 383   4 ~guard() { 388   4 ~guard() {
HITCBC 384   4 if(!committed && self->storage_) { 389   4 if(!committed && self->storage_) {
HITCBC 385   1 if(self->stream_) 390   1 if(self->stream_)
386   self->vt_->destroy(self->stream_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws 391   self->vt_->destroy(self->stream_); // LCOV_EXCL_LINE OOM rollback: only when the cached-awaitable allocation throws
HITCBC 387   1 ::operator delete(self->storage_); 392   1 ::operator delete(self->storage_);
HITCBC 388   1 self->storage_ = nullptr; 393   1 self->storage_ = nullptr;
HITCBC 389   1 self->stream_ = nullptr; 394   1 self->stream_ = nullptr;
390   } 395   }
HITCBC 391   4 } 396   4 }
HITCBC 392   4 } g{this}; 397   4 } g{this};
393   398  
HITCBC 394   4 storage_ = ::operator new(sizeof(S)); 399   4 storage_ = ::operator new(sizeof(S));
HITCBC 395   4 stream_ = ::new(storage_) S(std::move(s)); 400   4 stream_ = ::new(storage_) S(std::move(s));
396   401  
397   // Preallocate the awaitable storage 402   // Preallocate the awaitable storage
HITCBC 398   3 cached_awaitable_ = ::operator new(vt_->awaitable_size); 403   3 cached_awaitable_ = ::operator new(vt_->awaitable_size);
399   404  
HITCBC 400   3 g.committed = true; 405   3 g.committed = true;
HITCBC 401   4 } 406   4 }
402   407  
403   template<WriteStream S> 408   template<WriteStream S>
HITCBC 404   105 any_write_stream::any_write_stream(S* s) 409   105 any_write_stream::any_write_stream(S* s)
HITCBC 405   105 : stream_(s) 410   105 : stream_(s)
HITCBC 406   105 , vt_(&vtable_for_impl<S>::value) 411   105 , vt_(&vtable_for_impl<S>::value)
407   { 412   {
408   // Preallocate the awaitable storage 413   // Preallocate the awaitable storage
HITCBC 409   105 cached_awaitable_ = ::operator new(vt_->awaitable_size); 414   105 cached_awaitable_ = ::operator new(vt_->awaitable_size);
HITCBC 410   105 } 415   105 }
411   416  
412   template<ConstBufferSequence CB> 417   template<ConstBufferSequence CB>
413   auto 418   auto
HITCBC 414   91 any_write_stream::write_some(CB buffers) 419   91 any_write_stream::write_some(CB buffers)
415   { 420   {
416   struct awaitable 421   struct awaitable
417   { 422   {
418   any_write_stream* self_; 423   any_write_stream* self_;
419   detail::const_buffer_array<detail::max_iovec_> ba_; 424   detail::const_buffer_array<detail::max_iovec_> ba_;
420   // Set while this awaitable, not another on the same stream, 425   // Set while this awaitable, not another on the same stream,
421   // holds the stream's inner awaitable. 426   // holds the stream's inner awaitable.
422   bool owns_ = false; 427   bool owns_ = false;
423   428  
424   // A chain unwound while suspended here never reaches 429   // A chain unwound while suspended here never reaches
425   // await_resume, so the inner awaitable is destroyed here. 430   // await_resume, so the inner awaitable is destroyed here.
HITCBC 426   178 ~awaitable() 431   178 ~awaitable()
427   { 432   {
HITCBC 428   178 if(owns_) 433   178 if(owns_)
429   { 434   {
HITCBC 430   3 self_->vt_->destroy_awaitable(self_->cached_awaitable_); 435   3 self_->vt_->destroy_awaitable(self_->cached_awaitable_);
HITCBC 431   3 self_->awaitable_active_ = false; 436   3 self_->awaitable_active_ = false;
HITCBC 432   3 self_->owner_ = nullptr; 437   3 self_->owner_ = nullptr;
433   } 438   }
HITCBC 434   178 } 439   178 }
435   440  
HITCBC 436   91 awaitable( 441   91 awaitable(
437   any_write_stream* self, 442   any_write_stream* self,
438   CB const& buffers) noexcept 443   CB const& buffers) noexcept
HITCBC 439   91 : self_(self) 444   91 : self_(self)
HITCBC 440   91 , ba_(buffers) 445   91 , ba_(buffers)
441   { 446   {
HITCBC 442   91 } 447   91 }
443   448  
HITCBC 444   87 awaitable(awaitable&& other) noexcept 449   87 awaitable(awaitable&& other) noexcept
HITCBC 445   87 : self_(std::exchange(other.self_, nullptr)) 450   87 : self_(std::exchange(other.self_, nullptr))
HITCBC 446   87 , ba_(other.ba_) 451   87 , ba_(other.ba_)
HITCBC 447   87 , owns_(std::exchange(other.owns_, false)) 452   87 , owns_(std::exchange(other.owns_, false))
448   { 453   {
HITCBC 449   87 if(owns_) 454   87 if(owns_)
HITCBC 450   1 self_->owner_ = &owns_; 455   1 self_->owner_ = &owns_;
HITCBC 451   87 } 456   87 }
452   457  
453   bool 458   bool
HITCBC 454   90 await_ready() 459   90 await_ready()
455   { 460   {
456   // An empty write never touches the underlying stream 461   // An empty write never touches the underlying stream
HITCBC 457   90 if(ba_.to_span().empty()) 462   90 if(ba_.to_span().empty())
HITCBC 458   4 return true; 463   4 return true;
459   464  
HITCBC 460   86 self_->vt_->construct_awaitable( 465   86 self_->vt_->construct_awaitable(
HITCBC 461   86 self_->stream_, 466   86 self_->stream_,
HITCBC 462   86 self_->cached_awaitable_, 467   86 self_->cached_awaitable_,
HITCBC 463   86 ba_.to_span()); 468   86 ba_.to_span());
HITCBC 464   86 self_->awaitable_active_ = true; 469   86 self_->awaitable_active_ = true;
HITCBC 465   86 owns_ = true; 470   86 owns_ = true;
HITCBC 466   86 self_->owner_ = &owns_; 471   86 self_->owner_ = &owns_;
467   472  
HITCBC 468   172 return self_->vt_->await_ready( 473   172 return self_->vt_->await_ready(
HITCBC 469   86 self_->cached_awaitable_); 474   86 self_->cached_awaitable_);
470   } 475   }
471   476  
472   std::coroutine_handle<> 477   std::coroutine_handle<>
HITCBC 473   74 await_suspend(std::coroutine_handle<> h, await_context const* env) 478   74 await_suspend(std::coroutine_handle<> h, await_context const* env)
474   { 479   {
HITCBC 475   74 return self_->vt_->await_suspend( 480   74 return self_->vt_->await_suspend(
HITCBC 476   74 self_->cached_awaitable_, h, env); 481   74 self_->cached_awaitable_, h, env);
477   } 482   }
478   483  
479   [[nodiscard]] results<std::size_t> 484   [[nodiscard]] results<std::size_t>
HITCBC 480   81 await_resume() 485   81 await_resume()
481   { 486   {
HITCBC 482   81 if(!owns_) 487   81 if(!owns_)
HITCBC 483   4 return {std::error_code(), 0}; 488   4 return {std::error_code(), 0};
484   struct guard { 489   struct guard {
485   awaitable* a; 490   awaitable* a;
HITCBC 486   77 ~guard() { 491   77 ~guard() {
HITCBC 487   77 a->self_->vt_->destroy_awaitable( 492   77 a->self_->vt_->destroy_awaitable(
HITCBC 488   77 a->self_->cached_awaitable_); 493   77 a->self_->cached_awaitable_);
HITCBC 489   77 a->self_->awaitable_active_ = false; 494   77 a->self_->awaitable_active_ = false;
HITCBC 490   77 a->self_->owner_ = nullptr; 495   77 a->self_->owner_ = nullptr;
HITCBC 491   77 a->owns_ = false; 496   77 a->owns_ = false;
HITCBC 492   77 } 497   77 }
HITCBC 493   77 } g{this}; 498   77 } g{this};
HITCBC 494   77 return self_->vt_->await_resume( 499   77 return self_->vt_->await_resume(
HITCBC 495   77 self_->cached_awaitable_); 500   77 self_->cached_awaitable_);
HITCBC 496   77 } 501   77 }
497   }; 502   };
HITCBC 498   91 return awaitable{this, buffers}; 503   91 return awaitable{this, buffers};
499   } 504   }
500   505  
501   } // namespace capy 506   } // namespace capy
502   } // namespace boost 507   } // namespace boost
503   508  
504   #endif 509   #endif