Skip to content

Commit

Permalink
pool resources: respect max_blocks_per_chunk
Browse files Browse the repository at this point in the history
... even when less than `_Default_next_capacity`.

Fixes microsoft#3408
  • Loading branch information
CaseyCarter committed Feb 27, 2023
1 parent 16bb556 commit 0e9c287
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
4 changes: 4 additions & 0 deletions stl/inc/memory_resource
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,10 @@ namespace pmr {

void _Increase_capacity(unsynchronized_pool_resource& _Pool_resource) {
// this pool has no free blocks; get a new chunk from upstream
if (_Next_capacity > _Pool_resource._Options.max_blocks_per_chunk) {
// This is a fresh pool, _Next_capacity hasn't yet been bounded by max_blocks_per_chunk:
_Next_capacity = _Pool_resource._Options.max_blocks_per_chunk;
}
const size_t _Size = _Size_for_capacity(_Next_capacity);
memory_resource* const _Resource = _Pool_resource.upstream_resource();
void* const _Ptr = _Resource->allocate(_Size, _Block_size);
Expand Down
15 changes: 15 additions & 0 deletions tests/std/tests/P0220R1_polymorphic_memory_resources/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1546,6 +1546,19 @@ namespace {
al.construct(static_cast<const volatile PairType*>(raw_ptr), mem_pair_conv{});
}
} // namespace map_containers

void test_gh3408() {
// We ignored the possibility that max_blocks_per_chunk could be less than _Default_next_capacity
recording_resource upstream;
std::pmr::pool_options options{};
options.max_blocks_per_chunk = 1;
std::pmr::unsynchronized_pool_resource res{options, &upstream};
const std::size_t size = 0x8009;
(void) res.allocate(size);
const allocation& alloc = upstream.allocations_[upstream.allocations_.size() - 1];
CHECK(alloc.size >= 0x10000);
CHECK(alloc.size < 2 * 0x10000);
}
} // unnamed namespace

int main() {
Expand Down Expand Up @@ -1591,4 +1604,6 @@ int main() {
map_containers::test();

map_containers::lwg3677_test();

test_gh3408();
}

0 comments on commit 0e9c287

Please sign in to comment.