libstdc++: handle small max_blocks_per_chunk in pool resources [PR 94160]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 10 Sep 2020 14:39:15 +0000 (15:39 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Thu, 10 Sep 2020 14:42:09 +0000 (15:42 +0100)
When a pool resource is constructed with max_blocks_per_chunk=1 it ends
up creating a pool with blocks_per_chunk=0 which means it never
allocates anything. Instead it returns null pointers, which should be
impossible.

To avoid this problem, round the max_blocks_per_chunk value to a
multiple of four, so it's never smaller than four.

libstdc++-v3/ChangeLog:

PR libstdc++/94160
* src/c++17/memory_resource.cc (munge_options): Round
max_blocks_per_chunk to a multiple of four.
(__pool_resource::_M_alloc_pools()): Simplify slightly.
* testsuite/20_util/unsynchronized_pool_resource/allocate.cc:
Check that valid pointers are returned when small values are
used for max_blocks_per_chunk.

libstdc++-v3/src/c++17/memory_resource.cc
libstdc++-v3/testsuite/20_util/unsynchronized_pool_resource/allocate.cc

index 392d72ef32df472ffefa5f5be58824625407dbd2..c04ea6a5da4eca9fbb67f876459394864b18cd6c 100644 (file)
@@ -898,7 +898,18 @@ namespace pmr
       }
     else
       {
-       // TODO round to preferred granularity ?
+       // Round to preferred granularity.
+       if (opts.max_blocks_per_chunk < size_t(-4))
+         {
+           // round up
+           opts.max_blocks_per_chunk
+             = aligned_ceil(opts.max_blocks_per_chunk, 4);
+         }
+       else
+         {
+           // round down
+           opts.max_blocks_per_chunk &= ~size_t(3);
+         }
       }
 
     if (opts.max_blocks_per_chunk > chunk::max_blocks_per_chunk())
@@ -1039,11 +1050,9 @@ namespace pmr
          : pool_sizes[i];
 
        // Decide on initial number of blocks per chunk.
-       // Always have at least 16 blocks per chunk:
-       const size_t min_blocks_per_chunk = 16;
-       // But for smaller blocks, use a larger initial size:
-       size_t blocks_per_chunk
-         = std::max(1024 / block_size, min_blocks_per_chunk);
+       // At least 16 blocks per chunk seems reasonable,
+       // more for smaller blocks:
+       size_t blocks_per_chunk = std::max(size_t(16), 1024 / block_size);
        // But don't exceed the requested max_blocks_per_chunk:
        blocks_per_chunk
          = std::min(blocks_per_chunk, _M_opts.max_blocks_per_chunk);
index 2775e1260ea1704fd5d1ca89322821e1fb7eb472..526b7ede13a441d3d84700d84535cff566e8a32e 100644 (file)
@@ -300,6 +300,25 @@ test07()
   }
 }
 
+void
+test08()
+{
+  std::pmr::pool_options opts;
+  opts.largest_required_pool_block = 64;
+
+  // PR libstdc++/94160
+  // max_blocks_per_chunk=1 causes pool resources to return null pointers
+  for (int i = 0; i < 8; ++i)
+  {
+    opts.max_blocks_per_chunk = i;
+    std::pmr::unsynchronized_pool_resource upr(opts);
+    auto* p = (int*)upr.allocate(4);
+    VERIFY( p != nullptr );
+    *p = i;
+    upr.deallocate(p, 4);
+  }
+}
+
 int
 main()
 {
@@ -310,4 +329,5 @@ main()
   test05();
   test06();
   test07();
+  test08();
 }