GCC Code Coverage Report


Directory: libs/http_proto/include/boost/http_proto/
File: boost/http_proto/detail/impl/workspace.ipp
Date: 2023-02-27 20:43:06
Exec Total Coverage
Lines: 61 82 74.4%
Functions: 7 8 87.5%
Branches: 16 30 53.3%

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_WORKSPACE_IPP
11 #define BOOST_HTTP_PROTO_DETAIL_IMPL_WORKSPACE_IPP
12
13 #include <boost/http_proto/detail/workspace.hpp>
14 #include <boost/http_proto/detail/except.hpp>
15 #include <boost/assert.hpp>
16
17 namespace boost {
18 namespace http_proto {
19 namespace detail {
20
21 workspace::
22 any::
23 ~any() = default;
24
25 6658 workspace::
26 6658 ~workspace()
27 {
28
1/2
✓ Branch 0 taken 6658 times.
✗ Branch 1 not taken.
6658 if(begin_)
29 {
30 6658 clear();
31
1/2
✓ Branch 0 taken 6658 times.
✗ Branch 1 not taken.
6658 delete[] begin_;
32 }
33 6658 }
34
35 12 workspace::
36 workspace(
37 12 std::size_t n)
38 12 : begin_(new unsigned char[n])
39 12 , front_(begin_)
40 12 , head_(begin_ + n)
41 12 , back_(head_)
42 12 , end_(head_)
43 {
44 12 }
45
46 workspace::
47 workspace(
48 workspace&& other) noexcept
49 : begin_(other.begin_)
50 , front_(other.front_)
51 , head_(other.end_)
52 , back_(other.back_)
53 , end_(other.end_)
54 {
55 other.begin_ = nullptr;
56 other.front_ = nullptr;
57 other.head_ = nullptr;
58 other.back_ = nullptr;
59 other.end_ = nullptr;
60 }
61
62 void
63 6646 workspace::
64 allocate(
65 std::size_t n)
66 {
67 // Cannot be empty
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6646 times.
6646 if(n == 0)
69 detail::throw_invalid_argument();
70
71 // Already allocated
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6646 times.
6646 if(begin_ != nullptr)
73 detail::throw_logic_error();
74
75 6646 begin_ = new unsigned char[n];
76 6646 front_ = begin_;
77 6646 head_ = begin_ + n;
78 6646 back_ = head_;
79 6646 end_ = head_;
80 6646 }
81
82 void
83 19963 workspace::
84 clear() noexcept
85 {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19963 times.
19963 if(! begin_)
87 return;
88
89 19963 auto const end =
90 reinterpret_cast<
91 any const*>(back_);
92 19963 auto p =
93 reinterpret_cast<
94 any const*>(head_);
95
2/2
✓ Branch 0 taken 37 times.
✓ Branch 1 taken 19963 times.
20000 while(p != end)
96 {
97 37 auto next = p->next;
98 37 p->~any();
99 37 p = next;
100 }
101 19963 front_ = begin_;
102 19963 head_ = end_;
103 19963 back_ = end_;
104 }
105
106 unsigned char*
107 6522 workspace::
108 reserve_front(
109 std::size_t n)
110 {
111 //
112 // Requested size exceeds available space.
113 // Note you can never reserve the last byte.
114
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6522 times.
6522 if(n >= size())
115 detail::throw_length_error();
116
117 6522 auto const p = front_;
118 6522 front_ += n ;
119 6522 return p;
120 }
121
122 unsigned char*
123 6516 workspace::
124 reserve_back(
125 std::size_t n)
126 {
127 // can't reserve after acquire
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6516 times.
6516 if(head_ != end_)
129 detail::throw_logic_error();
130
131 // can't reserve twice
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6516 times.
6516 if(back_ != end_)
133 detail::throw_logic_error();
134
135 // over capacity
136 6516 std::size_t const lim =
137 6516 head_ - front_;
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6516 times.
6516 if(n >= lim)
139 detail::throw_length_error();
140
141 6516 head_ -= n;
142 6516 back_ = head_;
143 6516 return back_;
144 }
145
146 // https://fitzgeraldnick.com/2019/11/01/always-bump-downwards.html
147 unsigned char*
148 37 workspace::
149 bump_down(
150 std::size_t size,
151 std::size_t align)
152 {
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 BOOST_ASSERT(align > 0);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 BOOST_ASSERT(
155 (align & (align - 1)) == 0);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 BOOST_ASSERT(front_);
157
158 37 auto ip0 = reinterpret_cast<
159 37 std::uintptr_t>(front_);
160 37 auto ip = reinterpret_cast<
161 37 std::uintptr_t>(head_);
162
163 // If you get an exception here, it
164 // means that a buffer was too small
165 // for your workload. Increase the
166 // buffer size.
167
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if(size > ip - ip0)
168 detail::throw_bad_alloc();
169
170 37 ip -= size;
171 37 ip &= ~(align - 1);
172
173 // If you get an exception here, it
174 // means that a buffer was too small
175 // for your workload. Increase the
176 // buffer size.
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 if(ip < ip0)
178 detail::throw_bad_alloc();
179
180 return reinterpret_cast<
181 37 unsigned char*>(ip);
182 }
183
184 } // detail
185 } // http_proto
186 } // boost
187
188 #endif
189