1 //===---------------------------- test_vector.cpp -------------------------===//
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
7 //===----------------------------------------------------------------------===//
16 void *my_alloc2 ( size_t sz
) {
17 void *p
= std::malloc ( sz
);
18 // std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
22 void my_dealloc2 ( void *p
) {
23 // std::printf ( "Freeing %lx\n", (unsigned long) p );
27 void my_dealloc3 ( void *p
, size_t ) {
28 // std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
32 void my_construct ( void * ) {
33 // std::printf ( "Constructing %lx\n", (unsigned long) p );
36 void my_destruct ( void * ) {
37 // std::printf ( "Destructing %lx\n", (unsigned long) p );
41 void count_construct ( void * ) { ++gCounter
; }
42 void count_destruct ( void * ) { --gCounter
; }
45 int gConstructorCounter
;
46 int gConstructorThrowTarget
;
47 int gDestructorCounter
;
48 int gDestructorThrowTarget
;
49 void throw_construct ( void * ) {
50 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
51 if ( gConstructorCounter
== gConstructorThrowTarget
)
53 ++gConstructorCounter
;
56 void throw_destruct ( void * ) {
57 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
58 if ( ++gDestructorCounter
== gDestructorThrowTarget
)
63 #if __cplusplus >= 201103L
64 # define CAN_THROW noexcept(false)
71 vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct
, throw_destruct
)) {}
72 ~vec_on_stack () CAN_THROW
{__cxxabiv1::__cxa_vec_delete ( storage
, 40, 8, throw_destruct
); }
75 // Test calls with empty constructors and destructors
77 void *one
, *two
, *three
;
79 // Try with no padding and no con/destructors
80 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 0, NULL
, NULL
);
81 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 0, NULL
, NULL
, my_alloc2
, my_dealloc2
);
82 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 0, NULL
, NULL
, my_alloc2
, my_dealloc3
);
84 __cxxabiv1::__cxa_vec_delete ( one
, 40, 0, NULL
);
85 __cxxabiv1::__cxa_vec_delete2( two
, 40, 0, NULL
, my_dealloc2
);
86 __cxxabiv1::__cxa_vec_delete3( three
, 40, 0, NULL
, my_dealloc3
);
88 // Try with no padding
89 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 0, my_construct
, my_destruct
);
90 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 0, my_construct
, my_destruct
, my_alloc2
, my_dealloc2
);
91 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 0, my_construct
, my_destruct
, my_alloc2
, my_dealloc3
);
93 __cxxabiv1::__cxa_vec_delete ( one
, 40, 0, my_destruct
);
94 __cxxabiv1::__cxa_vec_delete2( two
, 40, 0, my_destruct
, my_dealloc2
);
95 __cxxabiv1::__cxa_vec_delete3( three
, 40, 0, my_destruct
, my_dealloc3
);
97 // Padding and no con/destructors
98 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 8, NULL
, NULL
);
99 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 8, NULL
, NULL
, my_alloc2
, my_dealloc2
);
100 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 8, NULL
, NULL
, my_alloc2
, my_dealloc3
);
102 __cxxabiv1::__cxa_vec_delete ( one
, 40, 8, NULL
);
103 __cxxabiv1::__cxa_vec_delete2( two
, 40, 8, NULL
, my_dealloc2
);
104 __cxxabiv1::__cxa_vec_delete3( three
, 40, 8, NULL
, my_dealloc3
);
106 // Padding with con/destructors
107 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 8, my_construct
, my_destruct
);
108 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 8, my_construct
, my_destruct
, my_alloc2
, my_dealloc2
);
109 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 8, my_construct
, my_destruct
, my_alloc2
, my_dealloc3
);
111 __cxxabiv1::__cxa_vec_delete ( one
, 40, 8, my_destruct
);
112 __cxxabiv1::__cxa_vec_delete2( two
, 40, 8, my_destruct
, my_dealloc2
);
113 __cxxabiv1::__cxa_vec_delete3( three
, 40, 8, my_destruct
, my_dealloc3
);
118 // Make sure the constructors and destructors are matched
119 int test_counted ( ) {
121 void *one
, *two
, *three
;
123 // Try with no padding
125 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 0, count_construct
, count_destruct
);
126 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 0, count_construct
, count_destruct
, my_alloc2
, my_dealloc2
);
127 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 0, count_construct
, count_destruct
, my_alloc2
, my_dealloc3
);
129 __cxxabiv1::__cxa_vec_delete ( one
, 40, 0, count_destruct
);
130 __cxxabiv1::__cxa_vec_delete2( two
, 40, 0, count_destruct
, my_dealloc2
);
131 __cxxabiv1::__cxa_vec_delete3( three
, 40, 0, count_destruct
, my_dealloc3
);
133 // Since there was no padding, the # of elements in the array are not stored
134 // and the destructors are not called.
135 if ( gCounter
!= 30 ) {
136 std::cerr
<< "Mismatched Constructor/Destructor calls (1)" << std::endl
;
137 std::cerr
<< " Expected 30, got " << gCounter
<< std::endl
;
142 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 8, count_construct
, count_destruct
);
143 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 8, count_construct
, count_destruct
, my_alloc2
, my_dealloc2
);
144 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 8, count_construct
, count_destruct
, my_alloc2
, my_dealloc3
);
146 __cxxabiv1::__cxa_vec_delete ( one
, 40, 8, count_destruct
);
147 __cxxabiv1::__cxa_vec_delete2( two
, 40, 8, count_destruct
, my_dealloc2
);
148 __cxxabiv1::__cxa_vec_delete3( three
, 40, 8, count_destruct
, my_dealloc3
);
150 if ( gCounter
!= 0 ) {
151 std::cerr
<< "Mismatched Constructor/Destructor calls (2)" << std::endl
;
152 std::cerr
<< " Expected 0, got " << gCounter
<< std::endl
;
159 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
160 // Make sure the constructors and destructors are matched
161 int test_exception_in_constructor ( ) {
163 void *one
, *two
, *three
;
165 // Try with no padding
166 gConstructorCounter
= gDestructorCounter
= 0;
167 gConstructorThrowTarget
= 15;
168 gDestructorThrowTarget
= -1;
170 one
= two
= three
= NULL
;
171 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 0, throw_construct
, throw_destruct
);
172 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 0, throw_construct
, throw_destruct
, my_alloc2
, my_dealloc2
);
173 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 0, throw_construct
, throw_destruct
, my_alloc2
, my_dealloc3
);
177 __cxxabiv1::__cxa_vec_delete ( one
, 40, 0, throw_destruct
);
178 __cxxabiv1::__cxa_vec_delete2( two
, 40, 0, throw_destruct
, my_dealloc2
);
179 __cxxabiv1::__cxa_vec_delete3( three
, 40, 0, throw_destruct
, my_dealloc3
);
181 // Since there was no padding, the # of elements in the array are not stored
182 // and the destructors are not called.
183 // Since we threw after 15 calls to the constructor, we should see 5 calls to
184 // the destructor from the partially constructed array.
185 if ( gConstructorCounter
- gDestructorCounter
!= 10 ) {
186 std::cerr
<< "Mismatched Constructor/Destructor calls (1C)" << std::endl
;
187 std::cerr
<< gConstructorCounter
<< " constructors, but " <<
188 gDestructorCounter
<< " destructors" << std::endl
;
192 gConstructorCounter
= gDestructorCounter
= 0;
193 gConstructorThrowTarget
= 15;
194 gDestructorThrowTarget
= -1;
196 one
= two
= three
= NULL
;
197 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct
, throw_destruct
);
198 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct
, throw_destruct
, my_alloc2
, my_dealloc2
);
199 three
= __cxxabiv1::__cxa_vec_new3( 10, 40, 8, throw_construct
, throw_destruct
, my_alloc2
, my_dealloc3
);
203 __cxxabiv1::__cxa_vec_delete ( one
, 40, 8, throw_destruct
);
204 __cxxabiv1::__cxa_vec_delete2( two
, 40, 8, throw_destruct
, my_dealloc2
);
205 __cxxabiv1::__cxa_vec_delete3( three
, 40, 8, throw_destruct
, my_dealloc3
);
207 if ( gConstructorCounter
!= gDestructorCounter
) {
208 std::cerr
<< "Mismatched Constructor/Destructor calls (2C)" << std::endl
;
209 std::cerr
<< gConstructorCounter
<< " constructors, but " <<
210 gDestructorCounter
<< " destructors" << std::endl
;
218 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
219 // Make sure the constructors and destructors are matched
220 int test_exception_in_destructor ( ) {
222 void *one
, *two
, *three
;
223 one
= two
= three
= NULL
;
225 // Throw from within a destructor
226 gConstructorCounter
= gDestructorCounter
= 0;
227 gConstructorThrowTarget
= -1;
228 gDestructorThrowTarget
= 15;
231 one
= __cxxabiv1::__cxa_vec_new ( 10, 40, 8, throw_construct
, throw_destruct
);
232 two
= __cxxabiv1::__cxa_vec_new2( 10, 40, 8, throw_construct
, throw_destruct
, my_alloc2
, my_dealloc2
);
237 __cxxabiv1::__cxa_vec_delete ( one
, 40, 8, throw_destruct
);
238 __cxxabiv1::__cxa_vec_delete2( two
, 40, 8, throw_destruct
, my_dealloc2
);
243 // We should have thrown in the middle of cleaning up "two", which means that
244 // there should be 20 calls to the destructor and the try block should exit
245 // before the assertion.
246 if ( gConstructorCounter
!= 20 || gDestructorCounter
!= 20 ) {
247 std::cerr
<< "Unexpected Constructor/Destructor calls (1D)" << std::endl
;
248 std::cerr
<< "Expected (20, 20), but got (" << gConstructorCounter
<< ", " <<
249 gDestructorCounter
<< ")" << std::endl
;
253 // Try throwing from a destructor - should be fine.
254 gConstructorCounter
= gDestructorCounter
= 0;
255 gConstructorThrowTarget
= -1;
256 gDestructorThrowTarget
= 5;
257 try { vec_on_stack v
; }
260 if ( gConstructorCounter
!= gDestructorCounter
) {
261 std::cerr
<< "Mismatched Constructor/Destructor calls (2D)" << std::endl
;
262 std::cerr
<< gConstructorCounter
<< " constructors, but " <<
263 gDestructorCounter
<< " destructors" << std::endl
;
273 retVal
+= test_empty ();
274 retVal
+= test_counted ();
275 #ifndef LIBCXXABI_HAS_NO_EXCEPTIONS
276 retVal
+= test_exception_in_constructor ();
277 retVal
+= test_exception_in_destructor ();