GCC Code Coverage Report


Directory: libs/http_proto/include/boost/http_proto/
File: boost/http_proto/detail/impl/workspace.ipp
Date: 2023-03-02 17:13:10
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 750 workspace::
26 750 ~workspace()
27 {
28
1/2
✓ Branch 0 taken 750 times.
✗ Branch 1 not taken.
750 if(begin_)
29 {
30 750 clear();
31
1/2
✓ Branch 0 taken 750 times.
✗ Branch 1 not taken.
750 delete[] begin_;
32 }
33 750 }
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 738 workspace::
64 allocate(
65 std::size_t n)
66 {
67 // Cannot be empty
68
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 738 times.
738 if(n == 0)
69 detail::throw_invalid_argument();
70
71 // Already allocated
72
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 738 times.
738 if(begin_ != nullptr)
73 detail::throw_logic_error();
74
75 738 begin_ = new unsigned char[n];
76 738 front_ = begin_;
77 738 head_ = begin_ + n;
78 738 back_ = head_;
79 738 end_ = head_;
80 738 }
81
82 void
83 20402 workspace::
84 clear() noexcept
85 {
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20402 times.
20402 if(! begin_)
87 return;
88
89 20402 auto const end =
90 reinterpret_cast<
91 any const*>(back_);
92 20402 auto p =
93 reinterpret_cast<
94 any const*>(head_);
95
2/2
✓ Branch 0 taken 6812 times.
✓ Branch 1 taken 20402 times.
27214 while(p != end)
96 {
97 6812 auto next = p->next;
98 6812 p->~any();
99 6812 p = next;
100 }
101 20402 front_ = begin_;
102 20402 head_ = end_;
103 20402 back_ = end_;
104 }
105
106 unsigned char*
107 14158 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 14158 times.
14158 if(n >= size())
115 detail::throw_length_error();
116
117 14158 auto const p = front_;
118 14158 front_ += n ;
119 14158 return p;
120 }
121
122 unsigned char*
123 14152 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 14152 times.
14152 if(head_ != end_)
129 detail::throw_logic_error();
130
131 // can't reserve twice
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14152 times.
14152 if(back_ != end_)
133 detail::throw_logic_error();
134
135 // over capacity
136 14152 std::size_t const lim =
137 14152 head_ - front_;
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14152 times.
14152 if(n >= lim)
139 detail::throw_length_error();
140
141 14152 head_ -= n;
142 14152 back_ = head_;
143 14152 return back_;
144 }
145
146 // https://fitzgeraldnick.com/2019/11/01/always-bump-downwards.html
147 unsigned char*
148 6812 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 6812 times.
6812 BOOST_ASSERT(align > 0);
154
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6812 times.
6812 BOOST_ASSERT(
155 (align & (align - 1)) == 0);
156
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6812 times.
6812 BOOST_ASSERT(front_);
157
158 6812 auto ip0 = reinterpret_cast<
159 6812 std::uintptr_t>(front_);
160 6812 auto ip = reinterpret_cast<
161 6812 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 6812 times.
6812 if(size > ip - ip0)
168 detail::throw_bad_alloc();
169
170 6812 ip -= size;
171 6812 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 6812 times.
6812 if(ip < ip0)
178 detail::throw_bad_alloc();
179
180 return reinterpret_cast<
181 6812 unsigned char*>(ip);
182 }
183
184 } // detail
185 } // http_proto
186 } // boost
187
188 #endif
189