amstream: Implement IMediaStreamFilter::ReferenceTimeToStreamTime().
[wine.git] / include / wine / exception.h
bloba2b0bb08dc32c8b0aa50a49cc24d8308dfbb8821
1 /*
2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #ifndef __WINE_WINE_EXCEPTION_H
22 #define __WINE_WINE_EXCEPTION_H
24 #include <windef.h>
25 #include <excpt.h>
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
31 /* The following definitions allow using exceptions in Wine and Winelib code
33 * They should be used like this:
35 * __TRY
36 * {
37 * do some stuff that can raise an exception
38 * }
39 * __EXCEPT(filter_func)
40 * {
41 * handle the exception here
42 * }
43 * __ENDTRY
45 * or
47 * __TRY
48 * {
49 * do some stuff that can raise an exception
50 * }
51 * __FINALLY(finally_func)
53 * The filter_func and finally_func functions must be defined like this:
55 * LONG CALLBACK filter_func( PEXCEPTION_POINTERS __eptr ) { ... }
57 * void CALLBACK finally_func( BOOL __normal ) { ... }
59 * The filter function must return one of the EXCEPTION_* code; it can
60 * use GetExceptionInformation() and GetExceptionCode() to retrieve the
61 * exception info.
63 * Warning: Inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
64 * break out of the current block, but avoid using them because they
65 * won't work when compiling with native exceptions. You cannot use
66 * 'return', 'goto', or 'longjmp' to leave a __TRY block either, as
67 * this will surely crash. You can use 'return', 'goto', or 'longjmp'
68 * to leave an __EXCEPT block though.
70 * -- AJ
73 #if !defined(__GNUC__) && !defined(__clang__)
74 #define __attribute__(x) /* nothing */
75 #endif
77 /* Define this if you want to use your compiler built-in __try/__except support.
78 * This is only useful when compiling to a native Windows binary, as the built-in
79 * compiler exceptions will most certainly not work under Winelib.
81 #ifdef USE_COMPILER_EXCEPTIONS
83 #define __TRY __try
84 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
85 #define __EXCEPT_CTX(func, ctx) __except((func)(GetExceptionInformation(), ctx))
86 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
87 #define __FINALLY_CTX(func, ctx) __finally { (func)(!AbnormalTermination(), ctx); }
88 #define __ENDTRY /*nothing*/
89 #define __EXCEPT_PAGE_FAULT __except(GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
90 #define __EXCEPT_ALL __except(EXCEPTION_EXECUTE_HANDLER)
92 #else /* USE_COMPILER_EXCEPTIONS */
94 #ifdef __i386__
95 typedef struct { int reg[16]; } __wine_jmp_buf;
96 #elif defined(__x86_64__)
97 typedef struct { DECLSPEC_ALIGN(16) struct { unsigned __int64 Part[2]; } reg[16]; } __wine_jmp_buf;
98 #elif defined(__arm__)
99 typedef struct { int reg[28]; } __wine_jmp_buf;
100 #elif defined(__aarch64__)
101 typedef struct { __int64 reg[24]; } __wine_jmp_buf;
102 #else
103 typedef struct { int reg; } __wine_jmp_buf;
104 #endif
106 extern int __cdecl __attribute__ ((__nothrow__,__returns_twice__)) __wine_setjmpex( __wine_jmp_buf *buf,
107 EXCEPTION_REGISTRATION_RECORD *frame ) DECLSPEC_HIDDEN;
108 extern void DECLSPEC_NORETURN __cdecl __wine_longjmp( __wine_jmp_buf *buf, int retval ) DECLSPEC_HIDDEN;
109 extern void DECLSPEC_NORETURN __cdecl __wine_rtl_unwind( EXCEPTION_REGISTRATION_RECORD* frame, EXCEPTION_RECORD *record,
110 void (*target)(void) ) DECLSPEC_HIDDEN;
111 extern DWORD __cdecl __wine_exception_handler( EXCEPTION_RECORD *record,
112 EXCEPTION_REGISTRATION_RECORD *frame,
113 CONTEXT *context,
114 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
115 extern DWORD __cdecl __wine_exception_ctx_handler( EXCEPTION_RECORD *record,
116 EXCEPTION_REGISTRATION_RECORD *frame,
117 CONTEXT *context,
118 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
119 extern DWORD __cdecl __wine_exception_handler_page_fault( EXCEPTION_RECORD *record,
120 EXCEPTION_REGISTRATION_RECORD *frame,
121 CONTEXT *context,
122 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
123 extern DWORD __cdecl __wine_exception_handler_all( EXCEPTION_RECORD *record,
124 EXCEPTION_REGISTRATION_RECORD *frame,
125 CONTEXT *context,
126 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
127 extern DWORD __cdecl __wine_finally_handler( EXCEPTION_RECORD *record,
128 EXCEPTION_REGISTRATION_RECORD *frame,
129 CONTEXT *context,
130 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
131 extern DWORD __cdecl __wine_finally_ctx_handler( EXCEPTION_RECORD *record,
132 EXCEPTION_REGISTRATION_RECORD *frame,
133 CONTEXT *context,
134 EXCEPTION_REGISTRATION_RECORD **pdispatcher ) DECLSPEC_HIDDEN;
136 #define __TRY \
137 do { __WINE_FRAME __f; \
138 int __first = 1; \
139 for (;;) if (!__first) \
141 do {
143 #define __EXCEPT(func) \
144 } while(0); \
145 __wine_pop_frame( &__f.frame ); \
146 break; \
147 } else { \
148 __f.frame.Handler = __wine_exception_handler; \
149 __f.u.filter = (func); \
150 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
151 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
152 do {
154 #define __EXCEPT_CTX(func, context) \
155 } while(0); \
156 __wine_pop_frame( &__f.frame ); \
157 break; \
158 } else { \
159 __f.frame.Handler = __wine_exception_ctx_handler; \
160 __f.u.filter_ctx = (func); \
161 __f.ctx = context; \
162 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
163 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
164 do {
166 #define __EXCEPT_HANDLER(handler) \
167 } while(0); \
168 __wine_pop_frame( &__f.frame ); \
169 break; \
170 } else { \
171 __f.frame.Handler = (handler); \
172 if (__wine_setjmpex( &__f.jmp, &__f.frame )) { \
173 const __WINE_FRAME * const __eptr __attribute__((unused)) = &__f; \
174 do {
176 #define __EXCEPT_PAGE_FAULT __EXCEPT_HANDLER(__wine_exception_handler_page_fault)
177 #define __EXCEPT_ALL __EXCEPT_HANDLER(__wine_exception_handler_all)
179 #define __ENDTRY \
180 } while (0); \
181 break; \
183 __wine_push_frame( &__f.frame ); \
184 __first = 0; \
186 } while (0);
188 #define __FINALLY(func) \
189 } while(0); \
190 __wine_pop_frame( &__f.frame ); \
191 (func)(1); \
192 break; \
193 } else { \
194 __f.frame.Handler = __wine_finally_handler; \
195 __f.u.finally_func = (func); \
196 __wine_push_frame( &__f.frame ); \
197 __first = 0; \
199 } while (0);
201 #define __FINALLY_CTX(func, context) \
202 } while(0); \
203 __wine_pop_frame( &__f.frame ); \
204 (func)(1, context); \
205 break; \
206 } else { \
207 __f.frame.Handler = __wine_finally_ctx_handler; \
208 __f.u.finally_func_ctx = (func); \
209 __f.ctx = context; \
210 __wine_push_frame( &__f.frame ); \
211 __first = 0; \
213 } while (0);
216 typedef LONG (CALLBACK *__WINE_FILTER)(PEXCEPTION_POINTERS);
217 typedef LONG (CALLBACK *__WINE_FILTER_CTX)(PEXCEPTION_POINTERS, void*);
218 typedef void (CALLBACK *__WINE_FINALLY)(BOOL);
219 typedef void (CALLBACK *__WINE_FINALLY_CTX)(BOOL, void*);
221 #define GetExceptionInformation() (__eptr)
222 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
223 #define AbnormalTermination() (!__normal)
225 typedef struct __tagWINE_FRAME
227 EXCEPTION_REGISTRATION_RECORD frame;
228 union
230 /* exception data */
231 __WINE_FILTER filter;
232 __WINE_FILTER_CTX filter_ctx;
233 /* finally data */
234 __WINE_FINALLY finally_func;
235 __WINE_FINALLY_CTX finally_func_ctx;
236 } u;
237 void *ctx;
238 __wine_jmp_buf jmp;
239 /* hack to make GetExceptionCode() work in handler */
240 DWORD ExceptionCode;
241 const struct __tagWINE_FRAME *ExceptionRecord;
242 } __WINE_FRAME;
244 #endif /* USE_COMPILER_EXCEPTIONS */
246 static inline EXCEPTION_REGISTRATION_RECORD *__wine_push_frame( EXCEPTION_REGISTRATION_RECORD *frame )
248 #if defined(__GNUC__) && defined(__i386__)
249 EXCEPTION_REGISTRATION_RECORD *prev;
250 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
251 "\n\tmovl %0,(%1)"
252 "\n\t.byte 0x64\n\tmovl %1,(0)"
253 : "=&r" (prev) : "r" (frame) : "memory" );
254 return prev;
255 #else
256 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
257 frame->Prev = teb->ExceptionList;
258 teb->ExceptionList = frame;
259 return frame->Prev;
260 #endif
263 static inline EXCEPTION_REGISTRATION_RECORD *__wine_pop_frame( EXCEPTION_REGISTRATION_RECORD *frame )
265 #if defined(__GNUC__) && defined(__i386__)
266 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
267 : : "r" (frame->Prev) : "memory" );
268 return frame->Prev;
270 #else
271 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
272 teb->ExceptionList = frame->Prev;
273 return frame->Prev;
274 #endif
277 static inline EXCEPTION_REGISTRATION_RECORD *__wine_get_frame(void)
279 #if defined(__GNUC__) && defined(__i386__)
280 EXCEPTION_REGISTRATION_RECORD *ret;
281 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0" : "=r" (ret) );
282 return ret;
283 #else
284 NT_TIB *teb = (NT_TIB *)NtCurrentTeb();
285 return teb->ExceptionList;
286 #endif
289 /* Exception handling flags - from OS/2 2.0 exception handling */
291 /* Win32 seems to use the same flags as ExceptionFlags in an EXCEPTION_RECORD */
292 #define EH_NONCONTINUABLE 0x01
293 #define EH_UNWINDING 0x02
294 #define EH_EXIT_UNWIND 0x04
295 #define EH_STACK_INVALID 0x08
296 #define EH_NESTED_CALL 0x10
297 #define EH_TARGET_UNWIND 0x20
298 #define EH_COLLIDED_UNWIND 0x40
300 /* Wine-specific exceptions codes */
302 #define EXCEPTION_WINE_STUB 0x80000100 /* stub entry point called */
303 #define EXCEPTION_WINE_ASSERTION 0x80000101 /* assertion failed */
305 #ifdef __cplusplus
307 #endif
309 #endif /* __WINE_WINE_EXCEPTION_H */