xref: /aosp_15_r20/external/libcxxabi/test/test_vector2.pass.cpp (revision c05d8e5dc3e10f6ce4317e8bc22cc4a25f55fa94)
1*c05d8e5dSAndroid Build Coastguard Worker //===--------------------------- test_vector2.cpp -------------------------===//
2*c05d8e5dSAndroid Build Coastguard Worker //
3*c05d8e5dSAndroid Build Coastguard Worker //                     The LLVM Compiler Infrastructure
4*c05d8e5dSAndroid Build Coastguard Worker //
5*c05d8e5dSAndroid Build Coastguard Worker // This file is dual licensed under the MIT and the University of Illinois Open
6*c05d8e5dSAndroid Build Coastguard Worker // Source Licenses. See LICENSE.TXT for details.
7*c05d8e5dSAndroid Build Coastguard Worker //
8*c05d8e5dSAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*c05d8e5dSAndroid Build Coastguard Worker 
10*c05d8e5dSAndroid Build Coastguard Worker // UNSUPPORTED: libcxxabi-no-exceptions
11*c05d8e5dSAndroid Build Coastguard Worker 
12*c05d8e5dSAndroid Build Coastguard Worker #include "cxxabi.h"
13*c05d8e5dSAndroid Build Coastguard Worker 
14*c05d8e5dSAndroid Build Coastguard Worker #include <iostream>
15*c05d8e5dSAndroid Build Coastguard Worker #include <cstdlib>
16*c05d8e5dSAndroid Build Coastguard Worker 
my_terminate()17*c05d8e5dSAndroid Build Coastguard Worker void my_terminate () { exit ( 0 ); }
18*c05d8e5dSAndroid Build Coastguard Worker 
19*c05d8e5dSAndroid Build Coastguard Worker //  Wrapper routines
my_alloc2(size_t sz)20*c05d8e5dSAndroid Build Coastguard Worker void *my_alloc2 ( size_t sz ) {
21*c05d8e5dSAndroid Build Coastguard Worker     void *p = std::malloc ( sz );
22*c05d8e5dSAndroid Build Coastguard Worker //  std::printf ( "Allocated %ld bytes at %lx\n", sz, (unsigned long) p );
23*c05d8e5dSAndroid Build Coastguard Worker     return p;
24*c05d8e5dSAndroid Build Coastguard Worker     }
25*c05d8e5dSAndroid Build Coastguard Worker 
my_dealloc2(void * p)26*c05d8e5dSAndroid Build Coastguard Worker void my_dealloc2 ( void *p ) {
27*c05d8e5dSAndroid Build Coastguard Worker //  std::printf ( "Freeing %lx\n", (unsigned long) p );
28*c05d8e5dSAndroid Build Coastguard Worker     std::free ( p );
29*c05d8e5dSAndroid Build Coastguard Worker     }
30*c05d8e5dSAndroid Build Coastguard Worker 
my_dealloc3(void * p,size_t)31*c05d8e5dSAndroid Build Coastguard Worker void my_dealloc3 ( void *p, size_t ) {
32*c05d8e5dSAndroid Build Coastguard Worker //  std::printf ( "Freeing %lx (size %ld)\n", (unsigned long) p, sz );
33*c05d8e5dSAndroid Build Coastguard Worker     std::free ( p );
34*c05d8e5dSAndroid Build Coastguard Worker     }
35*c05d8e5dSAndroid Build Coastguard Worker 
my_construct(void *)36*c05d8e5dSAndroid Build Coastguard Worker void my_construct ( void *) {
37*c05d8e5dSAndroid Build Coastguard Worker //  std::printf ( "Constructing %lx\n", (unsigned long) p );
38*c05d8e5dSAndroid Build Coastguard Worker     }
39*c05d8e5dSAndroid Build Coastguard Worker 
my_destruct(void *)40*c05d8e5dSAndroid Build Coastguard Worker void my_destruct  ( void *) {
41*c05d8e5dSAndroid Build Coastguard Worker //  std::printf ( "Destructing  %lx\n", (unsigned long) p );
42*c05d8e5dSAndroid Build Coastguard Worker     }
43*c05d8e5dSAndroid Build Coastguard Worker 
44*c05d8e5dSAndroid Build Coastguard Worker int gCounter;
count_construct(void *)45*c05d8e5dSAndroid Build Coastguard Worker void count_construct ( void * ) { ++gCounter; }
count_destruct(void *)46*c05d8e5dSAndroid Build Coastguard Worker void count_destruct  ( void * ) { --gCounter; }
47*c05d8e5dSAndroid Build Coastguard Worker 
48*c05d8e5dSAndroid Build Coastguard Worker 
49*c05d8e5dSAndroid Build Coastguard Worker int gConstructorCounter;
50*c05d8e5dSAndroid Build Coastguard Worker int gConstructorThrowTarget;
51*c05d8e5dSAndroid Build Coastguard Worker int gDestructorCounter;
52*c05d8e5dSAndroid Build Coastguard Worker int gDestructorThrowTarget;
throw_construct(void *)53*c05d8e5dSAndroid Build Coastguard Worker void throw_construct ( void * ) { if ( gConstructorCounter   == gConstructorThrowTarget ) throw 1; ++gConstructorCounter; }
throw_destruct(void *)54*c05d8e5dSAndroid Build Coastguard Worker void throw_destruct  ( void * ) { if ( ++gDestructorCounter  == gDestructorThrowTarget  ) throw 2; }
55*c05d8e5dSAndroid Build Coastguard Worker 
56*c05d8e5dSAndroid Build Coastguard Worker struct vec_on_stack {
57*c05d8e5dSAndroid Build Coastguard Worker     void *storage;
vec_on_stackvec_on_stack58*c05d8e5dSAndroid Build Coastguard Worker     vec_on_stack () : storage ( __cxxabiv1::__cxa_vec_new    (            10, 40, 8, throw_construct, throw_destruct )) {}
~vec_on_stackvec_on_stack59*c05d8e5dSAndroid Build Coastguard Worker     ~vec_on_stack () {          __cxxabiv1::__cxa_vec_delete ( storage,       40, 8,                  throw_destruct );  }
60*c05d8e5dSAndroid Build Coastguard Worker     };
61*c05d8e5dSAndroid Build Coastguard Worker 
62*c05d8e5dSAndroid Build Coastguard Worker 
63*c05d8e5dSAndroid Build Coastguard Worker //  Make sure the constructors and destructors are matched
test_exception_in_destructor()64*c05d8e5dSAndroid Build Coastguard Worker void test_exception_in_destructor ( ) {
65*c05d8e5dSAndroid Build Coastguard Worker 
66*c05d8e5dSAndroid Build Coastguard Worker //  Try throwing from a destructor while unwinding the stack -- should abort
67*c05d8e5dSAndroid Build Coastguard Worker     gConstructorCounter = gDestructorCounter = 0;
68*c05d8e5dSAndroid Build Coastguard Worker     gConstructorThrowTarget = -1;
69*c05d8e5dSAndroid Build Coastguard Worker     gDestructorThrowTarget  = 5;
70*c05d8e5dSAndroid Build Coastguard Worker     try {
71*c05d8e5dSAndroid Build Coastguard Worker         vec_on_stack v;
72*c05d8e5dSAndroid Build Coastguard Worker         throw 3;
73*c05d8e5dSAndroid Build Coastguard Worker         }
74*c05d8e5dSAndroid Build Coastguard Worker     catch ( int i ) {}
75*c05d8e5dSAndroid Build Coastguard Worker 
76*c05d8e5dSAndroid Build Coastguard Worker     std::cerr << "should never get here" << std::endl;
77*c05d8e5dSAndroid Build Coastguard Worker     }
78*c05d8e5dSAndroid Build Coastguard Worker 
79*c05d8e5dSAndroid Build Coastguard Worker 
80*c05d8e5dSAndroid Build Coastguard Worker 
main()81*c05d8e5dSAndroid Build Coastguard Worker int main () {
82*c05d8e5dSAndroid Build Coastguard Worker     std::set_terminate ( my_terminate );
83*c05d8e5dSAndroid Build Coastguard Worker     test_exception_in_destructor ();
84*c05d8e5dSAndroid Build Coastguard Worker     return 1;       // we failed if we get here
85*c05d8e5dSAndroid Build Coastguard Worker     }
86