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