]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/beast/core/impl/static_buffer.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / beast / core / impl / static_buffer.ipp
1 //
2 // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
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
13 #include <boost/beast/core/detail/type_traits.hpp>
14 #include <boost/asio/buffer.hpp>
15 #include <boost/throw_exception.hpp>
16 #include <algorithm>
17 #include <cstring>
18 #include <iterator>
19 #include <stdexcept>
20
21 namespace boost {
22 namespace beast {
23
24 inline
25 static_buffer_base::
26 static_buffer_base(void* p, std::size_t size)
27 : begin_(reinterpret_cast<char*>(p))
28 , capacity_(size)
29 {
30 }
31
32 inline
33 auto
34 static_buffer_base::
35 data() const ->
36 const_buffers_type
37 {
38 using boost::asio::mutable_buffer;
39 const_buffers_type result;
40 if(in_off_ + in_size_ <= capacity_)
41 {
42 result[0] = mutable_buffer{begin_ + in_off_, in_size_};
43 result[1] = mutable_buffer{begin_, 0};
44 }
45 else
46 {
47 result[0] = mutable_buffer{begin_ + in_off_, capacity_ - in_off_};
48 result[1] = mutable_buffer{begin_, in_size_ - (capacity_ - in_off_)};
49 }
50 return result;
51 }
52
53 inline
54 auto
55 static_buffer_base::
56 prepare(std::size_t size) ->
57 mutable_buffers_type
58 {
59 using boost::asio::mutable_buffer;
60 if(size > capacity_ - in_size_)
61 BOOST_THROW_EXCEPTION(std::length_error{
62 "buffer overflow"});
63 out_size_ = size;
64 auto const out_off = (in_off_ + in_size_) % capacity_;
65 mutable_buffers_type result;
66 if(out_off + out_size_ <= capacity_ )
67 {
68 result[0] = mutable_buffer{begin_ + out_off, out_size_};
69 result[1] = mutable_buffer{begin_, 0};
70 }
71 else
72 {
73 result[0] = mutable_buffer{begin_ + out_off, capacity_ - out_off};
74 result[1] = mutable_buffer{begin_, out_size_ - (capacity_ - out_off)};
75 }
76 return result;
77 }
78
79 inline
80 void
81 static_buffer_base::
82 commit(std::size_t size)
83 {
84 in_size_ += (std::min)(size, out_size_);
85 out_size_ = 0;
86 }
87
88 inline
89 void
90 static_buffer_base::
91 consume(std::size_t size)
92 {
93 if(size < in_size_)
94 {
95 in_off_ = (in_off_ + size) % capacity_;
96 in_size_ -= size;
97 }
98 else
99 {
100 // rewind the offset, so the next call to prepare
101 // can have a longer continguous segment. this helps
102 // algorithms optimized for larger buffesr.
103 in_off_ = 0;
104 in_size_ = 0;
105 }
106 }
107
108 inline
109 void
110 static_buffer_base::
111 reset(void* p, std::size_t size)
112 {
113 begin_ = reinterpret_cast<char*>(p);
114 capacity_ = size;
115 in_off_ = 0;
116 in_size_ = 0;
117 out_size_ = 0;
118 }
119
120 //------------------------------------------------------------------------------
121
122 template<std::size_t N>
123 static_buffer<N>::
124 static_buffer(static_buffer const& other)
125 : static_buffer_base(buf_, N)
126 {
127 using boost::asio::buffer_copy;
128 this->commit(buffer_copy(
129 this->prepare(other.size()), other.data()));
130 }
131
132 template<std::size_t N>
133 auto
134 static_buffer<N>::
135 operator=(static_buffer const& other) ->
136 static_buffer<N>&
137 {
138 using boost::asio::buffer_copy;
139 this->consume(this->size());
140 this->commit(buffer_copy(
141 this->prepare(other.size()), other.data()));
142 return *this;
143 }
144
145 } // beast
146 } // boost
147
148 #endif