GCC Code Coverage Report


Directory: ./
File: libs/buffers/src/circular_buffer.cpp
Date: 2025-12-06 02:12:43
Exec Total Coverage
Lines: 33 33 100.0%
Functions: 4 4 100.0%
Branches: 12 12 100.0%

Line Branch Exec Source
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/buffers
8 //
9
10 #include <boost/buffers/circular_buffer.hpp>
11 #include <boost/buffers/detail/except.hpp>
12 #include <boost/assert.hpp>
13
14 namespace boost {
15 namespace buffers {
16
17 auto
18 35410 circular_buffer::
19 data() const noexcept ->
20 const_buffers_type
21 {
22
2/2
✓ Branch 0 taken 19926 times.
✓ Branch 1 taken 15484 times.
35410 if(in_pos_ + in_len_ <= cap_)
23 return {{
24 19926 const_buffer{ base_ + in_pos_, in_len_ },
25 19926 const_buffer{ base_, 0} }};
26 return {{
27 15484 const_buffer{ base_ + in_pos_, cap_ - in_pos_},
28 15484 const_buffer{ base_, in_len_- (cap_ - in_pos_)} }};
29 }
30
31 auto
32 23618 circular_buffer::
33 prepare(std::size_t n) ->
34 mutable_buffers_type
35 {
36 // Buffer is too small for n
37
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23617 times.
23618 if(n > cap_ - in_len_)
38 1 detail::throw_length_error();
39
40 23617 out_size_ = n;
41 23617 auto const pos = (
42 23617 in_pos_ + in_len_) % cap_;
43
2/2
✓ Branch 0 taken 17793 times.
✓ Branch 1 taken 5824 times.
23617 if(pos + n <= cap_)
44 return {{
45 17793 mutable_buffer{ base_ + pos, n },
46 17793 mutable_buffer{ base_, 0 } }};
47 return {{
48 5824 mutable_buffer{ base_ + pos, cap_ - pos },
49 5824 mutable_buffer{ base_, n - (cap_ - pos) } }};
50 }
51
52 void
53 23617 circular_buffer::
54 commit(
55 std::size_t n) noexcept
56 {
57
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23616 times.
23617 if(n < out_size_)
58 1 in_len_ += n;
59 else
60 23616 in_len_ += out_size_;
61 23617 out_size_ = 0;
62 23617 }
63
64 void
65 22608 circular_buffer::
66 consume(
67 std::size_t n) noexcept
68 {
69
2/2
✓ Branch 0 taken 21600 times.
✓ Branch 1 taken 1008 times.
22608 if(n < in_len_)
70 {
71 21600 in_pos_ = (in_pos_ + n) % cap_;
72 21600 in_len_ -= n;
73 }
74 else
75 {
76 // preserve in_pos_ if there is
77 // a prepared buffer
78
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 993 times.
1008 if(out_size_ != 0)
79 {
80 15 in_pos_ = (in_pos_ + in_len_) % cap_;
81 15 in_len_ = 0;
82 }
83 else
84 {
85 // make prepare return a
86 // bigger single buffer
87 993 in_pos_ = 0;
88 993 in_len_ = 0;
89 }
90 }
91 22608 }
92
93 } // buffers
94 } // boost
95