Debian Codegraph @

vendor/c_exception/lib/CException.c

raw
1 #include "CException.h"
2
3 #pragma GCC diagnostic push
4 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
5 volatile CEXCEPTION_FRAME_T CExceptionFrames[CEXCEPTION_NUM_ID] = {{ 0 }};
6 #pragma GCC diagnostic pop
7
8 //------------------------------------------------------------------------------------------
9 // Throw
10 //------------------------------------------------------------------------------------------
11 void Throw(CEXCEPTION_T ExceptionID)
12 {
13 unsigned int MY_ID = CEXCEPTION_GET_ID;
14 CExceptionFrames[MY_ID].Exception = ExceptionID;
15 if (CExceptionFrames[MY_ID].pFrame)
16 {
17 longjmp(*CExceptionFrames[MY_ID].pFrame, 1);
18 }
19 CEXCEPTION_NO_CATCH_HANDLER(ExceptionID);
20 }
21
22 //------------------------------------------------------------------------------------------
23 // Explanation of what it's all for:
24 //------------------------------------------------------------------------------------------
25 /*
26 #define Try
27 { <- give us some local scope. most compilers are happy with this
28 jmp_buf *PrevFrame, NewFrame; <- prev frame points to the last try block's frame. new frame gets created on stack for this Try block
29 unsigned int MY_ID = CEXCEPTION_GET_ID; <- look up this task's id for use in frame array. always 0 if single-tasking
30 PrevFrame = CExceptionFrames[CEXCEPTION_GET_ID].pFrame; <- set pointer to point at old frame (which array is currently pointing at)
31 CExceptionFrames[MY_ID].pFrame = &NewFrame; <- set array to point at my new frame instead, now
32 CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- initialize my exception id to be NONE
33 if (setjmp(NewFrame) == 0) { <- do setjmp. it returns 1 if longjump called, otherwise 0
34 if (&PrevFrame) <- this is here to force proper scoping. it requires braces or a single line to be but after Try, otherwise won't compile. This is always true at this point.
35
36 #define Catch(e)
37 else { } <- this also forces proper scoping. Without this they could stick their own 'else' in and it would get ugly
38 CExceptionFrames[MY_ID].Exception = CEXCEPTION_NONE; <- no errors happened, so just set the exception id to NONE (in case it was corrupted)
39 }
40 else <- an exception occurred
41 { e = CExceptionFrames[MY_ID].Exception; e=e;} <- assign the caught exception id to the variable passed in.
42 CExceptionFrames[MY_ID].pFrame = PrevFrame; <- make the pointer in the array point at the previous frame again, as if NewFrame never existed.
43 } <- finish off that local scope we created to have our own variables
44 if (CExceptionFrames[CEXCEPTION_GET_ID].Exception != CEXCEPTION_NONE) <- start the actual 'catch' processing if we have an exception id saved away
45 */
46