Repository navigation
Conversation
do_allocate doubles next_size_ for every block it obtains from the upstream, and release() returns all the blocks but keeps next_size_. A resource that is reused by calling release() between rounds of work therefore asks the upstream for a block twice as large on every round, even when each round allocates the same amount: the 20th round asks for more than 4 GiB to satisfy a 4000-byte allocation, and soon every allocation that needs a new block fails with bad_alloc. Remember the initial block size and restore it in release().
|
An automated preview of the documentation is available at https://1227.json.prtest2.cppalliance.org/libs/json/doc/html/index.html If more commits are pushed to the pull request, the docs will rebuild at the same URL. 2026-11-20 14:53:01 UTC |
|
GCOVR code coverage report https://1227.json.prtest2.cppalliance.org/gcovr/index.html Build time: 2026-11-20 15:11:24 UTC |
|
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## develop #1227 +/- ##
========================================
Coverage 93.98% 93.98%
========================================
Files 85 85
Lines 8974 8977 +3
========================================
+ Hits 8434 8437 +3
Misses 540 540
Continue to review full report in Codecov by Harness.
🚀 New features to boost your workflow:
|

monotonic_resource::release()returns all blocks to the upstream but keeps the size of the next block, whichdo_allocatedoubles every time it obtains a block. A resource that is reused withrelease()between rounds of work therefore asks the upstream for a block twice as large on every round, even when every round allocates the same amount. After a couple dozen rounds the requested block can no longer be allocated, and from then on every allocation that needs a new block throwsbad_alloc.release()is documented to release all allocated memory, and the class documentation names the parse-then-free-everything pattern as the use case. A newly constructed resource does each of these rounds with one 4128-byte block.Repro (develop e93b106, clang 21
-O2, libc++, Linux x86-64):Output on develop:
Continuing the first loop, round 20 would ask for 4294967328 bytes. In the second loop the requests to the default resource fail from round 25 on, while the process never uses more than a few MB: it is the size of the request that fails, not a lack of memory. With this change every round asks for 4128 bytes and no round fails.
Cause:
do_allocatesetsnext_size_ = next_pow2(next_size_)after each new block.release()rewinds the initial buffer and the block list (the latter since 1b73c77) but notnext_size_.Fix: remember the initial block size computed by the constructors and restore
next_size_to it inrelease(). This adds astd::size_tmember tomonotonic_resource.Test:
testReleaseintest/monotonic_resource.cppreleases and allocates again a few times, with and without an initial buffer, and checks the size of the block requested from the upstream each time. It fails on develop and passes with the change. The other tests that usemonotonic_resource(array, object, string, value, stream_parser, memory_resource and the doc tests) also pass, built directly with clang 21 and libc++ under ASan and UBSan.