vbscript/tests: Add tests for the script TypeInfo's TypeComp binds.
[wine.git] / dlls / msvcrt / onexit.c
blobd1877a1c38ef6b900c9caa35e0998eee7a8268bc
1 /*
2 * msvcrt onexit functions
4 * Copyright 2016 Nikolay Sivov
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 /* these functions are part of the import lib for compatibility with the Mingw runtime */
22 #if 0
23 #pragma makedep implib
24 #endif
26 #include "msvcrt.h"
27 #include "mtdll.h"
30 /*********************************************************************
31 * _initialize_onexit_table (UCRTBASE.@)
33 int CDECL _initialize_onexit_table(MSVCRT__onexit_table_t *table)
35 if (!table)
36 return -1;
38 if (table->_first == table->_end)
39 table->_last = table->_end = table->_first = NULL;
40 return 0;
44 /*********************************************************************
45 * _register_onexit_function (UCRTBASE.@)
47 int CDECL _register_onexit_function(MSVCRT__onexit_table_t *table, MSVCRT__onexit_t func)
49 if (!table)
50 return -1;
52 _mlock(_EXIT_LOCK1);
53 if (!table->_first)
55 table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
56 if (!table->_first)
58 _munlock(_EXIT_LOCK1);
59 return -1;
61 table->_last = table->_first;
62 table->_end = table->_first + 32;
65 /* grow if full */
66 if (table->_last == table->_end)
68 int len = table->_end - table->_first;
69 MSVCRT__onexit_t *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
70 if (!tmp)
72 _munlock(_EXIT_LOCK1);
73 return -1;
75 table->_first = tmp;
76 table->_end = table->_first + 2 * len;
77 table->_last = table->_first + len;
80 *table->_last = func;
81 table->_last++;
82 _munlock(_EXIT_LOCK1);
83 return 0;
87 /*********************************************************************
88 * _execute_onexit_table (UCRTBASE.@)
90 int CDECL _execute_onexit_table(MSVCRT__onexit_table_t *table)
92 MSVCRT__onexit_t *func;
93 MSVCRT__onexit_table_t copy;
95 if (!table)
96 return -1;
98 _mlock(_EXIT_LOCK1);
99 if (!table->_first || table->_first >= table->_last)
101 _munlock(_EXIT_LOCK1);
102 return 0;
104 copy._first = table->_first;
105 copy._last = table->_last;
106 copy._end = table->_end;
107 memset(table, 0, sizeof(*table));
108 _initialize_onexit_table(table);
109 _munlock(_EXIT_LOCK1);
111 for (func = copy._last - 1; func >= copy._first; func--)
113 if (*func)
114 (*func)();
117 HeapFree(GetProcessHeap(), 0, copy._first);
118 return 0;