Fixed finally handler name.
[wine/multimedia.git] / include / wine / exception.h
blob712d3dc0a0f1502c8d4702592a8b702ad8d75ff3
1 /*
2 * Wine exception handling
4 * Copyright (c) 1999 Alexandre Julliard
5 */
7 #ifndef __WINE_WINE_EXCEPTION_H
8 #define __WINE_WINE_EXCEPTION_H
10 #include <setjmp.h>
11 #include "winnt.h"
12 #include "thread.h"
14 /* The following definitions allow using exceptions in Wine and Winelib code
16 * They should be used like this:
18 * __TRY
19 * {
20 * do some stuff that can raise an exception
21 * }
22 * __EXCEPT(filter_func,param)
23 * {
24 * handle the exception here
25 * }
26 * __ENDTRY
28 * or
30 * __TRY
31 * {
32 * do some stuff that can raise an exception
33 * }
34 * __FINALLY(finally_func,param)
36 * The filter_func must be defined with the WINE_EXCEPTION_FILTER
37 * macro, and return one of the EXCEPTION_* code; it can use
38 * GetExceptionInformation and GetExceptionCode to retrieve the
39 * exception info.
41 * The finally_func must be defined with the WINE_FINALLY_FUNC macro.
43 * Warning: inside a __TRY or __EXCEPT block, 'break' or 'continue' statements
44 * break out of the current block. You cannot use 'return', 'goto'
45 * or 'longjmp' to leave a __TRY block, as this will surely crash.
46 * You can use them to leave a __EXCEPT block though.
48 * -- AJ
51 /* Define this if you want to use your compiler built-in __try/__except support.
52 * This is only useful when compiling to a native Windows binary, as the built-in
53 * compiler exceptions will most certainly not work under Winelib.
55 #ifdef USE_COMPILER_EXCEPTIONS
57 #define __TRY __try
58 #define __EXCEPT(func) __except((func)(GetExceptionInformation()))
59 #define __FINALLY(func) __finally { (func)(!AbnormalTermination()); }
60 #define __ENDTRY /*nothing*/
62 #else /* USE_COMPILER_EXCEPTIONS */
64 #define __TRY \
65 do { __WINE_FRAME __f; \
66 int __first = 1; \
67 for (;;) if (!__first) \
68 { \
69 do {
71 #define __EXCEPT(func) \
72 } while(0); \
73 __wine_pop_frame( &__f.frame ); \
74 break; \
75 } else { \
76 __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_exception_handler; \
77 __f.u.filter = (func); \
78 __wine_push_frame( &__f.frame ); \
79 if (setjmp( __f.jmp)) { \
80 const __WINE_FRAME * const __eptr WINE_UNUSED = &__f; \
81 do {
83 #define __ENDTRY \
84 } while (0); \
85 break; \
86 } \
87 __first = 0; \
88 } \
89 } while (0);
91 #define __FINALLY(func) \
92 } while(0); \
93 __wine_pop_frame( &__f.frame ); \
94 (func)(1); \
95 break; \
96 } else { \
97 __f.frame.Handler = (PEXCEPTION_HANDLER)__wine_finally_handler; \
98 __f.u.finally_func = (func); \
99 __wine_push_frame( &__f.frame ); \
100 __first = 0; \
102 } while (0);
105 typedef DWORD CALLBACK (*__WINE_FILTER)(PEXCEPTION_POINTERS);
106 typedef void CALLBACK (*__WINE_FINALLY)(BOOL);
108 #define WINE_EXCEPTION_FILTER(func) DWORD WINAPI func( EXCEPTION_POINTERS *__eptr )
109 #define WINE_FINALLY_FUNC(func) void WINAPI func( BOOL __normal )
111 #define GetExceptionInformation() (__eptr)
112 #define GetExceptionCode() (__eptr->ExceptionRecord->ExceptionCode)
113 #define AbnormalTermination() (!__normal)
115 typedef struct __tagWINE_FRAME
117 EXCEPTION_FRAME frame;
118 union
120 /* exception data */
121 __WINE_FILTER filter;
122 /* finally data */
123 __WINE_FINALLY finally_func;
124 } u;
125 jmp_buf jmp;
126 /* hack to make GetExceptionCode() work in handler */
127 DWORD ExceptionCode;
128 const struct __tagWINE_FRAME *ExceptionRecord;
129 } __WINE_FRAME;
131 extern DWORD __wine_exception_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
132 CONTEXT *context, LPVOID pdispatcher );
133 extern DWORD __wine_finally_handler( PEXCEPTION_RECORD record, EXCEPTION_FRAME *frame,
134 CONTEXT *context, LPVOID pdispatcher );
136 #endif /* USE_COMPILER_EXCEPTIONS */
138 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_push_frame( EXCEPTION_FRAME *frame )
140 #if defined(__GNUC__) && defined(__i386__)
141 EXCEPTION_FRAME *prev;
142 __asm__ __volatile__(".byte 0x64\n\tmovl (0),%0"
143 "\n\tmovl %0,(%1)"
144 "\n\t.byte 0x64\n\tmovl %1,(0)"
145 : "=&r" (prev) : "r" (frame) : "memory" );
146 return prev;
147 #else
148 TEB *teb = NtCurrentTeb();
149 frame->Prev = teb->except;
150 teb->except = frame;
151 return frame->Prev;
152 #endif
155 static inline EXCEPTION_FRAME * WINE_UNUSED __wine_pop_frame( EXCEPTION_FRAME *frame )
157 #if defined(__GNUC__) && defined(__i386__)
158 __asm__ __volatile__(".byte 0x64\n\tmovl %0,(0)"
159 : : "r" (frame->Prev) : "memory" );
160 return frame->Prev;
162 #else
163 NtCurrentTeb()->except = frame->Prev;
164 return frame->Prev;
165 #endif
168 #ifdef __WINE__
169 extern void WINAPI EXC_RtlRaiseException( PEXCEPTION_RECORD, PCONTEXT );
170 extern BOOL SIGNAL_Init(void);
171 #endif
173 #endif /* __WINE_WINE_EXCEPTION_H */