ReactOS 0.4.16-dev-2102-g4cf8777
onexit.c
Go to the documentation of this file.
1/*
2 * msvcrt onexit functions
3 *
4 * Copyright 2016 Nikolay Sivov
5 *
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.
10 *
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.
15 *
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
19 */
20
21/* these functions are part of the import lib for compatibility with the Mingw runtime */
22#if 0
23#pragma makedep implib
24#endif
25
26#include <process.h>
27#include "msvcrt.h"
28#include "mtdll.h"
29
30
31/*********************************************************************
32 * _initialize_onexit_table (UCRTBASE.@)
33 */
35{
36 if (!table)
37 return -1;
38
39 if (table->_first == table->_end)
40 table->_last = table->_end = table->_first = NULL;
41 return 0;
42}
43
44
45/*********************************************************************
46 * _register_onexit_function (UCRTBASE.@)
47 */
49{
50 if (!table)
51 return -1;
52
54 if (!table->_first)
55 {
56 table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
57 if (!table->_first)
58 {
60 return -1;
61 }
62 table->_last = table->_first;
63 table->_end = table->_first + 32;
64 }
65
66 /* grow if full */
67 if (table->_last == table->_end)
68 {
69 int len = table->_end - table->_first;
70 _PVFV *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
71 if (!tmp)
72 {
74 return -1;
75 }
76 table->_first = tmp;
77 table->_end = table->_first + 2 * len;
78 table->_last = table->_first + len;
79 }
80
81 *table->_last = (_PVFV)func;
82 table->_last++;
84 return 0;
85}
86
87
88/*********************************************************************
89 * _execute_onexit_table (UCRTBASE.@)
90 */
92{
93 _PVFV *func;
95
96 if (!table)
97 return -1;
98
100 if (!table->_first || table->_first >= table->_last)
101 {
103 return 0;
104 }
105 copy._first = table->_first;
106 copy._last = table->_last;
107 copy._end = table->_end;
108 memset(table, 0, sizeof(*table));
111
112 for (func = copy._last - 1; func >= copy._first; func--)
113 {
114 if (*func)
115 (*func)();
116 }
117
118 HeapFree(GetProcessHeap(), 0, copy._first);
119 return 0;
120}
INT copy(TCHAR source[MAX_PATH], TCHAR dest[MAX_PATH], INT append, DWORD lpdwFlags, BOOL bTouch)
Definition: copy.c:51
#define NULL
Definition: types.h:112
#define GetProcessHeap()
Definition: compat.h:736
#define HeapAlloc
Definition: compat.h:733
#define HeapReAlloc
Definition: compat.h:734
#define HeapFree(x, y, z)
Definition: compat.h:735
#define HEAP_ZERO_MEMORY
Definition: compat.h:134
#define __cdecl
Definition: corecrt.h:121
int(__cdecl * _onexit_t)(void)
void(__cdecl * _PVFV)(void)
void CDECL _lock(int locknum)
Definition: lock.c:85
void CDECL _unlock(int locknum)
Definition: lock.c:114
#define _EXIT_LOCK1
Definition: mtdll.h:37
GLenum func
Definition: glext.h:6028
GLenum GLsizei len
Definition: glext.h:6722
int __cdecl _register_onexit_function(_onexit_table_t *table, _onexit_t func)
Definition: onexit.c:48
int __cdecl _initialize_onexit_table(_onexit_table_t *table)
Definition: onexit.c:34
int __cdecl _execute_onexit_table(_onexit_table_t *table)
Definition: onexit.c:91
#define memset(x, y, z)
Definition: compat.h:39