Debian Codegraph @

libstdc++-v3/testsuite/20_util/shared_ptr/creation/overwrite.cc

raw
1 // { dg-options "-fno-lifetime-dse -O0" }
2 // { dg-do run { target c++20 } }
3 // { dg-require-effective-target hosted }
4 // { dg-add-options no_pch }
5
6 // C++20 20.11.3.7 shared_ptr Creation [util.smartptr.shared.create]
7
8 #include <memory>
9
10 #ifndef __cpp_lib_smart_ptr_for_overwrite
11 # error "Feature-test macro for make_shared_for_overwrite missing in <memory>"
12 #elif __cpp_lib_smart_ptr_for_overwrite < 202002L
13 # error "Feature-test macro for make_shared_for_overwrite has wrong value in <memory>"
14 #endif
15
16 #include <cstring>
17 #include <testsuite_hooks.h>
18
19 int counter = 0;
20
21 template<typename T>
22 struct Alloc : std::allocator<T>
23 {
24 Alloc() = default;
25
26 template<typename U>
27 Alloc(const Alloc<U>&) { }
28
29 T* allocate(std::size_t n)
30 {
31 ++counter;
32 void* p = std::allocator<T>::allocate(n);
33 // need -fno-lifetime-dse to check for these values later.
34 std::memset(p, 0xff, n * sizeof(T));
35 return (T*)p;
36 }
37
38 void construct(auto*, auto&&...)
39 {
40 // The objects must be default-initialized, not using this function.
41 VERIFY( ! "allocator_traits::construct" );
42 }
43
44 void destroy(auto*)
45 {
46 // The objects must be destroyed by ~T(), not using this function.
47 VERIFY( ! "allocator_traits::destroy" );
48 }
49 };
50
51 void
52 test01()
53 {
54 Alloc<int> a;
55 const int expected = 0xffffffff;
56
57 std::shared_ptr<int> p1 = std::allocate_shared_for_overwrite<int>(a);
58 VERIFY( counter == 1 );
59 VERIFY( *p1 == expected );
60 std::shared_ptr<int[44]> p2 = std::allocate_shared_for_overwrite<int[44]>(a);
61 VERIFY( counter == 2 );
62 VERIFY( p2[0] == expected );
63 p2.reset();
64 std::shared_ptr<int[]> p3 = std::allocate_shared_for_overwrite<int[]>(a, 88);
65 VERIFY( counter == 3 );
66 VERIFY( p3[0] == expected );
67 VERIFY( p3[87] == expected );
68 std::shared_ptr<int[3][4]> p4 = std::allocate_shared_for_overwrite<int[3][4]>(a);
69 VERIFY( counter == 4 );
70 VERIFY( p4[0][0] == expected );
71 VERIFY( p4[2][3] == expected );
72 std::shared_ptr<int[][5]> p5 = std::allocate_shared_for_overwrite<int[][5]>(a, 6);
73 VERIFY( counter == 5 );
74 VERIFY( p5[0][0] == expected );
75 VERIFY( p5[5][4] == expected );
76
77 struct BigBoi { int x[100]; };
78 std::shared_ptr<BigBoi> p6 = std::allocate_shared_for_overwrite<BigBoi>(a);
79 VERIFY( counter == 6 );
80 VERIFY( p6->x[0] == expected );
81 std::shared_ptr<BigBoi[22]> p7 = std::allocate_shared_for_overwrite<BigBoi[22]>(a);
82 VERIFY( counter == 7 );
83 VERIFY( p7[0].x[0] == expected );
84 VERIFY( p7[21].x[99] == expected );
85 std::shared_ptr<BigBoi[]> p8 = std::allocate_shared_for_overwrite<BigBoi[]>(a, 11);
86 VERIFY( counter == 8 );
87 VERIFY( p8[0].x[0] == expected );
88 VERIFY( p8[10].x[10] == expected );
89 }
90
91 void
92 test02()
93 {
94 // These aren't created by the custom allocator, so we can't check that the
95 // memory was left uninitialized. Just dereference them.
96
97 std::shared_ptr<int> p1 = std::make_shared_for_overwrite<int>();
98 (void) *p1;
99 std::shared_ptr<int[44]> p2 = std::make_shared_for_overwrite<int[44]>();
100 (void) p2[0];
101 std::shared_ptr<int[]> p3 = std::make_shared_for_overwrite<int[]>(88);
102 (void) p3[0];
103 (void) p3[87];
104 std::shared_ptr<int[3][4]> p4 = std::make_shared_for_overwrite<int[3][4]>();
105 (void) p4[0][0];
106 (void) p4[2][3];
107 std::shared_ptr<int[][5]> p5 = std::make_shared_for_overwrite<int[][5]>(6);
108 (void) p5[0][0];
109 (void) p5[5][4];
110
111 struct BigBoi { int x[100]; };
112 std::shared_ptr<BigBoi> p6 = std::make_shared_for_overwrite<BigBoi>();
113 (void) p6->x[0];
114 std::shared_ptr<BigBoi[22]> p7 = std::make_shared_for_overwrite<BigBoi[22]>();
115 (void) p7[0].x[0];
116 (void) p7[21].x[99];
117 std::shared_ptr<BigBoi[]> p8 = std::make_shared_for_overwrite<BigBoi[]>(11);
118 (void) p8[0].x[0];
119 (void) p8[10].x[10];
120 }
121
122 void
123 test03()
124 {
125 // Type with non-trivial initialization should still be default-initialized.
126 struct NonTriv
127 {
128 int init = 0xbb;
129 int uninit;
130 };
131 std::shared_ptr<NonTriv> a = std::make_shared_for_overwrite<NonTriv>();
132 VERIFY( a->init == 0xbb );
133 std::shared_ptr<NonTriv[]> b = std::make_shared_for_overwrite<NonTriv[2]>();
134 VERIFY( b[1].init == 0xbb );
135 std::shared_ptr<NonTriv[]> c = std::make_shared_for_overwrite<NonTriv[]>(2);
136 VERIFY( c[1].init == 0xbb );
137 }
138
139 int
140 main()
141 {
142 test01();
143 test02();
144 test03();
145 }