forked from sdarwin/json
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory_resource.cpp
More file actions
378 lines (281 loc) · 8.5 KB
/
Copy pathmemory_resource.cpp
File metadata and controls
378 lines (281 loc) · 8.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//
// Copyright (c) 2019 Vinnie Falco ([email protected])
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/json
//
// Test that header file is self-contained.
#include <boost/json/memory_resource.hpp>
#include <boost/json/monotonic_resource.hpp>
#include <boost/json/static_resource.hpp>
#include <boost/json/value.hpp>
#ifndef BOOST_JSON_STANDALONE
#include <boost/container/pmr/vector.hpp>
#endif
#include <vector>
#include "test_suite.hpp"
#ifdef assert
#undef assert
#endif
#define assert BOOST_TEST
BOOST_JSON_NS_BEGIN
namespace snippets1 {
//--------------------------------------
namespace std {
template<class T>
using allocator = ::std::allocator<T>;
} // std
//[snippet_background_1
namespace std {
template< class T, class Allocator = std::allocator< T > >
class vector;
} // namespace std
//]
//--------------------------------------
//[snippet_background_2
namespace std {
template< class T, class Allocator >
class vector
{
public:
explicit vector( Allocator const& alloc );
//...
//]
};
template<class T, class A>
vector<T,A>::vector(A const&){}
}
//--------------------------------------
//[snippet_background_3
namespace std {
namespace pmr {
class memory_resource
{
public:
virtual ~memory_resource();
void* allocate ( size_t bytes, size_t alignment );
void deallocate( void* p, size_t bytes, size_t alignment );
bool is_equal ( const memory_resource& other ) const;
protected:
virtual void* do_allocate ( size_t bytes, size_t alignment ) = 0;
virtual void do_deallocate( void* p, size_t bytes, size_t alignment ) = 0;
virtual bool do_is_equal ( memory_resource const& other ) const noexcept = 0;
};
} // namespace pmr
} // namespace std
//]
//--------------------------------------
//[snippet_background_4
namespace std {
namespace pmr {
template< class T >
using vector = std::vector< T, polymorphic_allocator< T > >;
} // namespace pmr
} // namespace std
//]
//--------------------------------------
struct my_resource : memory_resource
{
void* do_allocate ( size_t, size_t ) override { return 0; }
void do_deallocate( void*, size_t, size_t ) override {}
bool do_is_equal ( memory_resource const& ) const noexcept override { return true; }
};
//[snippet_background_7
namespace my_library {
std::pmr::vector<char> get_chars1()
{
// This leaks memory because `v` does not own the memory resource
std::pmr::vector<char> v( new my_resource );
return v;
}
} // my_library
//]
//--------------------------------------
//[snippet_background_8
namespace my_library {
std::pmr::vector<char> get_chars2()
{
// Declare a local memory resource
my_resource mr;
// Construct a vector that uses our resource
std::pmr::vector<char> v( &mr );
// Undefined behavior, `mr` goes out of scope!
return v;
}
} // my_library
//]
//--------------------------------------
} // snippets
namespace snippets2 {
#ifdef BOOST_JSON_STANDALONE
template<class T>
using vector = std::pmr::vector<T>;
#else
template<class T>
using vector = boost::container::pmr::vector<T>;
#endif
} // snippets2
class memory_resource_test
{
public:
void
testSnippets()
{
{
using namespace snippets2;
struct T {};
//[snippet_background_5
// A type of memory resource
monotonic_resource mr;
// Construct a vector using the monotonic buffer resource
vector< T > v1(( polymorphic_allocator< T >(&mr) ));
// Or this way, since construction from memory_resource* is implicit:
vector< T > v2( &mr );
//]
}
//----------------------------------
{
using namespace snippets2;
//[snippet_background_6
{
// A type of memory resource which uses a stack buffer
char temp[4096];
static_resource mr( temp, sizeof(temp) );
// Construct a vector using the static buffer resource
vector< value > v( &mr );
// The vector will allocate from `temp` first, and then the heap.
}
//]
}
//----------------------------------
{
//[snippet_uses_allocator_1
// We want to use this resource for all the containers
monotonic_resource mr;
// Declare a vector of JSON values
std::vector< value, polymorphic_allocator< value > > v( &mr );
// The polymorphic allocator will use our resource
assert( v.get_allocator().resource() == &mr );
// Add a string to the vector
v.emplace_back( "boost" );
// The vector propagates the memory resource to the string
assert( v[0].storage().get() == &mr );
//]
}
{
//[snippet_uses_allocator_2
// This vector will use the default memory resource
std::vector< value, polymorphic_allocator < value > > v;
// This value will same memory resource as the vector
value jv( v.get_allocator() );
// However, ownership is not transferred,
assert( ! jv.storage().is_counted() );
// and deallocate is never null
assert( ! jv.storage().is_deallocate_trivial() );
//]
}
}
void
testBoostPmr()
{
#ifndef BOOST_JSON_STANDALONE
using allocator_type =
polymorphic_allocator<value>;
// pass polymorphic_allocator
// where storage_ptr is expected
{
monotonic_resource mr;
value jv( allocator_type{&mr} );
object o( allocator_type{&mr} );
array a( allocator_type{&mr} );
string s( allocator_type{&mr} );
}
{
monotonic_resource mr;
allocator_type a(&mr);
boost::container::pmr::vector<value> v1(a);
v1.resize(3);
BOOST_TEST(v1[1].storage().get() == &mr);
std::vector<value, allocator_type> v2(3, {}, a);
BOOST_TEST(v2[1].storage().get() == &mr);
}
#endif
}
void
testStdPmr()
{
#ifdef BOOST_JSON_STANDALONE
using allocator_type =
std::pmr::polymorphic_allocator<value>;
// pass polymorphic_allocator
// where storage_ptr is expected
{
value jv( allocator_type{} );
object o( allocator_type{} );
array a( allocator_type{} );
string s( allocator_type{} );
}
{
monotonic_resource mr;
allocator_type a(&mr);
std::vector<value, allocator_type> v2(3, {}, a);
BOOST_TEST(v2[1].storage().get() == &mr);
}
#endif
}
// These are here instead of the type-specific
// test TUs, so that we only need to link to
// Boost.Container from one file.
void
testPmr()
{
// array
{
// get_allocator
{
monotonic_resource mr;
array a(&mr);
BOOST_TEST(a.get_allocator().resource() == &mr);
}
}
// object
{
// get_allocator
{
monotonic_resource mr;
object o(&mr);
BOOST_TEST(o.get_allocator().resource() == &mr);
}
}
// string
{
// get_allocator
{
monotonic_resource mr;
string s(&mr);
BOOST_TEST(s.get_allocator().resource() == &mr);
}
}
// value
{
// get_allocator
{
monotonic_resource mr;
value jv(&mr);
BOOST_TEST(jv.get_allocator().resource() == &mr);
}
}
}
void
run()
{
testSnippets();
testBoostPmr();
testStdPmr();
testPmr();
}
};
TEST_SUITE(memory_resource_test, "boost.json.memory_resource");
BOOST_JSON_NS_END