Debian Codegraph @

libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate-max-chunks.cc

raw
1 // Copyright (C) 2018-2025 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-do run { target c++17 } }
19
20 #include <memory_resource>
21 #include <testsuite_hooks.h>
22
23 struct custom_mr : std::pmr::memory_resource
24 {
25 custom_mr(std::size_t max) : max(max) { }
26
27 bool reached_max = false;
28
29 private:
30 std::size_t max;
31 std::size_t count = 0;
32
33 void* do_allocate(std::size_t b, std::size_t a)
34 {
35 if (b >= max)
36 reached_max = true;
37 count += b;
38 if (count > (18 * 1024 * 1024))
39 // Something went wrong, should not need to allocate this much.
40 throw std::bad_alloc();
41 return std::pmr::new_delete_resource()->allocate(b, a);
42 }
43
44 void do_deallocate(void* p, std::size_t b, std::size_t a)
45 { std::pmr::new_delete_resource()->deallocate(p, b, a); }
46
47 bool do_is_equal(const memory_resource& r) const noexcept
48 { return false; }
49 };
50
51 void
52 test01()
53 {
54 // Only going to allocate blocks of this size:
55 const std::size_t block_size = 8;
56 std::pmr::pool_options opts{};
57 // Use maximum allowed number of blocks per chunk:
58 opts.max_blocks_per_chunk = (std::size_t)-1;
59 opts.largest_required_pool_block = block_size;
60 {
61 std::pmr::unsynchronized_pool_resource r(opts);
62 // Get the real max_blocks_per_chunk that will be used:
63 opts = r.options();
64 // Sanity test in case chunk::max_blocks_per_chunk() changes,
65 // as that could make this test take much longer to run:
66 VERIFY( opts.max_blocks_per_chunk <= (1 << 19) );
67 }
68 custom_mr c(block_size * opts.max_blocks_per_chunk);
69 std::pmr::unsynchronized_pool_resource r(opts, &c);
70 // Keep allocating from the pool until reaching the maximum chunk size:
71 while (!c.reached_max)
72 (void) r.allocate(block_size, 1);
73 c.reached_max = false;
74 // Now fill that maximally-sized chunk
75 // (this used to go into an infinite loop ):
76 for (std::size_t i = 0; i < opts.max_blocks_per_chunk; ++i)
77 (void) r.allocate(block_size, 1);
78 // Should have filled the maximally-sized chunk and allocated another
79 // maximally-sized chunk from upstream:
80 VERIFY( c.reached_max );
81 }
82
83 int
84 main()
85 {
86 test01();
87 }