9 #include "exception_handling.h"
11 #include <acess/sys.h>
13 /*__thread*/ struct __cxa_eh_globals {
14 __cxa_exception *caughtExceptions;
15 unsigned int uncaughtExceptions;
21 } emergency_exception;
22 /*__thread*/ bool emergency_exception_used;
24 static bool TEST_AND_SET(bool& flag) {
31 extern "C" __cxa_eh_globals *__cxa_get_globals(void)
35 extern "C" __cxa_eh_globals *__cxa_get_globals_fast(void)
39 extern "C" void __cxa_call_unexpected(void *)
41 // An unexpected exception was thrown from a function that lists its possible exceptions
42 _SysDebug("__cxa_call_unexpected - TODO");
46 extern "C" void *__cxa_allocate_exception(size_t thrown_size)
48 ::_SysDebug("__cxa_allocate_exception(%i)", thrown_size);
49 __cxa_exception *ret = static_cast<__cxa_exception*>( malloc( sizeof(__cxa_exception) + thrown_size ) );
51 if( thrown_size <= sizeof(emergency_exception.buf) && TEST_AND_SET(emergency_exception_used) )
53 ret = &emergency_exception.info;
57 _SysDebug("__cxa_allocate_exception - No free space");
60 ::_SysDebug("__cxa_allocate_exception: return %p", ret+1);
64 extern "C" void __cxa_free_exception(void *thrown_exception)
66 ::_SysDebug("__cxa_free_exception(%p)", thrown_exception);
67 if(thrown_exception == &emergency_exception.buf) {
68 //assert(emergency_exception_used);
69 emergency_exception_used = false;
72 __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
77 extern "C" void __cxa_throw(void *thrown_exception, std::type_info *tinfo, void (*dest)(void*))
79 ::_SysDebug("__cxa_throw(%p,%p,%p) '%s'", thrown_exception, tinfo, dest, tinfo->name());
81 const ::std::exception* e = reinterpret_cast<const ::std::exception*>(thrown_exception);
82 ::_SysDebug("- e.what() = '%s'", e->what());
84 ::_SysDebug("- typeid(*tinfo) = %p", &typeid(*tinfo));
86 __cxa_exception *except = static_cast<__cxa_exception*>( thrown_exception ) - 1;
88 except->unexpectedHandler = 0;
89 except->terminateHandler = 0;
90 except->exceptionType = tinfo;
91 except->exceptionDestructor = dest;
92 memcpy(&except->unwindHeader.exception_class, "Ac20C++\0", 8);
93 __cxa_get_globals()->uncaughtExceptions ++;
95 _Unwind_RaiseException(thrown_exception);
97 ::_SysDebug("__cxa_throw(%p,%s) :: UNCAUGHT", thrown_exception, tinfo->name());
101 extern "C" void *__cxa_begin_catch(void *exceptionObject)
103 ::_SysDebug("__cxa_begin_catch(%p)", exceptionObject);
104 __cxa_exception *except = static_cast<__cxa_exception*>( exceptionObject ) - 1;
106 except->handlerCount ++;
108 except->nextException = __cxa_get_globals()->caughtExceptions;
109 __cxa_get_globals()->caughtExceptions = except;
111 __cxa_get_globals_fast()->uncaughtExceptions --;
116 extern "C" void __cxa_end_catch()
118 struct __cxa_exception *except = __cxa_get_globals()->caughtExceptions;
119 ::_SysDebug("__cxa_end_catch - %p", except);
120 except->handlerCount --;
121 __cxa_get_globals()->caughtExceptions = except->nextException;
123 if( except->handlerCount == 0 ) {
124 __cxa_free_exception(except+1);