1 | /*␊ |
2 | * contains some ideas from the Mark VanderVoord's CException project␊ |
3 | *␊ |
4 | * Copyright (c) 2012-2013 Cadet-Petit Armel␊ |
5 | * All rights reserved.␊ |
6 | *␊ |
7 | * Redistribution and use in source and binary forms, with or without␊ |
8 | * modification, are permitted provided that the following conditions are␊ |
9 | * met:␊ |
10 | *␊ |
11 | * * Redistributions of source code must retain the above copyright␊ |
12 | * notice, this list of conditions and the following disclaimer.␊ |
13 | *␊ |
14 | * * Redistributions in binary form must reproduce the above copyright␊ |
15 | * notice, this list of conditions and the following disclaimer in the␊ |
16 | * documentation and/or other materials provided with the␊ |
17 | * distribution.␊ |
18 | *␊ |
19 | * * Neither the name of Cadet-Petit Armel nor the names of the␊ |
20 | * contributors may be used to endorse or promote products derived␊ |
21 | * from this software without specific prior written permission.␊ |
22 | *␊ |
23 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS␊ |
24 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT␊ |
25 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR␊ |
26 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT␊ |
27 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,␊ |
28 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT␊ |
29 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,␊ |
30 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY␊ |
31 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT␊ |
32 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE␊ |
33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.␊ |
34 | */␊ |
35 | #include "CException.h"␊ |
36 | #include "libsaio.h"␊ |
37 | #include "modules.h"␊ |
38 | #include "assert.h"␊ |
39 | ␊ |
40 | static CEXCEPTION_FRAME_T *CExceptionFrames = NULL;␊ |
41 | static CEXCEPTION_FRAME_T *CExceptionMainFrames = NULL;␊ |
42 | ␊ |
43 | #ifdef DEBUG_EXCEPTION␊ |
44 | static unsigned long frameCount = 0;␊ |
45 | #endif␉␊ |
46 | ␊ |
47 | //------------------------------------------------------------------------------------------␊ |
48 | // Install or Restore Default handler␊ |
49 | //------------------------------------------------------------------------------------------␊ |
50 | int Install_Default_Handler(void)␊ |
51 | {␊ |
52 | #define XTRY \␊ |
53 | {␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
54 | pushFrame(); \␊ |
55 | if ((e = setjmp(CExceptionFrames->cx)) == 0) { \␊ |
56 | e = CEXCEPTION_NONE; \␊ |
57 | if (1)␊ |
58 | ␉␊ |
59 | #define XCATCH(e) \␊ |
60 | else { } \␊ |
61 | } \␊ |
62 | }␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉␉\␊ |
63 | if (e != CEXCEPTION_NONE)␊ |
64 | ␊ |
65 | CEXCEPTION_T e = CEXCEPTION_NONE;␊ |
66 | ␉␊ |
67 | ␉XTRY␊ |
68 | ␉{␊ |
69 | ␉␉CExceptionMainFrames = CExceptionFrames;␊ |
70 | ␉}␊ |
71 | ␉XCATCH(e)␊ |
72 | ␉{␊ |
73 | if (e != EXIT_SUCCESS) {␊ |
74 | popFrame();␊ |
75 | halt();␊ |
76 | }␊ |
77 | }␊ |
78 | return 1;␊ |
79 | }␊ |
80 | ␊ |
81 | //------------------------------------------------------------------------------------------␊ |
82 | // Throw␊ |
83 | //------------------------------------------------------------------------------------------␊ |
84 | void Throw(CEXCEPTION_T ExceptionID)␊ |
85 | { ␊ |
86 | ␉longjmp(CExceptionFrames->cx, ExceptionID);␊ |
87 | ␊ |
88 | }␊ |
89 | ␊ |
90 | void pushFrame(void)␊ |
91 | {␊ |
92 | ␉CEXCEPTION_FRAME_T* frame = (CEXCEPTION_FRAME_T*)malloc(sizeof(CEXCEPTION_FRAME_T)); ␊ |
93 | ␉assert( frame != NULL );␊ |
94 | ␉bzero(frame,sizeof(CEXCEPTION_FRAME_T));␉␊ |
95 | ␉frame->pFrame = CExceptionFrames;␊ |
96 | ␉CExceptionFrames = frame;␊ |
97 | #ifdef DEBUG_EXCEPTION␊ |
98 | ␉frameCount++;␊ |
99 | #endif␉␊ |
100 | }␊ |
101 | ␊ |
102 | void popFrame(void)␊ |
103 | {␊ |
104 | ␊ |
105 | ␉CEXCEPTION_FRAME_T* frame = NULL;␊ |
106 | ␉assert( CExceptionFrames != NULL );␊ |
107 | ␉frame = CExceptionFrames->pFrame;␊ |
108 | ␉if (CExceptionFrames->zone) execute_hook("destroy_zone", CExceptionFrames->zone, NULL, NULL, NULL, NULL, NULL);␊ |
109 | ␉free(CExceptionFrames);␊ |
110 | ␉CExceptionFrames = frame;␊ |
111 | #ifdef DEBUG_EXCEPTION␊ |
112 | if (frameCount) frameCount--;␊ |
113 | #endif ␊ |
114 | }␊ |
115 | ␊ |
116 | #ifdef DEBUG_EXCEPTION␊ |
117 | unsigned long getFramesCount(void)␊ |
118 | { ␊ |
119 | ␉return frameCount;␊ |
120 | }␊ |
121 | #endif␊ |
122 | ␊ |
123 | CEXCEPTION_FRAME_T * getcurrentFrame(void)␊ |
124 | { ␊ |
125 | ␉return CExceptionFrames;␊ |
126 | }␊ |
127 | ␊ |
128 | CEXCEPTION_FRAME_T * getMainFrames(void)␊ |
129 | { ␊ |
130 | ␉return CExceptionMainFrames;␊ |
131 | } |