win32u: Move NtUserTranslateMessage implementation from user32.
[wine.git] / dlls / msvcrt / onexit.c
blobd0c61700542aa7814c83363924291052f62a4170
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 <process.h>
27 #include "msvcrt.h"
28 #include "mtdll.h"
31 /*********************************************************************
32 * _initialize_onexit_table (UCRTBASE.@)
34 int __cdecl _initialize_onexit_table(_onexit_table_t *table)
36 if (!table)
37 return -1;
39 if (table->_first == table->_end)
40 table->_last = table->_end = table->_first = NULL;
41 return 0;
45 /*********************************************************************
46 * _register_onexit_function (UCRTBASE.@)
48 int __cdecl _register_onexit_function(_onexit_table_t *table, _onexit_t func)
50 if (!table)
51 return -1;
53 _lock(_EXIT_LOCK1);
54 if (!table->_first)
56 table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
57 if (!table->_first)
59 _unlock(_EXIT_LOCK1);
60 return -1;
62 table->_last = table->_first;
63 table->_end = table->_first + 32;
66 /* grow if full */
67 if (table->_last == table->_end)
69 int len = table->_end - table->_first;
70 _PVFV *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
71 if (!tmp)
73 _unlock(_EXIT_LOCK1);
74 return -1;
76 table->_first = tmp;
77 table->_end = table->_first + 2 * len;
78 table->_last = table->_first + len;
81 *table->_last = (_PVFV)func;
82 table->_last++;
83 _unlock(_EXIT_LOCK1);
84 return 0;
88 /*********************************************************************
89 * _execute_onexit_table (UCRTBASE.@)
91 int __cdecl _execute_onexit_table(_onexit_table_t *table)
93 _PVFV *func;
94 _onexit_table_t copy;
96 if (!table)
97 return -1;
99 _lock(_EXIT_LOCK1);
100 if (!table->_first || table->_first >= table->_last)
102 _unlock(_EXIT_LOCK1);
103 return 0;
105 copy._first = table->_first;
106 copy._last = table->_last;
107 copy._end = table->_end;
108 memset(table, 0, sizeof(*table));
109 _initialize_onexit_table(table);
110 _unlock(_EXIT_LOCK1);
112 for (func = copy._last - 1; func >= copy._first; func--)
114 if (*func)
115 (*func)();
118 HeapFree(GetProcessHeap(), 0, copy._first);
119 return 0;