@@ -19,11 +19,13 @@ simdjson_really_inline json_iterator &json_iterator::operator=(json_iterator &&o
19
19
return *this ;
20
20
}
21
21
simdjson_really_inline json_iterator::json_iterator (ondemand::parser *_parser) noexcept
22
- : token_iterator(_parser->dom_parser.buf, _parser->dom_parser.structural_indexes.get()), parser{_parser}
22
+ : token_iterator(_parser->dom_parser.buf, _parser->dom_parser.structural_indexes.get()),
23
+ parser{_parser},
24
+ current_string_buf_loc{parser->string_buf .get ()},
25
+ active_lease_depth{0 }
23
26
{
24
27
// Release the string buf so it can be reused by the next document
25
28
logger::log_headers ();
26
- current_string_buf_loc = parser->string_buf .get ();
27
29
}
28
30
simdjson_really_inline json_iterator::~json_iterator () noexcept = default ;
29
31
@@ -261,56 +263,69 @@ simdjson_really_inline bool json_iterator::is_alive() const noexcept {
261
263
}
262
264
263
265
simdjson_really_inline json_iterator_ref json_iterator::borrow () noexcept {
264
- return json_iterator_ref (this );
266
+ SIMDJSON_ASSUME (active_lease_depth == 0 );
267
+ const uint32_t child_depth = 1 ;
268
+ active_lease_depth = child_depth;
269
+ return json_iterator_ref (this , child_depth);
265
270
}
266
271
267
272
//
268
273
// json_iterator_ref
269
274
//
270
275
simdjson_really_inline json_iterator_ref::json_iterator_ref () noexcept = default;
271
276
simdjson_really_inline json_iterator_ref::json_iterator_ref (json_iterator_ref &&other) noexcept
272
- : iter{std::forward<json_iterator_ref>(other).iter }
277
+ : iter{std::exchange (other.iter , nullptr )},
278
+ lease_depth{other.lease_depth }
273
279
{
274
- other.iter = nullptr ;
275
280
}
276
281
simdjson_really_inline json_iterator_ref &json_iterator_ref::operator =(json_iterator_ref &&other) noexcept {
277
- iter = std::forward<json_iterator_ref>(other).iter ;
278
- other.iter = nullptr ;
282
+ SIMDJSON_ASSUME (!is_active ());
283
+ iter = std::exchange (other.iter , nullptr );
284
+ lease_depth = other.lease_depth ;
279
285
return *this ;
280
286
}
281
- simdjson_really_inline json_iterator_ref::json_iterator_ref (json_iterator *_iter) noexcept
282
- : iter{_iter}
287
+ simdjson_really_inline json_iterator_ref::json_iterator_ref (json_iterator *_iter, uint32_t _lease_depth) noexcept
288
+ : iter{_iter},
289
+ lease_depth{_lease_depth}
283
290
{
291
+ SIMDJSON_ASSUME (is_active ());
284
292
}
285
293
simdjson_really_inline json_iterator_ref::~json_iterator_ref () noexcept {
286
294
// The caller MUST consume their value and release the iterator before they die
287
- SIMDJSON_ASSUME (!iter );
295
+ SIMDJSON_ASSUME (!is_alive () );
288
296
}
289
297
290
298
simdjson_really_inline json_iterator_ref json_iterator_ref::borrow () noexcept {
291
- return json_iterator_ref (iter);
299
+ SIMDJSON_ASSUME (is_active ());
300
+ const uint32_t child_depth = lease_depth + 1 ;
301
+ iter->active_lease_depth = child_depth;
302
+ return json_iterator_ref (iter, child_depth);
292
303
}
293
304
simdjson_really_inline void json_iterator_ref::release () noexcept {
294
- SIMDJSON_ASSUME (is_alive ());
305
+ SIMDJSON_ASSUME (is_active ());
306
+ iter->active_lease_depth = lease_depth - 1 ;
295
307
iter = nullptr ;
296
308
}
297
309
298
310
simdjson_really_inline json_iterator *json_iterator_ref::operator ->() noexcept {
299
- SIMDJSON_ASSUME (is_alive ());
311
+ SIMDJSON_ASSUME (is_active ());
300
312
return iter;
301
313
}
302
314
simdjson_really_inline json_iterator &json_iterator_ref::operator *() noexcept {
303
- SIMDJSON_ASSUME (is_alive ());
315
+ SIMDJSON_ASSUME (is_active ());
304
316
return *iter;
305
317
}
306
318
simdjson_really_inline const json_iterator &json_iterator_ref::operator *() const noexcept {
307
- SIMDJSON_ASSUME (is_alive ());
319
+ SIMDJSON_ASSUME (is_active ());
308
320
return *iter;
309
321
}
310
322
311
323
simdjson_really_inline bool json_iterator_ref::is_alive () const noexcept {
312
324
return iter != nullptr ;
313
325
}
326
+ simdjson_really_inline bool json_iterator_ref::is_active () const noexcept {
327
+ return is_alive () && lease_depth == iter->active_lease_depth ;
328
+ }
314
329
315
330
316
331
} // namespace ondemand
0 commit comments