GCC Code Coverage Report


Directory: libs/buffers/
File: libs/buffers/src/circular_buffer.cpp
Date: 2024-07-24 21:34:17
Exec Total Coverage
Lines: 36 36 100.0%
Functions: 4 4 100.0%
Branches: 10 10 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/type_traits.hpp>
12 #include <boost/buffers/detail/except.hpp>
13 #include <boost/assert.hpp>
14 #include <boost/static_assert.hpp>
15
16 namespace boost {
17 namespace buffers {
18
19 BOOST_STATIC_ASSERT(
20 is_dynamic_buffer<
21 circular_buffer>::value);
22
23 auto
24 35378 circular_buffer::
25 data() const noexcept ->
26 const_buffers_type
27 {
28
2/2
✓ Branch 0 taken 19894 times.
✓ Branch 1 taken 15484 times.
35378 if(in_pos_ + in_len_ <= cap_)
29 return {
30 39788 const_buffer{
31 19894 base_ + in_pos_, in_len_ },
32 19894 const_buffer{ base_, 0} };
33 return {
34 30968 const_buffer{
35 15484 base_ + in_pos_, cap_ - in_pos_},
36 15484 const_buffer{
37 15484 base_, in_len_- (cap_ - in_pos_)}};
38 }
39
40 auto
41 23586 circular_buffer::
42 prepare(std::size_t n) ->
43 mutable_buffers_type
44 {
45 // Buffer is too small for n
46
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23585 times.
23586 if(n > cap_ - in_len_)
47 1 detail::throw_length_error();
48
49 23585 out_size_ = n;
50 23585 auto const pos = (
51 23585 in_pos_ + in_len_) % cap_;
52
2/2
✓ Branch 0 taken 17761 times.
✓ Branch 1 taken 5824 times.
23585 if(pos + n <= cap_)
53 return {
54 35522 mutable_buffer{
55 17761 base_ + pos, n},
56 17761 mutable_buffer{base_, 0}};
57 return {
58 11648 mutable_buffer{
59 5824 base_ + pos, cap_ - pos},
60 5824 mutable_buffer{
61 5824 base_, n - (cap_ - pos)}};
62 }
63
64 void
65 23585 circular_buffer::
66 commit(
67 std::size_t n) noexcept
68 {
69
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 23584 times.
23585 if(n < out_size_)
70 1 in_len_ += n;
71 else
72 23584 in_len_ += out_size_;
73 23585 out_size_ = 0;
74 23585 }
75
76 void
77 22592 circular_buffer::
78 consume(
79 std::size_t n) noexcept
80 {
81
2/2
✓ Branch 0 taken 21600 times.
✓ Branch 1 taken 992 times.
22592 if(n < in_len_)
82 {
83 21600 in_pos_ = (in_pos_ + n) % cap_;
84 21600 in_len_ -= n;
85 }
86 else
87 {
88 // make prepare return a
89 // bigger single buffer
90 992 in_pos_ = 0;
91 992 in_len_ = 0;
92 }
93 22592 }
94
95 } // buffers
96 } // boost
97