Line |
Branch |
Exec |
Source |
1 |
|
|
// |
2 |
|
|
// Copyright (c) 2019 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_proto |
8 |
|
|
// |
9 |
|
|
|
10 |
|
|
#ifndef BOOST_HTTP_PROTO_DETAIL_IMPL_CIRCULAR_BUFFER_IPP |
11 |
|
|
#define BOOST_HTTP_PROTO_DETAIL_IMPL_CIRCULAR_BUFFER_IPP |
12 |
|
|
|
13 |
|
|
#include <boost/http_proto/detail/circular_buffer.hpp> |
14 |
|
|
#include <boost/assert.hpp> |
15 |
|
|
|
16 |
|
|
namespace boost { |
17 |
|
|
namespace http_proto { |
18 |
|
|
namespace detail { |
19 |
|
|
|
20 |
|
✗ |
circular_buffer:: |
21 |
|
|
circular_buffer( |
22 |
|
|
void* base, |
23 |
|
✗ |
std::size_t capacity) noexcept |
24 |
|
|
: base_(reinterpret_cast< |
25 |
|
|
unsigned char*>(base)) |
26 |
|
✗ |
, cap_(capacity) |
27 |
|
|
{ |
28 |
|
|
} |
29 |
|
|
|
30 |
|
|
bool |
31 |
|
✗ |
circular_buffer:: |
32 |
|
|
empty() const noexcept |
33 |
|
|
{ |
34 |
|
✗ |
return in_len_ == 0; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
auto |
38 |
|
✗ |
circular_buffer:: |
39 |
|
|
data() const noexcept -> |
40 |
|
|
buffers |
41 |
|
|
{ |
42 |
|
✗ |
if(in_pos_ + in_len_ <= cap_) |
43 |
|
|
return { |
44 |
|
|
mutable_buffer{ |
45 |
|
✗ |
base_ + in_pos_, in_len_ }, |
46 |
|
✗ |
mutable_buffer{ base_, 0} }; |
47 |
|
|
return { |
48 |
|
|
mutable_buffer{ |
49 |
|
✗ |
base_ + in_pos_, cap_ - in_pos_}, |
50 |
|
|
mutable_buffer{ |
51 |
|
✗ |
base_, in_len_- (cap_ - in_pos_)}}; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
auto |
55 |
|
✗ |
circular_buffer:: |
56 |
|
|
prepare() noexcept -> |
57 |
|
|
buffers |
58 |
|
|
{ |
59 |
|
✗ |
auto const n = cap_ - in_len_; |
60 |
|
✗ |
auto const out_pos = |
61 |
|
✗ |
(in_pos_ + in_len_) % cap_; |
62 |
|
✗ |
if(out_pos + n <= cap_) |
63 |
|
|
return { |
64 |
|
|
mutable_buffer{ |
65 |
|
✗ |
base_ + out_pos, n}, |
66 |
|
✗ |
mutable_buffer{base_, |
67 |
|
✗ |
n - (cap_ - out_pos)} }; |
68 |
|
|
return { |
69 |
|
|
mutable_buffer{ |
70 |
|
✗ |
base_ + out_pos, cap_ - out_pos}, |
71 |
|
|
mutable_buffer{ |
72 |
|
✗ |
base_, n - (cap_ - out_pos)}}; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
void |
76 |
|
✗ |
circular_buffer:: |
77 |
|
|
commit(std::size_t n) noexcept |
78 |
|
|
{ |
79 |
|
✗ |
BOOST_ASSERT(n <= cap_ - in_len_); |
80 |
|
✗ |
in_len_ += n; |
81 |
|
|
} |
82 |
|
|
|
83 |
|
|
void |
84 |
|
✗ |
circular_buffer:: |
85 |
|
|
consume(std::size_t n) noexcept |
86 |
|
|
{ |
87 |
|
✗ |
if(n < in_len_) |
88 |
|
|
{ |
89 |
|
✗ |
in_pos_ = (in_pos_ + n) % cap_; |
90 |
|
✗ |
in_len_ -= n; |
91 |
|
|
} |
92 |
|
|
else |
93 |
|
|
{ |
94 |
|
|
// make prepare return a |
95 |
|
|
// bigger single buffer |
96 |
|
✗ |
in_pos_ = 0; |
97 |
|
✗ |
in_len_ = 0; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
} // detail |
102 |
|
|
} // http_proto |
103 |
|
|
} // boost |
104 |
|
|
|
105 |
|
|
#endif |
106 |
|
|
|