]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/boost/beast/core/impl/static_buffer.ipp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / beast / core / impl / static_buffer.ipp
CommitLineData
b32b8144 1//
92f5a8d4 2// Copyright (c) 2016-2019 Vinnie Falco (vinnie dot falco at gmail dot com)
b32b8144
FG
3//
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)
6//
7// Official repository: https://github.com/boostorg/beast
8//
9
10#ifndef BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
11#define BOOST_BEAST_IMPL_STATIC_BUFFER_IPP
12
92f5a8d4 13#include <boost/beast/core/static_buffer.hpp>
b32b8144
FG
14#include <boost/asio/buffer.hpp>
15#include <boost/throw_exception.hpp>
b32b8144
FG
16#include <stdexcept>
17
18namespace boost {
19namespace beast {
20
b32b8144 21static_buffer_base::
92f5a8d4
TL
22static_buffer_base(
23 void* p, std::size_t size) noexcept
24 : begin_(static_cast<char*>(p))
b32b8144
FG
25 , capacity_(size)
26{
27}
28
92f5a8d4
TL
29void
30static_buffer_base::
31clear() noexcept
32{
33 in_off_ = 0;
34 in_size_ = 0;
35 out_size_ = 0;
36}
37
b32b8144
FG
38auto
39static_buffer_base::
92f5a8d4 40data() const noexcept ->
b32b8144
FG
41 const_buffers_type
42{
b32b8144 43 if(in_off_ + in_size_ <= capacity_)
92f5a8d4
TL
44 return {
45 net::const_buffer{
46 begin_ + in_off_, in_size_},
47 net::const_buffer{
48 begin_, 0}};
49 return {
50 net::const_buffer{
51 begin_ + in_off_, capacity_ - in_off_},
52 net::const_buffer{
53 begin_, in_size_ - (capacity_ - in_off_)}};
11fdf7f2
TL
54}
55
11fdf7f2
TL
56auto
57static_buffer_base::
92f5a8d4
TL
58data() noexcept ->
59 mutable_data_type
11fdf7f2 60{
11fdf7f2 61 if(in_off_ + in_size_ <= capacity_)
92f5a8d4
TL
62 return {
63 net::mutable_buffer{
64 begin_ + in_off_, in_size_},
65 net::mutable_buffer{
66 begin_, 0}};
67 return {
68 net::mutable_buffer{
69 begin_ + in_off_, capacity_ - in_off_},
70 net::mutable_buffer{
71 begin_, in_size_ - (capacity_ - in_off_)}};
b32b8144
FG
72}
73
b32b8144
FG
74auto
75static_buffer_base::
92f5a8d4 76prepare(std::size_t n) ->
b32b8144
FG
77 mutable_buffers_type
78{
92f5a8d4
TL
79 using net::mutable_buffer;
80 if(n > capacity_ - in_size_)
b32b8144 81 BOOST_THROW_EXCEPTION(std::length_error{
92f5a8d4
TL
82 "static_buffer overflow"});
83 out_size_ = n;
84 auto const out_off =
85 (in_off_ + in_size_) % capacity_;
b32b8144 86 if(out_off + out_size_ <= capacity_ )
92f5a8d4
TL
87 return {
88 net::mutable_buffer{
89 begin_ + out_off, out_size_},
90 net::mutable_buffer{
91 begin_, 0}};
92 return {
93 net::mutable_buffer{
94 begin_ + out_off, capacity_ - out_off},
95 net::mutable_buffer{
96 begin_, out_size_ - (capacity_ - out_off)}};
b32b8144
FG
97}
98
b32b8144
FG
99void
100static_buffer_base::
92f5a8d4 101commit(std::size_t n) noexcept
b32b8144 102{
92f5a8d4 103 in_size_ += (std::min)(n, out_size_);
b32b8144
FG
104 out_size_ = 0;
105}
106
b32b8144
FG
107void
108static_buffer_base::
92f5a8d4 109consume(std::size_t n) noexcept
b32b8144 110{
92f5a8d4 111 if(n < in_size_)
b32b8144 112 {
92f5a8d4
TL
113 in_off_ = (in_off_ + n) % capacity_;
114 in_size_ -= n;
b32b8144
FG
115 }
116 else
117 {
118 // rewind the offset, so the next call to prepare
92f5a8d4
TL
119 // can have a longer contiguous segment. this helps
120 // algorithms optimized for larger buffers.
b32b8144
FG
121 in_off_ = 0;
122 in_size_ = 0;
123 }
124}
125
b32b8144
FG
126} // beast
127} // boost
128
129#endif