src/detail/circular_buffer.cpp

97.0% Lines (32/33) 100.0% List of functions (4/4)
circular_buffer.cpp
f(x) Functions (4)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2023 Vinnie Falco (vinnie.falco@gmail.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/cppalliance/http
8 //
9
10 #include <boost/http/detail/circular_buffer.hpp>
11 #include <boost/http/detail/except.hpp>
12
13 namespace boost {
14 namespace http {
15 namespace detail {
16
17 auto
18 169700x circular_buffer::
19 data() const noexcept ->
20 const_buffers_type
21 {
22 169700x if(in_pos_ + in_len_ <= cap_)
23 return {{
24 153419x capy::const_buffer{ base_ + in_pos_, in_len_ },
25 153419x capy::const_buffer{ base_, 0} }};
26 return {{
27 16281x capy::const_buffer{ base_ + in_pos_, cap_ - in_pos_},
28 16281x capy::const_buffer{ base_, in_len_- (cap_ - in_pos_)} }};
29 }
30
31 auto
32 90154x circular_buffer::
33 prepare(std::size_t n) ->
34 mutable_buffers_type
35 {
36 // Buffer is too small for n
37 90154x if(n > cap_ - in_len_)
38 detail::throw_length_error();
39
40 90154x out_size_ = n;
41 90154x auto const pos = (
42 90154x in_pos_ + in_len_) % cap_;
43 90154x if(pos + n <= cap_)
44 return {{
45 54078x capy::mutable_buffer{ base_ + pos, n },
46 54078x capy::mutable_buffer{ base_, 0 } }};
47 return {{
48 36076x capy::mutable_buffer{ base_ + pos, cap_ - pos },
49 36076x capy::mutable_buffer{ base_, n - (cap_ - pos) } }};
50 }
51
52 void
53 89826x circular_buffer::
54 commit(
55 std::size_t n) noexcept
56 {
57 89826x if(n < out_size_)
58 89549x in_len_ += n;
59 else
60 277x in_len_ += out_size_;
61 89826x out_size_ = 0;
62 89826x }
63
64 void
65 143923x circular_buffer::
66 consume(
67 std::size_t n) noexcept
68 {
69 143923x if(n < in_len_)
70 {
71 115209x in_pos_ = (in_pos_ + n) % cap_;
72 115209x in_len_ -= n;
73 }
74 else
75 {
76 // preserve in_pos_ if there is
77 // a prepared buffer
78 28714x if(out_size_ != 0)
79 {
80 1x in_pos_ = (in_pos_ + in_len_) % cap_;
81 1x in_len_ = 0;
82 }
83 else
84 {
85 // make prepare return a
86 // bigger single buffer
87 28713x in_pos_ = 0;
88 28713x in_len_ = 0;
89 }
90 }
91 143923x }
92
93 } // detail
94 } // http
95 } // boost
96