fixed warnings when compiled with -Wmissing-declarations
[reactos.git] / reactos / lib / pseh / framebased.c
1 /*
2 Copyright (c) 2004 KJK::Hyperion
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy of
5 this software and associated documentation files (the "Software"), to deal in
6 the Software without restriction, including without limitation the rights to
7 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
8 of the Software, and to permit persons to whom the Software is furnished to do
9 so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21 */
22
23 #define STRICT
24 #define WIN32_LEAN_AND_MEAN
25 #include <windows.h>
26
27 #include <pseh/pseh.h>
28 #include <pseh/framebased/internal.h>
29 #include <pseh/excpt.h>
30 #include <pseh/framebased.h>
31
32 #include <excpt.h>
33
34 /* Assembly helpers, see i386/framebased.asm */
35 extern void __cdecl _SEHCleanHandlerEnvironment(void);
36 extern struct __SEHRegistration * __cdecl _SEHRegisterFrame(_SEHRegistration_t *);
37 extern void __cdecl _SEHUnregisterFrame(void);
38 extern void __cdecl _SEHGlobalUnwind(_SEHPortableFrame_t *);
39 extern _SEHRegistration_t * __cdecl _SEHCurrentRegistration(void);
40
41 /* Borland C++ uses a different decoration (i.e. none) for stdcall functions */
42 extern void __stdcall RtlUnwind(void *, void *, void *, void *);
43 void const * _SEHRtlUnwind = RtlUnwind;
44
45 static void __stdcall _SEHLocalUnwind
46 (
47 _SEHPortableFrame_t * frame,
48 _SEHPortableTryLevel_t * dsttrylevel
49 )
50 {
51 _SEHPortableTryLevel_t * trylevel;
52
53 for
54 (
55 trylevel = frame->SPF_TopTryLevel;
56 trylevel != dsttrylevel;
57 trylevel = trylevel->SPT_Next
58 )
59 {
60 _SEHFinally_t pfnFinally;
61
62 /* ASSERT(trylevel); */
63
64 pfnFinally = trylevel->SPT_Handlers->SH_Finally;
65
66 if(pfnFinally)
67 pfnFinally(frame);
68 }
69 }
70
71 static void __cdecl _SEHCallHandler
72 (
73 _SEHPortableFrame_t * frame,
74 _SEHPortableTryLevel_t * trylevel
75 )
76 {
77 _SEHGlobalUnwind(frame);
78 _SEHLocalUnwind(frame, trylevel);
79 frame->SPF_Handler(trylevel);
80 }
81
82 static int __cdecl _SEHFrameHandler
83 (
84 struct _EXCEPTION_RECORD * ExceptionRecord,
85 void * EstablisherFrame,
86 struct _CONTEXT * ContextRecord,
87 void * DispatcherContext
88 )
89 {
90 _SEHPortableFrame_t * frame;
91
92 _SEHCleanHandlerEnvironment();
93
94 frame = EstablisherFrame;
95
96 /* Unwinding */
97 if(ExceptionRecord->ExceptionFlags & (4 | 2))
98 _SEHLocalUnwind(frame, NULL);
99 /* Handling */
100 else
101 {
102 int ret;
103 _SEHPortableTryLevel_t * trylevel;
104
105 if(ExceptionRecord->ExceptionCode)
106 frame->SPF_Code = ExceptionRecord->ExceptionCode;
107 else
108 frame->SPF_Code = 0xC0000001;
109
110 for
111 (
112 trylevel = frame->SPF_TopTryLevel;
113 trylevel != NULL;
114 trylevel = trylevel->SPT_Next
115 )
116 {
117 _SEHFilter_t pfnFilter = trylevel->SPT_Handlers->SH_Filter;
118
119 switch((UINT_PTR)pfnFilter)
120 {
121 case (UINT_PTR)_SEH_STATIC_FILTER(_SEH_EXECUTE_HANDLER):
122 case (UINT_PTR)_SEH_STATIC_FILTER(_SEH_CONTINUE_SEARCH):
123 case (UINT_PTR)_SEH_STATIC_FILTER(_SEH_CONTINUE_EXECUTION):
124 {
125 ret = (int)((UINT_PTR)pfnFilter) - 2;
126 break;
127 }
128
129 default:
130 {
131 if(trylevel->SPT_Handlers->SH_Filter)
132 {
133 EXCEPTION_POINTERS ep;
134
135 ep.ExceptionRecord = ExceptionRecord;
136 ep.ContextRecord = ContextRecord;
137
138 ret = pfnFilter(&ep, frame);
139 }
140 else
141 ret = _SEH_CONTINUE_SEARCH;
142
143 break;
144 }
145 }
146
147 /* _SEH_CONTINUE_EXECUTION */
148 if(ret < 0)
149 return ExceptionContinueExecution;
150 /* _SEH_EXECUTE_HANDLER */
151 else if(ret > 0)
152 _SEHCallHandler(frame, trylevel);
153 /* _SEH_CONTINUE_SEARCH */
154 else
155 continue;
156 }
157
158 /* FALLTHROUGH */
159 }
160
161 return ExceptionContinueSearch;
162 }
163
164 void __stdcall _SEHEnterFrame_s
165 (
166 _SEHPortableFrame_t * frame,
167 _SEHPortableTryLevel_t * trylevel
168 )
169 {
170 _SEHEnterFrame_f(frame, trylevel);
171 }
172
173 void __stdcall _SEHEnterTry_s(_SEHPortableTryLevel_t * trylevel)
174 {
175 _SEHEnterTry_f(trylevel);
176 }
177
178 void __stdcall _SEHLeave_s(void)
179 {
180 _SEHLeave_f();
181 }
182
183 void _SEH_FASTCALL _SEHEnterFrame_f
184 (
185 _SEHPortableFrame_t * frame,
186 _SEHPortableTryLevel_t * trylevel
187 )
188 {
189 /* ASSERT(frame); */
190 /* ASSERT(trylevel); */
191 frame->SPF_Registration.SER_Handler = _SEHFrameHandler;
192 frame->SPF_Code = 0;
193 frame->SPF_TopTryLevel = trylevel;
194 trylevel->SPT_Next = NULL;
195 _SEHRegisterFrame(&frame->SPF_Registration);
196 }
197
198 void _SEH_FASTCALL _SEHEnterTry_f(_SEHPortableTryLevel_t * trylevel)
199 {
200 _SEHPortableFrame_t * frame;
201
202 frame = _SEH_CONTAINING_RECORD
203 (
204 _SEHCurrentRegistration(),
205 _SEHPortableFrame_t,
206 SPF_Registration
207 );
208
209 trylevel->SPT_Next = frame->SPF_TopTryLevel;
210 frame->SPF_TopTryLevel = trylevel;
211 }
212
213 void _SEH_FASTCALL _SEHLeave_f(void)
214 {
215 _SEHPortableFrame_t * frame;
216 _SEHPortableTryLevel_t * trylevel;
217
218 frame = _SEH_CONTAINING_RECORD
219 (
220 _SEHCurrentRegistration(),
221 _SEHPortableFrame_t,
222 SPF_Registration
223 );
224
225 /* ASSERT(frame); */
226
227 trylevel = frame->SPF_TopTryLevel;
228
229 /* ASSERT(trylevel); */
230
231 if(trylevel->SPT_Next)
232 frame->SPF_TopTryLevel = trylevel->SPT_Next;
233 else
234 _SEHUnregisterFrame();
235 }
236
237 /* EOF */