mf/session: Implement support for sinks that provide sample allocators.
[wine.git] / dlls / msvcrt / exit.c
blobcb38b35de9a7ed6b2a5686350b6e63372d10a2b0
1 /*
2 * msvcrt.dll exit functions
4 * Copyright 2000 Jon Griffiths
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
20 #include <stdio.h>
21 #include "msvcrt.h"
22 #include "mtdll.h"
23 #include "winuser.h"
24 #include "wine/debug.h"
26 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
28 /* MT */
29 #define LOCK_EXIT _mlock(_EXIT_LOCK1)
30 #define UNLOCK_EXIT _munlock(_EXIT_LOCK1)
32 static MSVCRT_purecall_handler purecall_handler = NULL;
34 static MSVCRT__onexit_table_t MSVCRT_atexit_table;
36 typedef void (__stdcall *_tls_callback_type)(void*,ULONG,void*);
37 static _tls_callback_type tls_atexit_callback;
39 static CRITICAL_SECTION MSVCRT_onexit_cs;
40 static CRITICAL_SECTION_DEBUG MSVCRT_onexit_cs_debug =
42 0, 0, &MSVCRT_onexit_cs,
43 { &MSVCRT_onexit_cs_debug.ProcessLocksList, &MSVCRT_onexit_cs_debug.ProcessLocksList },
44 0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_onexit_cs") }
46 static CRITICAL_SECTION MSVCRT_onexit_cs = { &MSVCRT_onexit_cs_debug, -1, 0, 0, 0, 0 };
48 extern int MSVCRT_app_type;
49 extern MSVCRT_wchar_t *MSVCRT__wpgmptr;
51 static unsigned int MSVCRT_abort_behavior = MSVCRT__WRITE_ABORT_MSG | MSVCRT__CALL_REPORTFAULT;
52 static int MSVCRT_error_mode = MSVCRT__OUT_TO_DEFAULT;
54 void (*CDECL _aexit_rtn)(int) = MSVCRT__exit;
56 static int initialize_onexit_table(MSVCRT__onexit_table_t *table)
58 if (!table)
59 return -1;
61 if (table->_first == table->_end)
62 table->_last = table->_end = table->_first = NULL;
63 return 0;
66 static int register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
68 if (!table)
69 return -1;
71 EnterCriticalSection(&MSVCRT_onexit_cs);
72 if (!table->_first)
74 table->_first = MSVCRT_calloc(32, sizeof(void *));
75 if (!table->_first)
77 WARN("failed to allocate initial table.\n");
78 LeaveCriticalSection(&MSVCRT_onexit_cs);
79 return -1;
81 table->_last = table->_first;
82 table->_end = table->_first + 32;
85 /* grow if full */
86 if (table->_last == table->_end)
88 int len = table->_end - table->_first;
89 MSVCRT__onexit_t *tmp = MSVCRT_realloc(table->_first, 2 * len * sizeof(void *));
90 if (!tmp)
92 WARN("failed to grow table.\n");
93 LeaveCriticalSection(&MSVCRT_onexit_cs);
94 return -1;
96 table->_first = tmp;
97 table->_end = table->_first + 2 * len;
98 table->_last = table->_first + len;
101 *table->_last = func;
102 table->_last++;
103 LeaveCriticalSection(&MSVCRT_onexit_cs);
104 return 0;
107 static int execute_onexit_table(MSVCRT__onexit_table_t *table)
109 MSVCRT__onexit_t *func;
110 MSVCRT__onexit_table_t copy;
112 if (!table)
113 return -1;
115 EnterCriticalSection(&MSVCRT_onexit_cs);
116 if (!table->_first || table->_first >= table->_last)
118 LeaveCriticalSection(&MSVCRT_onexit_cs);
119 return 0;
121 copy._first = table->_first;
122 copy._last = table->_last;
123 copy._end = table->_end;
124 memset(table, 0, sizeof(*table));
125 initialize_onexit_table(table);
126 LeaveCriticalSection(&MSVCRT_onexit_cs);
128 for (func = copy._last - 1; func >= copy._first; func--)
130 if (*func)
131 (*func)();
134 MSVCRT_free(copy._first);
135 return 0;
138 static void call_atexit(void)
140 /* Note: should only be called with the exit lock held */
141 if (tls_atexit_callback) tls_atexit_callback(NULL, DLL_PROCESS_DETACH, NULL);
142 execute_onexit_table(&MSVCRT_atexit_table);
145 /*********************************************************************
146 * __dllonexit (MSVCRT.@)
148 MSVCRT__onexit_t CDECL __dllonexit(MSVCRT__onexit_t func, MSVCRT__onexit_t **start, MSVCRT__onexit_t **end)
150 MSVCRT__onexit_t *tmp;
151 int len;
153 TRACE("(%p,%p,%p)\n", func, start, end);
155 if (!start || !*start || !end || !*end)
157 FIXME("bad table\n");
158 return NULL;
161 len = (*end - *start);
163 TRACE("table start %p-%p, %d entries\n", *start, *end, len);
165 if (++len <= 0)
166 return NULL;
168 tmp = MSVCRT_realloc(*start, len * sizeof(*tmp));
169 if (!tmp)
170 return NULL;
171 *start = tmp;
172 *end = tmp + len;
173 tmp[len - 1] = func;
174 TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
175 return func;
178 /*********************************************************************
179 * _exit (MSVCRT.@)
181 void CDECL MSVCRT__exit(int exitcode)
183 TRACE("(%d)\n", exitcode);
184 ExitProcess(exitcode);
187 /* Print out an error message with an option to debug */
188 static void DoMessageBoxW(const MSVCRT_wchar_t *lead, const MSVCRT_wchar_t *message)
190 static const MSVCRT_wchar_t message_format[] = {'%','l','s','\n','\n','P','r','o','g','r','a','m',':',' ','%','l','s','\n',
191 '%','l','s','\n','\n','P','r','e','s','s',' ','O','K',' ','t','o',' ','e','x','i','t',' ','t','h','e',' ',
192 'p','r','o','g','r','a','m',',',' ','o','r',' ','C','a','n','c','e','l',' ','t','o',' ','s','t','a','r','t',' ',
193 't','h','e',' ','W','i','n','e',' ','d','e','b','u','g','g','e','r','.','\n',0};
194 static const WCHAR title[] =
195 {'W','i','n','e',' ','C','+','+',' ','R','u','n','t','i','m','e',' ','L','i','b','r','a','r','y',0};
197 MSGBOXPARAMSW msgbox;
198 MSVCRT_wchar_t text[2048];
199 INT ret;
201 MSVCRT__snwprintf(text, ARRAY_SIZE(text), message_format, lead, MSVCRT__wpgmptr, message);
203 msgbox.cbSize = sizeof(msgbox);
204 msgbox.hwndOwner = GetActiveWindow();
205 msgbox.hInstance = 0;
206 msgbox.lpszText = text;
207 msgbox.lpszCaption = title;
208 msgbox.dwStyle = MB_OKCANCEL|MB_ICONERROR;
209 msgbox.lpszIcon = NULL;
210 msgbox.dwContextHelpId = 0;
211 msgbox.lpfnMsgBoxCallback = NULL;
212 msgbox.dwLanguageId = LANG_NEUTRAL;
214 ret = MessageBoxIndirectW(&msgbox);
215 if (ret == IDCANCEL)
216 DebugBreak();
219 static void DoMessageBox(const char *lead, const char *message)
221 MSVCRT_wchar_t leadW[1024], messageW[1024];
223 MSVCRT_mbstowcs(leadW, lead, 1024);
224 MSVCRT_mbstowcs(messageW, message, 1024);
226 DoMessageBoxW(leadW, messageW);
229 /*********************************************************************
230 * _amsg_exit (MSVCRT.@)
232 void CDECL _amsg_exit(int errnum)
234 TRACE("(%d)\n", errnum);
236 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
237 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
239 char text[32];
240 MSVCRT_sprintf(text, "Error: R60%d",errnum);
241 DoMessageBox("Runtime error!", text);
243 else
244 _cprintf("\nruntime error R60%d\n",errnum);
245 _aexit_rtn(255);
248 /*********************************************************************
249 * abort (MSVCRT.@)
251 void CDECL MSVCRT_abort(void)
253 TRACE("()\n");
255 if (MSVCRT_abort_behavior & MSVCRT__WRITE_ABORT_MSG)
257 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
258 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
260 DoMessageBox("Runtime error!", "abnormal program termination");
262 else
263 _cputs("\nabnormal program termination\n");
265 MSVCRT_raise(MSVCRT_SIGABRT);
266 /* in case raise() returns */
267 MSVCRT__exit(3);
270 #if _MSVCR_VER>=80
271 /*********************************************************************
272 * _set_abort_behavior (MSVCR80.@)
274 unsigned int CDECL MSVCRT__set_abort_behavior(unsigned int flags, unsigned int mask)
276 unsigned int old = MSVCRT_abort_behavior;
278 TRACE("%x, %x\n", flags, mask);
279 if (mask & MSVCRT__CALL_REPORTFAULT)
280 FIXME("_WRITE_CALL_REPORTFAULT unhandled\n");
282 MSVCRT_abort_behavior = (MSVCRT_abort_behavior & ~mask) | (flags & mask);
283 return old;
285 #endif
287 /*********************************************************************
288 * _wassert (MSVCRT.@)
290 void CDECL MSVCRT__wassert(const MSVCRT_wchar_t* str, const MSVCRT_wchar_t* file, unsigned int line)
292 static const MSVCRT_wchar_t assertion_failed[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d','!',0};
293 static const MSVCRT_wchar_t format_msgbox[] = {'F','i','l','e',':',' ','%','l','s','\n','L','i','n','e',':',' ','%','d',
294 '\n','\n','E','x','p','r','e','s','s','i','o','n',':',' ','\"','%','l','s','\"',0};
295 static const MSVCRT_wchar_t format_console[] = {'A','s','s','e','r','t','i','o','n',' ','f','a','i','l','e','d',':',' ',
296 '%','l','s',',',' ','f','i','l','e',' ','%','l','s',',',' ','l','i','n','e',' ','%','d','\n','\n',0};
298 TRACE("(%s,%s,%d)\n", debugstr_w(str), debugstr_w(file), line);
300 if ((MSVCRT_error_mode == MSVCRT__OUT_TO_MSGBOX) ||
301 ((MSVCRT_error_mode == MSVCRT__OUT_TO_DEFAULT) && (MSVCRT_app_type == 2)))
303 MSVCRT_wchar_t text[2048];
304 MSVCRT__snwprintf(text, sizeof(text), format_msgbox, file, line, str);
305 DoMessageBoxW(assertion_failed, text);
307 else
308 MSVCRT_fwprintf(MSVCRT_stderr, format_console, str, file, line);
310 MSVCRT_raise(MSVCRT_SIGABRT);
311 MSVCRT__exit(3);
314 /*********************************************************************
315 * _assert (MSVCRT.@)
317 void CDECL MSVCRT__assert(const char* str, const char* file, unsigned int line)
319 MSVCRT_wchar_t strW[1024], fileW[1024];
321 MSVCRT_mbstowcs(strW, str, 1024);
322 MSVCRT_mbstowcs(fileW, file, 1024);
324 MSVCRT__wassert(strW, fileW, line);
327 /*********************************************************************
328 * _c_exit (MSVCRT.@)
330 void CDECL MSVCRT__c_exit(void)
332 TRACE("(void)\n");
333 /* All cleanup is done on DLL detach; Return to caller */
336 /*********************************************************************
337 * _cexit (MSVCRT.@)
339 void CDECL MSVCRT__cexit(void)
341 TRACE("(void)\n");
342 LOCK_EXIT;
343 call_atexit();
344 UNLOCK_EXIT;
347 /*********************************************************************
348 * _onexit (MSVCRT.@)
350 MSVCRT__onexit_t CDECL MSVCRT__onexit(MSVCRT__onexit_t func)
352 TRACE("(%p)\n",func);
354 if (!func)
355 return NULL;
357 LOCK_EXIT;
358 register_onexit_function(&MSVCRT_atexit_table, func);
359 UNLOCK_EXIT;
361 return func;
364 /*********************************************************************
365 * exit (MSVCRT.@)
367 void CDECL MSVCRT_exit(int exitcode)
369 HMODULE hmscoree;
370 static const WCHAR mscoreeW[] = {'m','s','c','o','r','e','e',0};
371 void (WINAPI *pCorExitProcess)(int);
373 TRACE("(%d)\n",exitcode);
374 MSVCRT__cexit();
376 hmscoree = GetModuleHandleW(mscoreeW);
378 if (hmscoree)
380 pCorExitProcess = (void*)GetProcAddress(hmscoree, "CorExitProcess");
382 if (pCorExitProcess)
383 pCorExitProcess(exitcode);
386 ExitProcess(exitcode);
389 /*********************************************************************
390 * atexit (MSVCRT.@)
392 int CDECL MSVCRT_atexit(void (__cdecl *func)(void))
394 TRACE("(%p)\n", func);
395 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
398 #if _MSVCR_VER >= 140
399 static MSVCRT__onexit_table_t MSVCRT_quick_exit_table;
401 /*********************************************************************
402 * _crt_at_quick_exit (UCRTBASE.@)
404 int CDECL MSVCRT__crt_at_quick_exit(void (__cdecl *func)(void))
406 TRACE("(%p)\n", func);
407 return register_onexit_function(&MSVCRT_quick_exit_table, (MSVCRT__onexit_t)func);
410 /*********************************************************************
411 * quick_exit (UCRTBASE.@)
413 void CDECL MSVCRT_quick_exit(int exitcode)
415 TRACE("(%d)\n", exitcode);
417 execute_onexit_table(&MSVCRT_quick_exit_table);
418 MSVCRT__exit(exitcode);
421 /*********************************************************************
422 * _crt_atexit (UCRTBASE.@)
424 int CDECL MSVCRT__crt_atexit(void (__cdecl *func)(void))
426 TRACE("(%p)\n", func);
427 return MSVCRT__onexit((MSVCRT__onexit_t)func) == (MSVCRT__onexit_t)func ? 0 : -1;
430 /*********************************************************************
431 * _initialize_onexit_table (UCRTBASE.@)
433 int CDECL MSVCRT__initialize_onexit_table(MSVCRT__onexit_table_t *table)
435 TRACE("(%p)\n", table);
437 return initialize_onexit_table(table);
440 /*********************************************************************
441 * _register_onexit_function (UCRTBASE.@)
443 int CDECL MSVCRT__register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
445 TRACE("(%p %p)\n", table, func);
447 return register_onexit_function(table, func);
450 /*********************************************************************
451 * _execute_onexit_table (UCRTBASE.@)
453 int CDECL MSVCRT__execute_onexit_table(MSVCRT__onexit_table_t *table)
455 TRACE("(%p)\n", table);
457 return execute_onexit_table(table);
459 #endif
461 /*********************************************************************
462 * _register_thread_local_exe_atexit_callback (UCRTBASE.@)
464 void CDECL _register_thread_local_exe_atexit_callback(_tls_callback_type callback)
466 TRACE("(%p)\n", callback);
467 tls_atexit_callback = callback;
470 #if _MSVCR_VER>=71
471 /*********************************************************************
472 * _set_purecall_handler (MSVCR71.@)
474 MSVCRT_purecall_handler CDECL _set_purecall_handler(MSVCRT_purecall_handler function)
476 MSVCRT_purecall_handler ret = purecall_handler;
478 TRACE("(%p)\n", function);
479 purecall_handler = function;
480 return ret;
482 #endif
484 #if _MSVCR_VER>=80
485 /*********************************************************************
486 * _get_purecall_handler (MSVCR80.@)
488 MSVCRT_purecall_handler CDECL _get_purecall_handler(void)
490 TRACE("\n");
491 return purecall_handler;
493 #endif
495 /*********************************************************************
496 * _purecall (MSVCRT.@)
498 void CDECL _purecall(void)
500 TRACE("(void)\n");
502 if(purecall_handler)
503 purecall_handler();
504 _amsg_exit( 25 );
507 /******************************************************************************
508 * _set_error_mode (MSVCRT.@)
510 * Set the error mode, which describes where the C run-time writes error messages.
512 * PARAMS
513 * mode - the new error mode
515 * RETURNS
516 * The old error mode.
519 int CDECL _set_error_mode(int mode)
522 const int old = MSVCRT_error_mode;
523 if ( MSVCRT__REPORT_ERRMODE != mode ) {
524 MSVCRT_error_mode = mode;
526 return old;