Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:27

0001 //
0002 // Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // Official repository: https://github.com/boostorg/beast
0008 //
0009 
0010 #ifndef BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
0011 #define BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
0012 
0013 #include <boost/beast/core/static_buffer.hpp>
0014 #include <boost/asio/buffer.hpp>
0015 #include <boost/throw_exception.hpp>
0016 #include <stdexcept>
0017 
0018 namespace boost {
0019 namespace beast {
0020 
0021 static_buffer_base::
0022 static_buffer_base(
0023     void* p, std::size_t size) noexcept
0024     : begin_(static_cast<char*>(p))
0025     , capacity_(size)
0026 {
0027 }
0028 
0029 void
0030 static_buffer_base::
0031 clear() noexcept
0032 {
0033     in_off_ = 0;
0034     in_size_ = 0;
0035     out_size_ = 0;
0036 }
0037 
0038 auto
0039 static_buffer_base::
0040 data() const noexcept ->
0041     const_buffers_type
0042 {
0043     if(in_off_ + in_size_ <= capacity_)
0044         return {
0045             net::const_buffer{
0046                 begin_ + in_off_, in_size_},
0047             net::const_buffer{
0048                 begin_, 0}};
0049     return {
0050         net::const_buffer{
0051             begin_ + in_off_, capacity_ - in_off_},
0052         net::const_buffer{
0053             begin_, in_size_ - (capacity_ - in_off_)}};
0054 }
0055 
0056 auto
0057 static_buffer_base::
0058 data() noexcept ->
0059     mutable_buffers_type
0060 {
0061     if(in_off_ + in_size_ <= capacity_)
0062         return {
0063             net::mutable_buffer{
0064                 begin_ + in_off_, in_size_},
0065             net::mutable_buffer{
0066                 begin_, 0}};
0067     return {
0068         net::mutable_buffer{
0069             begin_ + in_off_, capacity_ - in_off_},
0070         net::mutable_buffer{
0071             begin_, in_size_ - (capacity_ - in_off_)}};
0072 }
0073 
0074 auto
0075 static_buffer_base::
0076 prepare(std::size_t n) ->
0077     mutable_buffers_type
0078 {
0079     using net::mutable_buffer;
0080     if(n > capacity_ - in_size_)
0081         BOOST_THROW_EXCEPTION(std::length_error{
0082             "static_buffer overflow"});
0083     out_size_ = n;
0084     auto const out_off =
0085         (in_off_ + in_size_) % capacity_;
0086     if(out_off + out_size_ <= capacity_ )
0087         return {
0088             net::mutable_buffer{
0089                 begin_ + out_off, out_size_},
0090             net::mutable_buffer{
0091                 begin_, 0}};
0092     return {
0093         net::mutable_buffer{
0094             begin_ + out_off, capacity_ - out_off},
0095         net::mutable_buffer{
0096             begin_, out_size_ - (capacity_ - out_off)}};
0097 }
0098 
0099 void
0100 static_buffer_base::
0101 commit(std::size_t n) noexcept
0102 {
0103     in_size_ += (std::min)(n, out_size_);
0104     out_size_ = 0;
0105 }
0106 
0107 void
0108 static_buffer_base::
0109 consume(std::size_t n) noexcept
0110 {
0111     if(n < in_size_)
0112     {
0113         in_off_ = (in_off_ + n) % capacity_;
0114         in_size_ -= n;
0115     }
0116     else
0117     {
0118         // rewind the offset, so the next call to prepare
0119         // can have a longer contiguous segment. this helps
0120         // algorithms optimized for larger buffers.
0121         in_off_ = 0;
0122         in_size_ = 0;
0123     }
0124 }
0125 
0126 } // beast
0127 } // boost
0128 
0129 #endif