// versions will not use this symbol.
monotonic_buffer_resource::~monotonic_buffer_resource() { release(); }
- namespace {
-
+namespace
+{
// aligned_size<N> stores the size and alignment of a memory allocation.
// The size must be a multiple of N, leaving the low log2(N) bits free
// to store the base-2 logarithm of the alignment.
return (n + alignment - 1) & ~(alignment - 1);
}
- } // namespace
+} // namespace
// Memory allocated by the upstream resource is managed in a linked list
// of _Chunk objects. A _Chunk object recording the size and alignment of
// Helper types for synchronized_pool_resource & unsynchronized_pool_resource
- namespace {
-
+namespace
+{
// Simple bitset with runtime size.
// Tracks which blocks in a pool chunk are used/unused.
struct bitset
static_assert(sizeof(big_block) == (2 * sizeof(void*)));
- } // namespace
+} // namespace
// A pool that serves blocks of a particular size.
// Each pool manages a number of chunks.
using big_block::big_block;
};
- namespace {
+namespace
+{
+ // N.B. it is important that we don't skip any power of two sizes if there
+ // is a non-power of two size between them, e.g. must not have pool sizes
+ // of 24 and 40 without having a pool size of 32. Otherwise an allocation
+ // of 32 bytes with alignment 16 would choose the 40-byte pool which is not
+ // correctly aligned for 16-byte alignment. It would be OK (but suboptimal)
+ // to have no pool of size 32 if we have pool sizes of 16 and 64 and no
+ // non-power of two sizes between those, because the example of (32, 16)
+ // would choose the 64-byte pool, which would be correctly aligned.
constexpr size_t pool_sizes[] = {
8, 16, 24,
using exclusive_lock = lock_guard<shared_mutex>;
#endif
- } // namespace
+} // namespace
__pool_resource::
__pool_resource(const pool_options& opts, memory_resource* upstream)
return p;
}
+ // Determine the appropriate allocation size, rounding up to a multiple
+ // of the alignment if needed.
+ static inline size_t
+ choose_block_size(size_t bytes, size_t alignment)
+ {
+ if (bytes == 0) [[unlikely]]
+ return alignment;
+
+ // Use bit_ceil in case alignment is invalid (i.e. not a power of two).
+ size_t mask = std::__bit_ceil(alignment) - 1;
+ // Round up to a multiple of alignment.
+ size_t block_size = (bytes + mask) & ~mask;
+
+ if (block_size >= bytes) [[likely]]
+ return block_size;
+
+ // Wrapped around to zero, bytes must have been impossibly large.
+ return numeric_limits<size_t>::max();
+ }
+
+
#ifdef _GLIBCXX_HAS_GTHREADS
// synchronized_pool_resource members.
/* Notes on implementation and thread safety:
*
- * Each synchronized_pool_resource manages an linked list of N+1 _TPools
+ * Each synchronized_pool_resource manages a linked list of N+1 _TPools
* objects, where N is the number of threads using the pool resource.
* Each _TPools object has its own set of pools, with their own chunks.
* The first element of the list, _M_tpools[0], can be used by any thread.
synchronized_pool_resource::
do_allocate(size_t bytes, size_t alignment)
{
- const auto block_size = std::max(bytes, alignment);
+ const auto block_size = choose_block_size(bytes, alignment);
const pool_options opts = _M_impl._M_opts;
if (block_size <= opts.largest_required_pool_block)
{
synchronized_pool_resource::
do_deallocate(void* p, size_t bytes, size_t alignment)
{
- size_t block_size = std::max(bytes, alignment);
+ size_t block_size = choose_block_size(bytes, alignment);
if (block_size <= _M_impl._M_opts.largest_required_pool_block)
{
const ptrdiff_t index = pool_index(block_size, _M_impl._M_npools);
void*
unsynchronized_pool_resource::do_allocate(size_t bytes, size_t alignment)
{
- const auto block_size = std::max(bytes, alignment);
+ const auto block_size = choose_block_size(bytes, alignment);
if (block_size <= _M_impl._M_opts.largest_required_pool_block)
{
// Recreate pools if release() has been called:
unsynchronized_pool_resource::
do_deallocate(void* p, size_t bytes, size_t alignment)
{
- size_t block_size = std::max(bytes, alignment);
+ size_t block_size = choose_block_size(bytes, alignment);
if (block_size <= _M_impl._M_opts.largest_required_pool_block)
{
if (auto pool = _M_find_pool(block_size))
--- /dev/null
+// { dg-do run { target c++17 } }
+// Bug 118681 - unsynchronized_pool_resource may fail to respect alignment
+
+#include <memory_resource>
+#include <cstdio>
+#include <testsuite_hooks.h>
+
+#ifndef RESOURCE
+# define RESOURCE std::pmr::unsynchronized_pool_resource
+#endif
+
+bool any_misaligned = false;
+
+bool
+is_aligned(void* p, [[maybe_unused]] std::size_t size, std::size_t alignment)
+{
+ const bool misaligned = reinterpret_cast<std::uintptr_t>(p) % alignment;
+#ifdef DEBUG
+ std::printf("allocate(%2zu, %2zu): %p is aligned %scorrectly\n",
+ size, alignment, p, misaligned ? "in" : "");
+ any_misaligned |= misaligned;
+ return true;
+#endif
+ return ! misaligned;
+}
+
+void
+test_alignment(std::pmr::memory_resource& res, bool dealloc)
+{
+ for (std::size_t alignment : { 8, 16, 32, 64 })
+ {
+ for (std::size_t size : { 9, 12, 24, 40, 48, 56, 72 })
+ {
+ void* p1 = res.allocate(size, alignment);
+ void* p2 = res.allocate(size, alignment);
+
+ VERIFY( is_aligned(p1, size, alignment) );
+ VERIFY( is_aligned(p2, size, alignment) );
+
+ if (dealloc)
+ {
+ res.deallocate(p1, size, alignment);
+ res.deallocate(p2, size, alignment);
+ }
+ }
+ }
+}
+
+int main()
+{
+ RESOURCE res;
+ test_alignment(res, true);
+ res.release();
+ test_alignment(res, false);
+ res.release();
+
+ VERIFY( ! any_misaligned );
+}