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, c++17
10// TODO: Change to XFAIL once https://github.com/llvm/llvm-project/issues/40340 is fixed
11// UNSUPPORTED: availability-pmr-missing
12
13// test_memory_resource requires RTTI for dynamic_cast
14// UNSUPPORTED: no-rtti
15
16// <memory_resource>
17
18// polymorphic_allocator::allocate_bytes()
19// polymorphic_allocator::deallocate_bytes()
20
21#include <algorithm>
22#include <cassert>
23#include <concepts>
24#include <memory_resource>
25
26#include "tracking_mem_res.h"
27
28template <class T>
29void test() {
30 std::size_t last_size = 0;
31 std::size_t last_alignment = 0;
32 TrackingMemRes resource(&last_size, &last_alignment);
33
34 std::pmr::polymorphic_allocator<T> allocator(&resource);
35
36 {
37 std::same_as<void*> decltype(auto) allocation = allocator.allocate_bytes(13);
38 auto ptr = static_cast<char*>(allocation);
39 std::fill(first: ptr, last: ptr + 13, value: '0');
40 assert(last_size == 13);
41 assert(last_alignment == alignof(std::max_align_t));
42 allocator.deallocate_bytes(allocation, 13);
43 assert(last_size == 13);
44 assert(last_alignment == alignof(std::max_align_t));
45 }
46 {
47 void* allocation = allocator.allocate_bytes(13, 64);
48 auto ptr = static_cast<char*>(allocation);
49 std::fill(first: ptr, last: ptr + 13, value: '0');
50 assert(last_size == 13);
51 assert(last_alignment == 64);
52 allocator.deallocate_bytes(allocation, 13, 64);
53 assert(last_size == 13);
54 assert(last_alignment == 64);
55 }
56}
57
58struct S {};
59
60int main(int, char**) {
61 test<std::byte>();
62 test<S>();
63
64 return 0;
65}
66

source code of libcxx/test/std/utilities/utility/mem.res/mem.poly.allocator.class/mem.poly.allocator.mem/allocate_deallocate_bytes.pass.cpp