Fix build with gcc 4.1.2
[reactos.git] / reactos / include / reactos / libs / pseh / native.h
1 /*
2 Copyright (c) 2004/2005 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 #ifndef KJK_PSEH_NATIVE_H_
24 #define KJK_PSEH_NATIVE_H_
25
26 #include <excpt.h>
27 #include <pseh/excpt.h>
28
29 /*
30 Note: just define __inline to an empty symbol if your C compiler doesn't
31 support it
32 */
33 #ifdef __cplusplus
34 # ifndef __inline
35 # define __inline inline
36 # endif
37 #endif
38
39 typedef long (__stdcall * _SEHFilter_t)
40 (
41 long,
42 struct _EXCEPTION_POINTERS *,
43 void *
44 );
45
46 typedef void (__stdcall * _SEHFinally_t)
47 (
48 int,
49 void *
50 );
51
52 static __inline long _SEHCallFilter
53 (
54 _SEHFilter_t _SEHFilter,
55 long _SEHExceptionCode,
56 struct _EXCEPTION_POINTERS * _SEHExceptionPointers,
57 void * _SEHPVLocals
58 )
59 {
60 if(_SEHFilter == _SEH_STATIC_FILTER(_SEH_EXECUTE_HANDLER))
61 return _SEH_EXECUTE_HANDLER;
62 else if(_SEHFilter == _SEH_STATIC_FILTER(_SEH_CONTINUE_SEARCH))
63 return _SEH_CONTINUE_SEARCH;
64 else if(_SEHFilter == _SEH_STATIC_FILTER(_SEH_CONTINUE_EXECUTION))
65 return _SEH_CONTINUE_EXECUTION;
66 else if(_SEHFilter)
67 return _SEHFilter(_SEHExceptionCode, _SEHExceptionPointers, _SEHPVLocals);
68 else
69 return _SEH_CONTINUE_SEARCH;
70 }
71
72 static __inline void _SEHCallFinally
73 (
74 _SEHFinally_t _SEHFinally,
75 int _SEHAbnormalTermination,
76 void * _SEHPVLocals
77 )
78 {
79 if(_SEHFinally)
80 (_SEHFinally)(_SEHAbnormalTermination, _SEHPVLocals);
81 }
82
83 /* SHARED LOCALS */
84 /* Access the locals for the current frame */
85 #define _SEH_ACCESS_LOCALS(LOCALS_) \
86 _SEH_LOCALS_TYPENAME(LOCALS_) * _SEHPLocals; \
87 _SEHPLocals = _SEH_PVOID_CAST(_SEH_LOCALS_TYPENAME(LOCALS_) *, _SEHPVLocals);
88
89 /* Access local variable VAR_ */
90 #define _SEH_VAR(VAR_) _SEHPLocals->VAR_
91
92 /* FILTER FUNCTIONS */
93 /* Declares a filter function's prototype */
94 #define _SEH_FILTER(NAME_) \
95 long __stdcall NAME_ \
96 ( \
97 long _SEHExceptionCode, \
98 struct _EXCEPTION_POINTERS * _SEHExceptionPointers, \
99 void * _SEHPVLocals \
100 )
101
102 /* Declares a static filter */
103 #define _SEH_STATIC_FILTER(ACTION_) ((_SEHFilter_t)((ACTION_) + 2))
104
105 /* Declares a PSEH filter wrapping a regular filter function */
106 #define _SEH_WRAP_FILTER(WRAPPER_, NAME_) \
107 static __inline _SEH_FILTER(WRAPPER_) \
108 { \
109 return (NAME_)(_SEHExceptionPointers); \
110 }
111
112 /* FINALLY FUNCTIONS */
113 /* Declares a finally function's prototype */
114 #define _SEH_FINALLYFUNC(NAME_) \
115 void __stdcall NAME_ \
116 ( \
117 int _SEHAbnormalTermination, \
118 void * _SEHPVLocals \
119 )
120
121 /* Declares a PSEH finally function wrapping a regular function */
122 #define _SEH_WRAP_FINALLY(WRAPPER_, NAME_) \
123 _SEH_WRAP_FINALLY_ARGS(WRAPPER_, NAME_, ())
124
125 #define _SEH_WRAP_FINALLY_ARGS(WRAPPER_, NAME_, ARGS_) \
126 static __inline _SEH_FINALLYFUNC(WRAPPER_) \
127 { \
128 NAME_ ARGS_; \
129 }
130
131 #define _SEH_WRAP_FINALLY_LOCALS_ARGS(WRAPPER_, LOCALS_, NAME_, ARGS_) \
132 static __inline _SEH_FINALLYFUNC(WRAPPER_) \
133 { \
134 _SEH_ACCESS_LOCALS(LOCALS_); \
135 NAME_ ARGS_; \
136 }
137
138 /* SAFE BLOCKS */
139 #define _SEH_TRY_FINALLY(FINALLY_) \
140 _SEH_TRY_FILTER_FINALLY \
141 ( \
142 _SEH_STATIC_FILTER(_SEH_CONTINUE_SEARCH), \
143 (FINALLY_) \
144 )
145
146 #define _SEH_END_FINALLY _SEH_HANDLE _SEH_END
147
148 #define _SEH_TRY_FILTER(FILTER_) \
149 _SEH_TRY_FILTER_FINALLY((FILTER_), NULL)
150
151 #define _SEH_TRY_HANDLE_FINALLY(FINALLY_) \
152 _SEH_TRY_FILTER_FINALLY \
153 ( \
154 _SEH_STATIC_FILTER(_SEH_EXECUTE_HANDLER), \
155 (FINALLY_) \
156 )
157
158 #define _SEH_TRY \
159 _SEH_TRY_HANDLE_FINALLY(NULL)
160
161 #define _SEH_CALL_FILTER(FILTER_) \
162 _SEHCallFilter \
163 ( \
164 (FILTER_), \
165 GetExceptionCode(), \
166 GetExceptionPointers(), \
167 _SEHPVLocals \
168 )
169
170 #define _SEH_CALL_FINALLY(FINALLY_) \
171 _SEHCallFinally((FINALLY_), (AbnormalTermination() != 0), _SEHPVLocals)
172
173 #define _SEH_TRY_FILTER_FINALLY(FILTER_, FINALLY_) \
174 __try \
175 { \
176 _SEHFinally_t _SEHFinally = (FINALLY_); \
177 _SEHFilter_t _SEHFilter = (FILTER_); \
178 void * _SEHPVLocals = &_SEHLocals; \
179 (void)_SEHPVLocals; \
180 \
181 __try \
182 {
183
184 #define _SEH_HANDLE \
185 } \
186 __except(_SEH_CALL_FILTER(_SEHFilter)) \
187 { \
188 struct _EXCEPTION_POINTERS * _SEHExceptionPointers = GetExceptionPointers();\
189 long _SEHExceptionCode = GetExceptionCode(); \
190
191 #define _SEH_END \
192 } \
193 } \
194 __finally \
195 { \
196 _SEH_CALL_FINALLY(_SEHFinally); \
197 }
198
199 #define _SEH_LEAVE __leave
200
201 #define _SEH_GetExceptionCode() (_SEHExceptionCode)
202 #define _SEH_GetExceptionPointers() (_SEHExceptionPointers)
203 #define _SEH_AbnormalTermination() (_SEHAbnormalTermination)
204
205 /* New syntax */
206
207 #define _SEH2_TRY \
208 { \
209 void * _SEHPVLocals = &_SEHLocals; \
210 (void)_SEHPVLocals; \
211 \
212 __try \
213 {
214
215 #define _SEH2_EXCEPT(FILTER_) \
216 } \
217 __except(_SEH_CALL_FILTER(FILTER_)) \
218 { \
219 struct _EXCEPTION_POINTERS * _SEHExceptionPointers = GetExceptionPointers();\
220 long _SEHExceptionCode = GetExceptionCode(); \
221
222 #define _SEH2_FINALLY(FINALLY_) \
223 } \
224 __finally \
225 { \
226 _SEH_CALL_FINALLY(FINALLY_)
227
228 #define _SEH2_END \
229 } \
230 }
231
232 #define _SEH2_HANDLE _SEH2_EXCEPT(_SEH_STATIC_FILTER(_SEH_EXECUTE_HANDLER))
233
234 #define _SEH2_LEAVE _SEH_LEAVE
235
236 #define _SEH2_GetExceptionCode _SEH_GetExceptionCode
237 #define _SEH2_GetExceptionPointers _SEH_GetExceptionPointers
238 #define _SEH2_AbnormalTermination _SEH_AbnormalTermination
239
240 #endif
241
242 /* EOF */