monotonic_resource.hpp

100.0% Lines (3 / 3) 100.0% Functions (1 / 1)
monotonic_resource.hpp
f(x) Functions (1)
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_MONOTONIC_RESOURCE_HPP
12 #define BOOST_JSON_MONOTONIC_RESOURCE_HPP
13
14 #include <boost/container/pmr/memory_resource.hpp>
15 #include <boost/json/detail/config.hpp>
16 #include <boost/json/storage_ptr.hpp>
17 #include <cstddef>
18 #include <utility>
19
20 namespace boost {
21 namespace json {
22
23 #ifdef _MSC_VER
24 #pragma warning(push)
25 #pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
26 #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
27 #endif
28
29 //----------------------------------------------------------
30
31 /** A dynamically allocating resource with a trivial deallocate.
32
33 This memory resource is a special-purpose resource that releases allocated
34 memory only when the resource is destroyed (or when @ref release is
35 called). It has a trivial deallocate function; that is, the metafunction
36 @ref is_deallocate_trivial returns `true`.
37
38 The resource can be constructed with an initial buffer. If there is no
39 initial buffer, or if the buffer is exhausted, subsequent dynamic
40 allocations are made from the system heap. The size of buffers obtained in
41 this fashion follow a geometric progression.
42
43 The purpose of this resource is to optimize the use case for performing
44 many allocations, followed by deallocating everything at once. This is
45 precisely the pattern of memory allocation which occurs when parsing:
46 allocation is performed for each parsed element, and when the the resulting
47 @ref value is no longer needed, the entire structure is destroyed. However,
48 it is not suited for modifying the value after parsing is complete;
49 reallocations waste memory, since the older buffer is not reclaimed until
50 the resource is destroyed.
51
52 @par Example
53
54 This parses a JSON text into a value which uses a local stack buffer, then
55 prints the result.
56
57 @code
58 unsigned char buf[ 4000 ];
59 monotonic_resource mr( buf );
60
61 // Parse the string, using our memory resource
62 auto const jv = parse( "[1,2,3]", &mr );
63
64 // Print the JSON
65 std::cout << jv;
66 @endcode
67
68 @note The total amount of memory dynamically allocated is monotonically
69 increasing; That is, it never decreases.
70
71 @par Thread Safety
72 Members of the same instance may not be
73 called concurrently.
74
75 @see
76 https://en.wikipedia.org/wiki/Region-based_memory_management
77 */
78 class
79 BOOST_JSON_DECL
80 BOOST_SYMBOL_VISIBLE
81 monotonic_resource final
82 : public container::pmr::memory_resource
83 {
84 struct block;
85 struct block_base
86 {
87 void* p;
88 std::size_t avail;
89 std::size_t size;
90 block_base* next;
91 };
92
93 block_base buffer_;
94 block_base* head_ = &buffer_;
95 std::size_t next_size_ = 1024;
96 std::size_t initial_size_ = 1024;
97 storage_ptr upstream_;
98
99 static constexpr std::size_t min_size_ = 1024;
100 inline static constexpr std::size_t max_size();
101 inline static std::size_t round_pow2(
102 std::size_t n) noexcept;
103 inline static std::size_t next_pow2(
104 std::size_t n) noexcept;
105
106 public:
107 /** Assignment operator.
108
109 Copy assignment operator is deleted. This type is not copyable or
110 movable.
111 */
112 monotonic_resource& operator=(
113 monotonic_resource const&) = delete;
114
115 /** Destructor.
116
117 Deallocates all the memory owned by this resource.
118
119 @par Effects
120 @code
121 release();
122 @endcode
123
124 @par Complexity
125 Linear in the number of deallocations performed.
126
127 @par Exception Safety
128 No-throw guarantee.
129 */
130 ~monotonic_resource();
131
132 /** Constructors.
133
134 Construct the resource.
135
136 @li **(1)** indicates that the first internal dynamic allocation shall
137 be at least `initial_size` bytes.
138 @li **(2)**--**(5)** indicate that subsequent allocations should use
139 the specified caller-owned buffer. When this buffer is exhausted,
140 dynamic allocations from the upstream resource are made.
141 @li **(6)** copy constructor is deleted. This type is not copyable or
142 movable.
143
144 None of the constructors performs any dynamic allocations.
145
146 @par Complexity
147 Constant.
148
149 @par Exception Safety
150 No-throw guarantee.
151
152 @param initial_size The size of the first internal dynamic allocation.
153 If this is lower than the implementation-defined lower limit,
154 then the lower limit is used instead.
155 @param upstream An optional upstream memory resource to use for
156 performing internal dynamic allocations. If this parameter is
157 omitted, the \<\<default_memory_resource,default resource\>\> is
158 used.
159
160 @{
161 */
162 explicit
163 monotonic_resource(
164 std::size_t initial_size = 1024,
165 storage_ptr upstream = {}) noexcept;
166
167 /** Overload
168
169 @param buffer The buffer to use. Ownership is not transferred; the
170 caller is responsible for ensuring that the lifetime of the
171 buffer extends until the resource is destroyed.
172 @param size The number of valid bytes pointed to by `buffer`.
173 @param upstream
174 */
175 monotonic_resource(
176 unsigned char* buffer,
177 std::size_t size,
178 storage_ptr upstream = {}) noexcept;
179
180 #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
181 /// Overload
182 monotonic_resource(
183 std::byte* buffer,
184 std::size_t size,
185 storage_ptr upstream) noexcept
186 : monotonic_resource(reinterpret_cast<
187 unsigned char*>(buffer), size,
188 std::move(upstream))
189 {
190 }
191 #endif
192
193 /// Overload
194 template<std::size_t N>
195 explicit
196 2x monotonic_resource(
197 unsigned char(&buffer)[N],
198 storage_ptr upstream = {}) noexcept
199 : monotonic_resource(&buffer[0],
200 2x N, std::move(upstream))
201 {
202 2x }
203
204 #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
205 /// Overload
206 template<std::size_t N>
207 explicit
208 monotonic_resource(
209 std::byte(&buffer)[N],
210 storage_ptr upstream = {}) noexcept
211 : monotonic_resource(&buffer[0],
212 N, std::move(upstream))
213 {
214 }
215 #endif
216
217 #ifndef BOOST_JSON_DOCS
218 // Safety net for accidental buffer overflows
219 template<std::size_t N>
220 monotonic_resource(
221 unsigned char(&buffer)[N],
222 std::size_t n,
223 storage_ptr upstream = {}) noexcept
224 : monotonic_resource(&buffer[0],
225 n, std::move(upstream))
226 {
227 // If this goes off, check your parameters
228 // closely, chances are you passed an array
229 // thinking it was a pointer.
230 BOOST_ASSERT(n <= N);
231 }
232
233 #ifdef __cpp_lib_byte
234 // Safety net for accidental buffer overflows
235 template<std::size_t N>
236 monotonic_resource(
237 std::byte(&buffer)[N],
238 std::size_t n,
239 storage_ptr upstream = {}) noexcept
240 : monotonic_resource(&buffer[0],
241 n, std::move(upstream))
242 {
243 // If this goes off, check your parameters
244 // closely, chances are you passed an array
245 // thinking it was a pointer.
246 BOOST_ASSERT(n <= N);
247 }
248 #endif
249 #endif
250
251 /// Overload
252 monotonic_resource(
253 monotonic_resource const&) = delete;
254 /// @}
255
256 /** Release all allocated memory.
257
258 This function deallocates all allocated memory.
259 If an initial buffer was provided upon construction,
260 then all of the bytes will be available again for
261 allocation. Allocated memory is deallocated even
262 if deallocate has not been called for some of
263 the allocated blocks.
264
265 @par Complexity
266 Linear in the number of deallocations performed.
267
268 @par Exception Safety
269 No-throw guarantee.
270 */
271 void
272 release() noexcept;
273
274 protected:
275 #ifndef BOOST_JSON_DOCS
276 void*
277 do_allocate(
278 std::size_t n,
279 std::size_t align) override;
280
281 void
282 do_deallocate(
283 void* p,
284 std::size_t n,
285 std::size_t align) override;
286
287 bool
288 do_is_equal(
289 memory_resource const& mr) const noexcept override;
290 #endif
291 };
292
293 #ifdef _MSC_VER
294 #pragma warning(pop)
295 #endif
296
297 template<>
298 struct is_deallocate_trivial<
299 monotonic_resource>
300 {
301 static constexpr bool value = true;
302 };
303
304 } // namespace json
305 } // namespace boost
306
307 #endif
308