2019-03-26 20:21:14 +01:00
|
|
|
/*
|
|
|
|
* msvcrt onexit functions
|
|
|
|
*
|
|
|
|
* Copyright 2016 Nikolay Sivov
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* these functions are part of the import lib for compatibility with the Mingw runtime */
|
|
|
|
#if 0
|
|
|
|
#pragma makedep implib
|
|
|
|
#endif
|
|
|
|
|
2020-03-17 15:01:54 +01:00
|
|
|
#include <process.h>
|
2019-03-26 20:21:14 +01:00
|
|
|
#include "msvcrt.h"
|
|
|
|
#include "mtdll.h"
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _initialize_onexit_table (UCRTBASE.@)
|
|
|
|
*/
|
2020-03-17 15:01:54 +01:00
|
|
|
int __cdecl _initialize_onexit_table(_onexit_table_t *table)
|
2019-03-26 20:21:14 +01:00
|
|
|
{
|
|
|
|
if (!table)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (table->_first == table->_end)
|
|
|
|
table->_last = table->_end = table->_first = NULL;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _register_onexit_function (UCRTBASE.@)
|
|
|
|
*/
|
2020-03-17 15:01:54 +01:00
|
|
|
int __cdecl _register_onexit_function(_onexit_table_t *table, _onexit_t func)
|
2019-03-26 20:21:14 +01:00
|
|
|
{
|
|
|
|
if (!table)
|
|
|
|
return -1;
|
|
|
|
|
2020-11-19 11:33:51 +01:00
|
|
|
_lock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
if (!table->_first)
|
|
|
|
{
|
|
|
|
table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
|
|
|
|
if (!table->_first)
|
|
|
|
{
|
2020-11-19 11:33:51 +01:00
|
|
|
_unlock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
table->_last = table->_first;
|
|
|
|
table->_end = table->_first + 32;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* grow if full */
|
|
|
|
if (table->_last == table->_end)
|
|
|
|
{
|
|
|
|
int len = table->_end - table->_first;
|
2020-03-17 15:01:54 +01:00
|
|
|
_PVFV *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
|
2019-03-26 20:21:14 +01:00
|
|
|
if (!tmp)
|
|
|
|
{
|
2020-11-19 11:33:51 +01:00
|
|
|
_unlock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
table->_first = tmp;
|
|
|
|
table->_end = table->_first + 2 * len;
|
|
|
|
table->_last = table->_first + len;
|
|
|
|
}
|
|
|
|
|
2020-03-17 15:01:54 +01:00
|
|
|
*table->_last = (_PVFV)func;
|
2019-03-26 20:21:14 +01:00
|
|
|
table->_last++;
|
2020-11-19 11:33:51 +01:00
|
|
|
_unlock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*********************************************************************
|
|
|
|
* _execute_onexit_table (UCRTBASE.@)
|
|
|
|
*/
|
2020-03-17 15:01:54 +01:00
|
|
|
int __cdecl _execute_onexit_table(_onexit_table_t *table)
|
2019-03-26 20:21:14 +01:00
|
|
|
{
|
2020-03-17 15:01:54 +01:00
|
|
|
_PVFV *func;
|
|
|
|
_onexit_table_t copy;
|
2019-03-26 20:21:14 +01:00
|
|
|
|
|
|
|
if (!table)
|
|
|
|
return -1;
|
|
|
|
|
2020-11-19 11:33:51 +01:00
|
|
|
_lock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
if (!table->_first || table->_first >= table->_last)
|
|
|
|
{
|
2020-11-19 11:33:51 +01:00
|
|
|
_unlock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
copy._first = table->_first;
|
|
|
|
copy._last = table->_last;
|
|
|
|
copy._end = table->_end;
|
|
|
|
memset(table, 0, sizeof(*table));
|
|
|
|
_initialize_onexit_table(table);
|
2020-11-19 11:33:51 +01:00
|
|
|
_unlock(_EXIT_LOCK1);
|
2019-03-26 20:21:14 +01:00
|
|
|
|
|
|
|
for (func = copy._last - 1; func >= copy._first; func--)
|
|
|
|
{
|
|
|
|
if (*func)
|
|
|
|
(*func)();
|
|
|
|
}
|
|
|
|
|
|
|
|
HeapFree(GetProcessHeap(), 0, copy._first);
|
|
|
|
return 0;
|
|
|
|
}
|