src/buffers/circular_dynamic_buffer.cpp
100.0% Lines (33/33)
100.0% List of functions (4/4)
100.0% Branches (12/12)
Functions (4)
Function
Calls
Lines
Branches
Blocks
boost::capy::circular_dynamic_buffer::data() const
:17
0
100.0%
100.0%
–
boost::capy::circular_dynamic_buffer::prepare(unsigned long)
:31
0
100.0%
100.0%
–
boost::capy::circular_dynamic_buffer::commit(unsigned long)
:52
0
100.0%
100.0%
–
boost::capy::circular_dynamic_buffer::consume(unsigned long)
:64
0
100.0%
100.0%
–
| Line | Branch | 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/capy | |||
| 8 | // | |||
| 9 | ||||
| 10 | #include <boost/capy/buffers/circular_dynamic_buffer.hpp> | |||
| 11 | #include <boost/capy/detail/except.hpp> | |||
| 12 | ||||
| 13 | namespace boost { | |||
| 14 | namespace capy { | |||
| 15 | ||||
| 16 | auto | |||
| 17 | 1514x | circular_dynamic_buffer:: | ||
| 18 | data() const noexcept -> | |||
| 19 | const_buffers_type | |||
| 20 | { | |||
| 21 |
2/2✓ Branch 0 taken 1001 times.
✓ Branch 1 taken 513 times.
|
1514x | if(in_pos_ + in_len_ <= cap_) | |
| 22 | return {{ | |||
| 23 | 1001x | const_buffer{ base_ + in_pos_, in_len_ }, | ||
| 24 | 1001x | const_buffer{ base_, 0} }}; | ||
| 25 | return {{ | |||
| 26 | 513x | const_buffer{ base_ + in_pos_, cap_ - in_pos_}, | ||
| 27 | 513x | const_buffer{ base_, in_len_- (cap_ - in_pos_)} }}; | ||
| 28 | } | |||
| 29 | ||||
| 30 | auto | |||
| 31 | 866x | circular_dynamic_buffer:: | ||
| 32 | prepare(std::size_t n) -> | |||
| 33 | mutable_buffers_type | |||
| 34 | { | |||
| 35 | // Buffer is too small for n | |||
| 36 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 863 times.
|
866x | if(n > cap_ - in_len_) | |
| 37 | 3x | detail::throw_length_error(); | ||
| 38 | ||||
| 39 | 863x | out_size_ = n; | ||
| 40 | 863x | auto const pos = ( | ||
| 41 | 863x | in_pos_ + in_len_) % cap_; | ||
| 42 |
2/2✓ Branch 0 taken 731 times.
✓ Branch 1 taken 132 times.
|
863x | if(pos + n <= cap_) | |
| 43 | return {{ | |||
| 44 | 731x | mutable_buffer{ base_ + pos, n }, | ||
| 45 | 731x | mutable_buffer{ base_, 0 } }}; | ||
| 46 | return {{ | |||
| 47 | 132x | mutable_buffer{ base_ + pos, cap_ - pos }, | ||
| 48 | 132x | mutable_buffer{ base_, n - (cap_ - pos) } }}; | ||
| 49 | } | |||
| 50 | ||||
| 51 | void | |||
| 52 | 823x | circular_dynamic_buffer:: | ||
| 53 | commit( | |||
| 54 | std::size_t n) noexcept | |||
| 55 | { | |||
| 56 |
2/2✓ Branch 0 taken 52 times.
✓ Branch 1 taken 771 times.
|
823x | if(n < out_size_) | |
| 57 | 52x | in_len_ += n; | ||
| 58 | else | |||
| 59 | 771x | in_len_ += out_size_; | ||
| 60 | 823x | out_size_ = 0; | ||
| 61 | 823x | } | ||
| 62 | ||||
| 63 | void | |||
| 64 | 809x | circular_dynamic_buffer:: | ||
| 65 | consume( | |||
| 66 | std::size_t n) noexcept | |||
| 67 | { | |||
| 68 |
2/2✓ Branch 0 taken 555 times.
✓ Branch 1 taken 254 times.
|
809x | if(n < in_len_) | |
| 69 | { | |||
| 70 | 555x | in_pos_ = (in_pos_ + n) % cap_; | ||
| 71 | 555x | in_len_ -= n; | ||
| 72 | } | |||
| 73 | else | |||
| 74 | { | |||
| 75 | // preserve in_pos_ if there is | |||
| 76 | // a prepared buffer | |||
| 77 |
2/2✓ Branch 0 taken 1 time.
✓ Branch 1 taken 253 times.
|
254x | if(out_size_ != 0) | |
| 78 | { | |||
| 79 | 1x | in_pos_ = (in_pos_ + in_len_) % cap_; | ||
| 80 | 1x | in_len_ = 0; | ||
| 81 | } | |||
| 82 | else | |||
| 83 | { | |||
| 84 | // make prepare return a | |||
| 85 | // bigger single buffer | |||
| 86 | 253x | in_pos_ = 0; | ||
| 87 | 253x | in_len_ = 0; | ||
| 88 | } | |||
| 89 | } | |||
| 90 | 809x | } | ||
| 91 | ||||
| 92 | } // capy | |||
| 93 | } // boost | |||
| 94 |