TLA Line data 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 HIT 270 : monotonic_resource::
33 : max_size()
34 : {
35 270 : 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 57 : monotonic_resource::
41 : round_pow2(
42 : std::size_t n) noexcept
43 : {
44 57 : if(n & (n - 1))
45 18 : return next_pow2(n);
46 39 : return n;
47 : }
48 :
49 : // lowest power of 2 greater than n
50 : std::size_t
51 76 : monotonic_resource::
52 : next_pow2(
53 : std::size_t n) noexcept
54 : {
55 76 : std::size_t result = min_size_;
56 296 : while(result <= n)
57 : {
58 221 : if(result >= max_size() - result)
59 : {
60 : // overflow
61 1 : result = max_size();
62 1 : break;
63 : }
64 220 : result *= 2;
65 : }
66 76 : return result;
67 : }
68 :
69 : //----------------------------------------------------------
70 :
71 55 : monotonic_resource::
72 55 : ~monotonic_resource()
73 : {
74 55 : release();
75 55 : }
76 :
77 44 : monotonic_resource::
78 : monotonic_resource(
79 : std::size_t initial_size,
80 44 : storage_ptr upstream) noexcept
81 44 : : buffer_{
82 : nullptr, 0, 0, nullptr}
83 88 : , next_size_(round_pow2(initial_size))
84 44 : , initial_size_(next_size_)
85 44 : , upstream_(std::move(upstream))
86 : {
87 44 : }
88 :
89 11 : monotonic_resource::
90 : monotonic_resource(
91 : unsigned char* buffer,
92 : std::size_t size,
93 11 : storage_ptr upstream) noexcept
94 11 : : buffer_{
95 : buffer, size, size, nullptr}
96 22 : , next_size_(next_pow2(size))
97 11 : , initial_size_(next_size_)
98 11 : , upstream_(std::move(upstream))
99 : {
100 11 : }
101 :
102 : void
103 64 : monotonic_resource::
104 : release() noexcept
105 : {
106 64 : auto p = head_;
107 111 : while(p != &buffer_)
108 : {
109 47 : auto next = p->next;
110 47 : upstream_->deallocate(p, sizeof(block) + p->size);
111 47 : p = next;
112 : }
113 64 : buffer_.p = reinterpret_cast<
114 64 : unsigned char*>(buffer_.p) - (
115 64 : buffer_.size - buffer_.avail);
116 64 : buffer_.avail = buffer_.size;
117 64 : head_ = &buffer_;
118 64 : next_size_ = initial_size_;
119 64 : }
120 :
121 : void*
122 129512 : monotonic_resource::
123 : do_allocate(
124 : std::size_t n,
125 : std::size_t align)
126 : {
127 129512 : auto p = std::align(align, n, head_->p, head_->avail);
128 129512 : if(p)
129 : {
130 129464 : head_->p = reinterpret_cast<
131 129464 : unsigned char*>(p) + n;
132 129464 : head_->avail -= n;
133 129464 : 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 48 : std::size_t const pad = align > alignof(block)
140 48 : ? align - alignof(block) : 0;
141 48 : if(n > max_size() - pad)
142 2 : throw_exception( std::bad_alloc(), BOOST_CURRENT_LOCATION );
143 47 : if(next_size_ < n + pad)
144 13 : next_size_ = round_pow2(n + pad);
145 47 : auto b = ::new(upstream_->allocate(
146 47 : sizeof(block) + next_size_)) block;
147 47 : b->p = b + 1;
148 47 : b->avail = next_size_;
149 47 : b->size = next_size_;
150 47 : b->next = head_;
151 47 : head_ = b;
152 47 : next_size_ = next_pow2(next_size_);
153 :
154 47 : p = std::align(align, n, head_->p, head_->avail);
155 47 : BOOST_ASSERT(p);
156 47 : head_->p = reinterpret_cast<
157 47 : unsigned char*>(p) + n;
158 47 : head_->avail -= n;
159 47 : return p;
160 : }
161 :
162 : void
163 29 : monotonic_resource::
164 : do_deallocate(
165 : void*,
166 : std::size_t,
167 : std::size_t)
168 : {
169 : // do nothing
170 29 : }
171 :
172 : bool
173 1 : monotonic_resource::
174 : do_is_equal(
175 : memory_resource const& mr) const noexcept
176 : {
177 1 : return this == &mr;
178 : }
179 :
180 : } // namespace json
181 : } // namespace boost
182 :
183 : #endif
|