Blame view

3rdparty/boost_1_81_0/boost/json/monotonic_resource.hpp 9.5 KB
63e88f80   Hu Chunming   提交三方库
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
  //
  // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
  // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  //
  // 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/boostorg/json
  //
  
  #ifndef BOOST_JSON_MONOTONIC_RESOURCE_HPP
  #define BOOST_JSON_MONOTONIC_RESOURCE_HPP
  
  #include <boost/json/detail/config.hpp>
  #include <boost/json/memory_resource.hpp>
  #include <boost/json/storage_ptr.hpp>
  #include <cstddef>
  #include <utility>
  
  BOOST_JSON_NS_BEGIN
  
  #ifdef _MSC_VER
  #pragma warning(push)
  #pragma warning(disable: 4251) // class needs to have dll-interface to be used by clients of class
  #pragma warning(disable: 4275) // non dll-interface class used as base for dll-interface class
  #endif
  
  //----------------------------------------------------------
  
  /** A dynamically allocating resource with a trivial deallocate
  
      This memory resource is a special-purpose resource
      that releases allocated memory only when the resource
      is destroyed (or when @ref release is called).
      It has a trivial deallocate function; that is, the
      metafunction @ref is_deallocate_trivial returns `true`.
  \n
      The resource can be constructed with an initial buffer.
      If there is no initial buffer, or if the buffer is
      exhausted, subsequent dynamic allocations are made from
      the system heap. The size of buffers obtained in this
      fashion follow a geometric progression.
  \n
      The purpose of this resource is to optimize the use
      case for performing many allocations, followed by
      deallocating everything at once. This is precisely the
      pattern of memory allocation which occurs when parsing:
      allocation is performed for each parsed element, and
      when the the resulting @ref value is no longer needed,
      the entire structure is destroyed. However, it is not
      suited for modifying the value after parsing is
      complete; reallocations waste memory, since the
      older buffer is not reclaimed until the resource
      is destroyed.
  
      @par Example
  
      This parses a JSON into a value which uses a local
      stack buffer, then prints the result.
  
      @code
  
      unsigned char buf[ 4000 ];
      monotonic_resource mr( buf );
  
      // Parse the string, using our memory resource
      auto const jv = parse( "[1,2,3]", &mr );
  
      // Print the JSON
      std::cout << jv;
  
      @endcode
  
      @note The total amount of memory dynamically
      allocated is monotonically increasing; That is,
      it never decreases.
  
      @par Thread Safety
      Members of the same instance may not be
      called concurrently.
  
      @see
          https://en.wikipedia.org/wiki/Region-based_memory_management
  */
  class BOOST_JSON_CLASS_DECL
      monotonic_resource final
      : public memory_resource
  {
      struct block;
      struct block_base
      {
          void* p;
          std::size_t avail;
          std::size_t size;
          block_base* next;
      };
  
      block_base buffer_;
      block_base* head_ = &buffer_;
      std::size_t next_size_ = 1024;
      storage_ptr upstream_;
  
      static constexpr std::size_t min_size_ = 1024;
      inline static constexpr std::size_t max_size();
      inline static std::size_t round_pow2(
          std::size_t n) noexcept;
      inline static std::size_t next_pow2(
          std::size_t n) noexcept;
  
  public:
      /// Copy constructor (deleted)
      monotonic_resource(
          monotonic_resource const&) = delete;
  
      /// Copy assignment (deleted)
      monotonic_resource& operator=(
          monotonic_resource const&) = delete;
  
      /** Destructor
  
          Deallocates all the memory owned by this resource.
  
          @par Effects
          @code
          this->release();
          @endcode
  
          @par Complexity
          Linear in the number of deallocations performed.
  
          @par Exception Safety
          No-throw guarantee.
      */
      ~monotonic_resource();
  
      /** Constructor
  
          This constructs the resource and indicates
          that the first internal dynamic allocation
          shall be at least `initial_size` bytes.
      \n
          This constructor is guaranteed not to perform
          any dynamic allocations.
  
          @par Complexity
          Constant.
  
          @par Exception Safety
          No-throw guarantee.
  
          @param initial_size The size of the first
          internal dynamic allocation. If this is lower
          than the implementation-defined lower limit, then
          the lower limit is used instead.
  
          @param upstream An optional upstream memory resource
          to use for performing internal dynamic allocations.
          If this parameter is omitted, the default resource
          is used.
      */
      explicit
      monotonic_resource(
          std::size_t initial_size = 1024,
          storage_ptr upstream = {}) noexcept;
  
      /** Constructor
  
          This constructs the resource and indicates that
          subsequent allocations should use the specified
          caller-owned buffer.
          When this buffer is exhausted, dynamic allocations
          from the upstream resource are made.
      \n
          This constructor is guaranteed not to perform
          any dynamic allocations.
  
          @par Complexity
          Constant.
  
          @par Exception Safety
          No-throw guarantee.
  
          @param buffer The buffer to use.
          Ownership is not transferred; the caller is
          responsible for ensuring that the lifetime of
          the buffer extends until the resource is destroyed.
  
          @param size The number of valid bytes pointed
          to by `buffer`.
  
          @param upstream An optional upstream memory resource
          to use for performing internal dynamic allocations.
          If this parameter is omitted, the default resource
          is used.
      */
      /** @{ */
      monotonic_resource(
          unsigned char* buffer,
          std::size_t size,
          storage_ptr upstream = {}) noexcept;
  
  #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
      monotonic_resource(
          std::byte* buffer,
          std::size_t size,
          storage_ptr upstream) noexcept
          : monotonic_resource(reinterpret_cast<
              unsigned char*>(buffer), size,
                  std::move(upstream))
      {
      }
  #endif
      /** @} */
  
      /** Constructor
  
          This constructs the resource and indicates that
          subsequent allocations should use the specified
          caller-owned buffer.
          When this buffer is exhausted, dynamic allocations
          from the upstream resource are made.
      \n
          This constructor is guaranteed not to perform
          any dynamic allocations.
  
          @par Complexity
          Constant.
  
          @par Exception Safety
          No-throw guarantee.
  
          @param buffer The buffer to use.
          Ownership is not transferred; the caller is
          responsible for ensuring that the lifetime of
          the buffer extends until the resource is destroyed.
  
          @param upstream An optional upstream memory resource
          to use for performing internal dynamic allocations.
          If this parameter is omitted, the default resource
          is used.
      */
      /** @{ */
      template<std::size_t N>
      explicit
      monotonic_resource(
          unsigned char(&buffer)[N],
          storage_ptr upstream = {}) noexcept
          : monotonic_resource(&buffer[0],
              N, std::move(upstream))
      {
      }
  
  #if defined(__cpp_lib_byte) || defined(BOOST_JSON_DOCS)
      template<std::size_t N>
      explicit
      monotonic_resource(
          std::byte(&buffer)[N],
          storage_ptr upstream = {}) noexcept
          : monotonic_resource(&buffer[0],
              N, std::move(upstream))
      {
      }
  #endif
      /** @} */
  
  #ifndef BOOST_JSON_DOCS
      // Safety net for accidental buffer overflows
      template<std::size_t N>
      monotonic_resource(
          unsigned char(&buffer)[N],
          std::size_t n,
          storage_ptr upstream = {}) noexcept
          : monotonic_resource(&buffer[0],
              n, std::move(upstream))
      {
          // If this goes off, check your parameters
          // closely, chances are you passed an array
          // thinking it was a pointer.
          BOOST_ASSERT(n <= N);
      }
  
  #ifdef __cpp_lib_byte
      // Safety net for accidental buffer overflows
      template<std::size_t N>
      monotonic_resource(
          std::byte(&buffer)[N],
          std::size_t n,
          storage_ptr upstream = {}) noexcept
          : monotonic_resource(&buffer[0],
              n, std::move(upstream))
      {
          // If this goes off, check your parameters
          // closely, chances are you passed an array
          // thinking it was a pointer.
          BOOST_ASSERT(n <= N);
      }
  #endif
  #endif
  
      /** Release all allocated memory.
  
          This function deallocates all allocated memory.
          If an initial buffer was provided upon construction,
          then all of the bytes will be available again for
          allocation. Allocated memory is deallocated even
          if deallocate has not been called for some of
          the allocated blocks.
  
          @par Complexity
          Linear in the number of deallocations performed.
  
          @par Exception Safety
          No-throw guarantee.
      */
      void
      release() noexcept;
  
  protected:
  #ifndef BOOST_JSON_DOCS
      void*
      do_allocate(
          std::size_t n,
          std::size_t align) override;
  
      void
      do_deallocate(
          void* p,
          std::size_t n,
          std::size_t align) override;
  
      bool
      do_is_equal(
          memory_resource const& mr) const noexcept override;
  #endif
  };
  
  #ifdef _MSC_VER
  #pragma warning(pop)
  #endif
  
  template<>
  struct is_deallocate_trivial<
      monotonic_resource>
  {
      static constexpr bool value = true;
  };
  
  BOOST_JSON_NS_END
  
  #endif