impl/monotonic_resource.ipp

100.0% Lines (76 / 76) 100.0% Functions (10 / 10)
monotonic_resource.ipp
f(x) Functions (10)
Line TLA Hits Source Code
1 //
2 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
4 //
5 // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 //
8 // Official repository: https://github.com/boostorg/json
9 //
10
11 #ifndef BOOST_JSON_IMPL_MONOTONIC_RESOURCE_IPP
12 #define BOOST_JSON_IMPL_MONOTONIC_RESOURCE_IPP
13
14 #include <boost/json/monotonic_resource.hpp>
15 #include <boost/json/detail/except.hpp>
16 #include <boost/core/max_align.hpp>
17 #include <boost/throw_exception.hpp>
18
19 #include <memory>
20 #include <new>
21
22 namespace boost {
23 namespace json {
24
25 struct alignas(core::max_align_t)
26 monotonic_resource::block : block_base
27 {
28 };
29
30 constexpr
31 std::size_t
32 270x monotonic_resource::
33 max_size()
34 {
35 270x return std::size_t(-1) - sizeof(block);
36 }
37
38 // lowest power of 2 greater than or equal to n
39 std::size_t
40 57x monotonic_resource::
41 round_pow2(
42 std::size_t n) noexcept
43 {
44 57x if(n & (n - 1))
45 18x return next_pow2(n);
46 39x return n;
47 }
48
49 // lowest power of 2 greater than n
50 std::size_t
51 76x monotonic_resource::
52 next_pow2(
53 std::size_t n) noexcept
54 {
55 76x std::size_t result = min_size_;
56 296x while(result <= n)
57 {
58 221x if(result >= max_size() - result)
59 {
60 // overflow
61 1x result = max_size();
62 1x break;
63 }
64 220x result *= 2;
65 }
66 76x return result;
67 }
68
69 //----------------------------------------------------------
70
71 55x monotonic_resource::
72 55x ~monotonic_resource()
73 {
74 55x release();
75 55x }
76
77 44x monotonic_resource::
78 monotonic_resource(
79 std::size_t initial_size,
80 44x storage_ptr upstream) noexcept
81 44x : buffer_{
82 nullptr, 0, 0, nullptr}
83 88x , next_size_(round_pow2(initial_size))
84 44x , initial_size_(next_size_)
85 44x , upstream_(std::move(upstream))
86 {
87 44x }
88
89 11x monotonic_resource::
90 monotonic_resource(
91 unsigned char* buffer,
92 std::size_t size,
93 11x storage_ptr upstream) noexcept
94 11x : buffer_{
95 buffer, size, size, nullptr}
96 22x , next_size_(next_pow2(size))
97 11x , initial_size_(next_size_)
98 11x , upstream_(std::move(upstream))
99 {
100 11x }
101
102 void
103 64x monotonic_resource::
104 release() noexcept
105 {
106 64x auto p = head_;
107 111x while(p != &buffer_)
108 {
109 47x auto next = p->next;
110 47x upstream_->deallocate(p, sizeof(block) + p->size);
111 47x p = next;
112 }
113 64x buffer_.p = reinterpret_cast<
114 64x unsigned char*>(buffer_.p) - (
115 64x buffer_.size - buffer_.avail);
116 64x buffer_.avail = buffer_.size;
117 64x head_ = &buffer_;
118 64x next_size_ = initial_size_;
119 64x }
120
121 void*
122 129512x monotonic_resource::
123 do_allocate(
124 std::size_t n,
125 std::size_t align)
126 {
127 129512x auto p = std::align(align, n, head_->p, head_->avail);
128 129512x if(p)
129 {
130 129464x head_->p = reinterpret_cast<
131 129464x unsigned char*>(p) + n;
132 129464x head_->avail -= n;
133 129464x return p;
134 }
135
136 // a new block is only aligned to alignof(block), so an
137 // over-aligned request may need up to align - alignof(block)
138 // bytes of padding in addition to n
139 48x std::size_t const pad = align > alignof(block)
140 48x ? align - alignof(block) : 0;
141 48x if(n > max_size() - pad)
142 2x throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
143 47x if(next_size_ < n + pad)
144 13x next_size_ = round_pow2(n + pad);
145 47x auto b = ::new(upstream_->allocate(
146 47x sizeof(block) + next_size_)) block;
147 47x b->p = b + 1;
148 47x b->avail = next_size_;
149 47x b->size = next_size_;
150 47x b->next = head_;
151 47x head_ = b;
152 47x next_size_ = next_pow2(next_size_);
153
154 47x p = std::align(align, n, head_->p, head_->avail);
155 47x BOOST_ASSERT(p);
156 47x head_->p = reinterpret_cast<
157 47x unsigned char*>(p) + n;
158 47x head_->avail -= n;
159 47x return p;
160 }
161
162 void
163 29x monotonic_resource::
164 do_deallocate(
165 void*,
166 std::size_t,
167 std::size_t)
168 {
169 // do nothing
170 29x }
171
172 bool
173 1x monotonic_resource::
174 do_is_equal(
175 memory_resource const& mr) const noexcept
176 {
177 1x return this == &mr;
178 }
179
180 } // namespace json
181 } // namespace boost
182
183 #endif
184