44d029baabb2bdc717feb99c777b4220461af212
[tpg/acess2.git] / Usermode / Libraries / libc++.so_src / exception_handling.cc
1 /*
2  */
3 #include <typeinfo>
4 #include <cstdint>
5 #include <cstddef>
6 #include <cstdlib>
7 #include <cstring>
8 #include <exception>
9 #include "exception_handling.h"
10
11
12  int    uncaught_exception_count;
13 __cxa_exception *uncaught_exception_top;
14
15 /*__thread*/ struct {
16         __cxa_exception info;
17         char    buf[32];
18 } emergency_exception;
19 /*__thread*/ bool       emergency_exception_used;
20
21 static bool TEST_AND_SET(bool& flag) {
22         bool ret = flag;
23         flag = true;
24         return ret;
25 }
26
27 // === CODE ===
28 extern "C" void __cxa_call_unexpected(void *)
29 {
30         // An unexpected exception was thrown from a function that lists its possible exceptions
31         for(;;);
32 }
33
34 extern "C" void *__cxa_allocate_exception(size_t thrown_size)
35 {
36         __cxa_exception *ret = static_cast<__cxa_exception*>( malloc( sizeof(__cxa_exception) + thrown_size ) );
37         if( !ret ) {
38                 if( thrown_size <= sizeof(emergency_exception.buf) && TEST_AND_SET(emergency_exception_used) )
39                 {
40                         ret = &emergency_exception.info;
41                 }
42         }
43         if( !ret ) {
44                 ::std::terminate();
45         }
46         return ret;
47 }
48
49 extern "C" void __cxa_free_exception(void *thrown_exception)
50 {
51         if(thrown_exception == &emergency_exception.buf) {
52                 //assert(emergency_exception_used);
53                 emergency_exception_used = false;
54         }
55         else {
56                 __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
57                 free(except);
58         }
59 }
60
61 extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void (*dest)(void*))
62 {
63         __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
64         
65         except->exceptionType = tinfo;
66         except->exceptionDestructor = dest;
67         memcpy(&except->unwindHeader.exception_class, "Ac20C++\0", 8);
68         uncaught_exception_top ++;
69         
70         _Unwind_RaiseException(thrown_exception);
71         
72         ::std::terminate();
73 }
74
75 extern "C" void *__cxa_begin_catch(void *exceptionObject)
76 {
77         __cxa_exception *except = static_cast<__cxa_exception*>( exceptionObject ) - 1;
78         
79         except->handlerCount ++;
80         
81         except->nextException = uncaught_exception_top;
82         uncaught_exception_top = except;
83         
84         uncaught_exception_count --;
85         
86         return except;
87 }
88
89 extern "C" void __cxa_end_catch()
90 {
91         struct __cxa_exception  *except = uncaught_exception_top;
92         except->handlerCount --;
93         uncaught_exception_top = except->nextException;
94         
95         if( except->handlerCount == 0 ) {
96                 __cxa_free_exception(except+1);
97         }
98 }
99

UCC git Repository :: git.ucc.asn.au