100.00% Lines (31/31) 100.00% Functions (5/5)
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   // Copyright (c) 2026 Steve Gerbino 4   // Copyright (c) 2026 Steve Gerbino
5   // 5   //
6   // Distributed under the Boost Software License, Version 1.0. (See accompanying 6   // Distributed under the Boost Software License, Version 1.0. (See accompanying
7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 7   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8   // 8   //
9   // Official repository: https://github.com/cppalliance/http 9   // Official repository: https://github.com/cppalliance/http
10   // 10   //
11   11  
12   /* 12   /*
13   COROUTINE BUFFER SEQUENCE LIFETIME REQUIREMENT 13   COROUTINE BUFFER SEQUENCE LIFETIME REQUIREMENT
14   =============================================== 14   ===============================================
15   Buffer sequence parameters in coroutine APIs MUST be passed BY VALUE, 15   Buffer sequence parameters in coroutine APIs MUST be passed BY VALUE,
16   never by reference. When a coroutine suspends, reference parameters may 16   never by reference. When a coroutine suspends, reference parameters may
17   dangle if the caller's object goes out of scope before resumption. 17   dangle if the caller's object goes out of scope before resumption.
18   18  
19   CORRECT: task<> read_some(MutableBufferSequence auto buffers) 19   CORRECT: task<> read_some(MutableBufferSequence auto buffers)
20   WRONG: task<> read_some(MutableBufferSequence auto& buffers) 20   WRONG: task<> read_some(MutableBufferSequence auto& buffers)
21   WRONG: task<> read_some(MutableBufferSequence auto const& buffers) 21   WRONG: task<> read_some(MutableBufferSequence auto const& buffers)
22   22  
23   The buffer_param class works with this model: it takes a const& in its 23   The buffer_param class works with this model: it takes a const& in its
24   constructor (for the non-coroutine scope) but the caller's template 24   constructor (for the non-coroutine scope) but the caller's template
25   function accepts the buffer sequence by value, ensuring the sequence 25   function accepts the buffer sequence by value, ensuring the sequence
26   lives in the coroutine frame. 26   lives in the coroutine frame.
27   */ 27   */
28   28  
29   #ifndef BOOST_HTTP_IO_BUFFER_PARAM_HPP 29   #ifndef BOOST_HTTP_IO_BUFFER_PARAM_HPP
30   #define BOOST_HTTP_IO_BUFFER_PARAM_HPP 30   #define BOOST_HTTP_IO_BUFFER_PARAM_HPP
31   31  
32   #include <boost/capy/detail/config.hpp> 32   #include <boost/capy/detail/config.hpp>
33   #include <boost/capy/buffers.hpp> 33   #include <boost/capy/buffers.hpp>
34   34  
35   #include <new> 35   #include <new>
36   #include <span> 36   #include <span>
37   #include <type_traits> 37   #include <type_traits>
38   38  
39   namespace boost { 39   namespace boost {
40   namespace http { 40   namespace http {
41   41  
42   /** A buffer sequence wrapper providing windowed access. 42   /** A buffer sequence wrapper providing windowed access.
43   43  
44   This template class wraps any buffer sequence and provides 44   This template class wraps any buffer sequence and provides
45   incremental access through a sliding window of buffer 45   incremental access through a sliding window of buffer
46   descriptors. It handles both const and mutable buffer 46   descriptors. It handles both const and mutable buffer
47   sequences automatically. 47   sequences automatically.
48   48  
49   @par Coroutine Lifetime Requirement 49   @par Coroutine Lifetime Requirement
50   50  
51   When used in coroutine APIs, the outer template function 51   When used in coroutine APIs, the outer template function
52   MUST accept the buffer sequence parameter BY VALUE: 52   MUST accept the buffer sequence parameter BY VALUE:
53   53  
54   @code 54   @code
55   // CORRECT 55   // CORRECT
56   capy::task<> write(capy::ConstBufferSequence auto buffers); 56   capy::task<> write(capy::ConstBufferSequence auto buffers);
57   57  
58   // WRONG - dangling reference 58   // WRONG - dangling reference
59   capy::task<> write(capy::ConstBufferSequence auto& buffers); 59   capy::task<> write(capy::ConstBufferSequence auto& buffers);
60   @endcode 60   @endcode
61   61  
62   Pass-by-value ensures the buffer sequence is copied into 62   Pass-by-value ensures the buffer sequence is copied into
63   the coroutine frame and remains valid across suspension 63   the coroutine frame and remains valid across suspension
64   points. References would dangle when the caller's scope 64   points. References would dangle when the caller's scope
65   exits before the coroutine resumes. 65   exits before the coroutine resumes.
66   66  
67   @par Purpose 67   @par Purpose
68   68  
69   When iterating through large buffer sequences, it is often 69   When iterating through large buffer sequences, it is often
70   more efficient to process buffers in batches rather than 70   more efficient to process buffers in batches rather than
71   one at a time. This class maintains a window of up to a 71   one at a time. This class maintains a window of up to a
72   fixed, implementation-defined number of buffer descriptors 72   fixed, implementation-defined number of buffer descriptors
73   (currently 16). It refills the window from the underlying 73   (currently 16). It refills the window from the underlying
74   sequence as buffers are consumed. 74   sequence as buffers are consumed.
75   75  
76   @par Example 76   @par Example
77   77  
78   Create a `buffer_param` from any buffer sequence and use 78   Create a `buffer_param` from any buffer sequence and use
79   `data()` to get the current window of buffers. After 79   `data()` to get the current window of buffers. After
80   processing some bytes, call `consume()` to advance through 80   processing some bytes, call `consume()` to advance through
81   the sequence. 81   the sequence.
82   82  
83   @code 83   @code
84   capy::task<> send(capy::ConstBufferSequence auto buffers) 84   capy::task<> send(capy::ConstBufferSequence auto buffers)
85   { 85   {
86   buffer_param bp(buffers); 86   buffer_param bp(buffers);
87   while(true) 87   while(true)
88   { 88   {
89   auto bufs = bp.data(); 89   auto bufs = bp.data();
90   if(bufs.empty()) 90   if(bufs.empty())
91   break; 91   break;
92   auto n = co_await do_something(bufs); 92   auto n = co_await do_something(bufs);
93   bp.consume(n); 93   bp.consume(n);
94   } 94   }
95   } 95   }
96   @endcode 96   @endcode
97   97  
98   @par Virtual Interface Pattern 98   @par Virtual Interface Pattern
99   99  
100   This class enables passing arbitrary buffer sequences through 100   This class enables passing arbitrary buffer sequences through
101   a virtual function boundary. The template function captures 101   a virtual function boundary. The template function captures
102   the buffer sequence by value and drives the iteration, while 102   the buffer sequence by value and drives the iteration, while
103   the virtual function receives a simple span. Plain CTAD 103   the virtual function receives a simple span. Plain CTAD
104   (`buffer_param bp(buffers)`) deduces `BS`'s own buffer type, so a 104   (`buffer_param bp(buffers)`) deduces `BS`'s own buffer type, so a
105   mutable sequence yields `span<mutable_buffer>`. That does not match 105   mutable sequence yields `span<mutable_buffer>`. That does not match
106   `write_impl`'s `span<const_buffer>` parameter. Use @ref const_buffer_param 106   `write_impl`'s `span<const_buffer>` parameter. Use @ref const_buffer_param
107   to force `const_buffer` storage regardless of what `BS` is: 107   to force `const_buffer` storage regardless of what `BS` is:
108   108  
109   @code 109   @code
110   class base 110   class base
111   { 111   {
112   public: 112   public:
113   template<capy::ConstBufferSequence BS> 113   template<capy::ConstBufferSequence BS>
114   capy::task<> write(BS buffers) 114   capy::task<> write(BS buffers)
115   { 115   {
116   const_buffer_param<BS> bp(buffers); 116   const_buffer_param<BS> bp(buffers);
117   while(true) 117   while(true)
118   { 118   {
119   auto bufs = bp.data(); 119   auto bufs = bp.data();
120   if(bufs.empty()) 120   if(bufs.empty())
121   break; 121   break;
122   std::size_t n = 0; 122   std::size_t n = 0;
123   co_await write_impl(bufs, n); 123   co_await write_impl(bufs, n);
124   bp.consume(n); 124   bp.consume(n);
125   } 125   }
126   } 126   }
127   127  
128   protected: 128   protected:
129   virtual capy::task<> write_impl( 129   virtual capy::task<> write_impl(
130   std::span<capy::const_buffer> buffers, 130   std::span<capy::const_buffer> buffers,
131   std::size_t& bytes_written) = 0; 131   std::size_t& bytes_written) = 0;
132   }; 132   };
133   @endcode 133   @endcode
134   134  
135   @tparam BS The buffer sequence type. Must satisfy 135   @tparam BS The buffer sequence type. Must satisfy
136   `capy::ConstBufferSequence`. 136   `capy::ConstBufferSequence`.
137   */ 137   */
138   template<class BS, bool MakeConst = false> 138   template<class BS, bool MakeConst = false>
139   requires capy::ConstBufferSequence<BS> 139   requires capy::ConstBufferSequence<BS>
140   class buffer_param 140   class buffer_param
141   { 141   {
142   public: 142   public:
143   /// Names `const_buffer` when `MakeConst`, else `BS`'s own buffer type. 143   /// Names `const_buffer` when `MakeConst`, else `BS`'s own buffer type.
144   using buffer_type = std::conditional_t< 144   using buffer_type = std::conditional_t<
145   MakeConst, 145   MakeConst,
146   capy::const_buffer, 146   capy::const_buffer,
147   capy::buffer_type<BS>>; 147   capy::buffer_type<BS>>;
148   148  
149   private: 149   private:
150   decltype(capy::buffer_sequence_begin(std::declval<BS const&>())) it_; 150   decltype(capy::buffer_sequence_begin(std::declval<BS const&>())) it_;
151   decltype(capy::buffer_sequence_end(std::declval<BS const&>())) end_; 151   decltype(capy::buffer_sequence_end(std::declval<BS const&>())) end_;
152   union { 152   union {
153   int dummy_; 153   int dummy_;
154   buffer_type arr_[capy::detail::max_iovec_]; 154   buffer_type arr_[capy::detail::max_iovec_];
155   }; 155   };
156   std::size_t size_ = 0; 156   std::size_t size_ = 0;
157   std::size_t pos_ = 0; 157   std::size_t pos_ = 0;
158   158  
159   void 159   void
HITCBC 160   708 refill() 160   708 refill()
161   { 161   {
HITCBC 162   708 pos_ = 0; 162   708 pos_ = 0;
HITCBC 163   708 size_ = 0; 163   708 size_ = 0;
HITCBC 164   2027 for(; it_ != end_ && size_ < capy::detail::max_iovec_; ++it_) 164   2027 for(; it_ != end_ && size_ < capy::detail::max_iovec_; ++it_)
165   { 165   {
HITCBC 166   1319 buffer_type buf(*it_); 166   1319 buffer_type buf(*it_);
HITCBC 167   1319 if(buf.size() > 0) 167   1319 if(buf.size() > 0)
HITCBC 168   1276 ::new(&arr_[size_++]) buffer_type(buf); 168   1276 ::new(&arr_[size_++]) buffer_type(buf);
169   } 169   }
HITCBC 170   708 } 170   708 }
171   171  
172   public: 172   public:
173   /** Construct from a buffer sequence. 173   /** Construct from a buffer sequence.
174   174  
175   @param bs The buffer sequence to wrap. The caller must 175   @param bs The buffer sequence to wrap. The caller must
176   ensure the buffer sequence remains valid for the 176   ensure the buffer sequence remains valid for the
177   lifetime of this object. 177   lifetime of this object.
178   */ 178   */
179   explicit 179   explicit
HITCBC 180   540 buffer_param(BS const& bs) 180   540 buffer_param(BS const& bs)
HITCBC 181   540 : it_(capy::buffer_sequence_begin(bs)) 181   540 : it_(capy::buffer_sequence_begin(bs))
HITCBC 182   540 , end_(capy::buffer_sequence_end(bs)) 182   540 , end_(capy::buffer_sequence_end(bs))
HITCBC 183   540 , dummy_(0) 183   540 , dummy_(0)
184   { 184   {
HITCBC 185   540 refill(); 185   540 refill();
HITCBC 186   540 } 186   540 }
187   187  
188   /** Return the current window of buffer descriptors. 188   /** Return the current window of buffer descriptors.
189   189  
190   Returns a span of buffer descriptors representing the 190   Returns a span of buffer descriptors representing the
191   currently available portion of the buffer sequence. 191   currently available portion of the buffer sequence.
192   The span contains at most a fixed, implementation-defined 192   The span contains at most a fixed, implementation-defined
193   number of buffers (currently 16). 193   number of buffers (currently 16).
194   194  
195   When the current window is exhausted, this function 195   When the current window is exhausted, this function
196   automatically refills from the underlying sequence. 196   automatically refills from the underlying sequence.
197   197  
198   @return A span of buffer descriptors. Empty span 198   @return A span of buffer descriptors. Empty span
199   indicates no more data is available. 199   indicates no more data is available.
200   */ 200   */
201   std::span<buffer_type> 201   std::span<buffer_type>
HITCBC 202   929 data() 202   929 data()
203   { 203   {
HITCBC 204   929 if(pos_ >= size_) 204   929 if(pos_ >= size_)
HITCBC 205   168 refill(); 205   168 refill();
HITCBC 206   929 if(size_ == 0) 206   929 if(size_ == 0)
HITCBC 207   146 return {}; 207   146 return {};
HITCBC 208   783 return {arr_ + pos_, size_ - pos_}; 208   783 return {arr_ + pos_, size_ - pos_};
209   } 209   }
210   210  
211   /** Check if more buffers exist beyond the current window. 211   /** Check if more buffers exist beyond the current window.
212   212  
213   Returns `true` if the underlying buffer sequence has 213   Returns `true` if the underlying buffer sequence has
214   additional buffers that have not yet been loaded into 214   additional buffers that have not yet been loaded into
215   the current window. Call after @ref data to determine 215   the current window. Call after @ref data to determine
216   whether the current window is the last one. 216   whether the current window is the last one.
217   217  
218   @return `true` if more buffers remain in the sequence. 218   @return `true` if more buffers remain in the sequence.
219   */ 219   */
220   bool 220   bool
HITCBC 221   43 more() const noexcept 221   43 more() const noexcept
222   { 222   {
HITCBC 223   43 return it_ != end_; 223   43 return it_ != end_;
224   } 224   }
225   225  
226   /** Consume bytes from the buffer sequence. 226   /** Consume bytes from the buffer sequence.
227   227  
228   Advances the current position by `n` bytes, consuming 228   Advances the current position by `n` bytes, consuming
229   data from the front of the sequence. Partially consumed 229   data from the front of the sequence. Partially consumed
230   buffers are adjusted in place. 230   buffers are adjusted in place.
231   231  
232   @param n Number of bytes to consume. 232   @param n Number of bytes to consume.
233   */ 233   */
234   void 234   void
HITCBC 235   319 consume(std::size_t n) 235   319 consume(std::size_t n)
236   { 236   {
HITCBC 237   1012 while(n > 0 && pos_ < size_) 237   1012 while(n > 0 && pos_ < size_)
238   { 238   {
HITCBC 239   693 auto avail = arr_[pos_].size(); 239   693 auto avail = arr_[pos_].size();
HITCBC 240   693 if(n < avail) 240   693 if(n < avail)
241   { 241   {
HITCBC 242   187 arr_[pos_] += n; 242   187 arr_[pos_] += n;
HITCBC 243   187 n = 0; 243   187 n = 0;
244   } 244   }
245   else 245   else
246   { 246   {
HITCBC 247   506 n -= avail; 247   506 n -= avail;
HITCBC 248   506 ++pos_; 248   506 ++pos_;
249   } 249   }
250   } 250   }
HITCBC 251   319 } 251   319 }
252   }; 252   };
253   253  
254   /** Deduce the sequence type from the constructor argument. 254   /** Deduce the sequence type from the constructor argument.
255   255  
256   @tparam BS The buffer sequence type. 256   @tparam BS The buffer sequence type.
257   */ 257   */
258   template<class BS> 258   template<class BS>
259   buffer_param(BS const&) -> buffer_param<BS>; 259   buffer_param(BS const&) -> buffer_param<BS>;
260   260  
261   /// Forces `buffer_param` to store windows as `const_buffer`, whatever `BS` is. 261   /// Forces `buffer_param` to store windows as `const_buffer`, whatever `BS` is.
262   template<class BS> 262   template<class BS>
263   using const_buffer_param = buffer_param<BS, true>; 263   using const_buffer_param = buffer_param<BS, true>;
264   264  
265   } // namespace http 265   } // namespace http
266   } // namespace boost 266   } // namespace boost
267   267  
268   #endif 268   #endif