1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9// UNSUPPORTED: c++03, c++11, c++14
10// TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11// UNSUPPORTED: availability-pmr-missing
12
13// <memory_resource>
14
15// class synchronized_pool_resource
16
17#include <cassert>
18#include <cstddef>
19#include <memory> // std::align
20#include <memory_resource>
21
22#include "count_new.h"
23#include "test_macros.h"
24
25bool is_aligned_to(void* p, std::size_t alignment) {
26 void* p2 = p;
27 std::size_t space = 1;
28 void* result = std::align(align: alignment, size: 1, ptr&: p2, space&: space);
29 return (result == p);
30}
31
32int main(int, char**) {
33 globalMemCounter.reset();
34 std::pmr::pool_options opts{.max_blocks_per_chunk: 1, .largest_required_pool_block: 1024};
35 std::pmr::synchronized_pool_resource sync1(opts, std::pmr::new_delete_resource());
36 std::pmr::memory_resource& r1 = sync1;
37
38 constexpr std::size_t big_alignment = 8 * alignof(std::max_align_t);
39 static_assert(big_alignment > 4);
40
41 assert(globalMemCounter.checkNewCalledEq(0));
42
43 void* ret = r1.allocate(bytes: 2048, alignment: big_alignment);
44 assert(ret != nullptr);
45 assert(is_aligned_to(ret, big_alignment));
46 ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledGreaterThan(0));
47
48 ret = r1.allocate(bytes: 16, alignment: 4);
49 assert(ret != nullptr);
50 assert(is_aligned_to(ret, 4));
51 ASSERT_WITH_LIBRARY_INTERNAL_ALLOCATIONS(globalMemCounter.checkNewCalledGreaterThan(1));
52
53 return 0;
54}
55

source code of libcxx/test/std/utilities/utility/mem.res/mem.res.pool/mem.res.pool.mem/sync_allocate_overaligned_request.pass.cpp