LCOV - code coverage report
Current view: top level - include/boost/http/io - buffer_param.hpp (source / functions) Coverage Total Hit Missed
Test: coverage_remapped.info Lines: 100.0 % 31 31
Test Date: 2026-11-22 21:03:06 Functions: 96.4 % 56 54 2

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

Generated by: LCOV version 2.3