wine/dlls/msvcrt/onexit.c
Jacek Caban 6735dcc376 msvcrt: Use public onexit table declarations in importlib.
Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
2020-03-17 15:59:16 +01:00

121 lines
3.1 KiB
C

/*
* 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
#define _CRTIMP
#include <process.h>
#include "msvcrt.h"
#include "mtdll.h"
/*********************************************************************
* _initialize_onexit_table (UCRTBASE.@)
*/
int __cdecl _initialize_onexit_table(_onexit_table_t *table)
{
if (!table)
return -1;
if (table->_first == table->_end)
table->_last = table->_end = table->_first = NULL;
return 0;
}
/*********************************************************************
* _register_onexit_function (UCRTBASE.@)
*/
int __cdecl _register_onexit_function(_onexit_table_t *table, _onexit_t func)
{
if (!table)
return -1;
_mlock(_EXIT_LOCK1);
if (!table->_first)
{
table->_first = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, 32 * sizeof(void *));
if (!table->_first)
{
_munlock(_EXIT_LOCK1);
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;
_PVFV *tmp = HeapReAlloc(GetProcessHeap(), 0, table->_first, 2 * len * sizeof(void *));
if (!tmp)
{
_munlock(_EXIT_LOCK1);
return -1;
}
table->_first = tmp;
table->_end = table->_first + 2 * len;
table->_last = table->_first + len;
}
*table->_last = (_PVFV)func;
table->_last++;
_munlock(_EXIT_LOCK1);
return 0;
}
/*********************************************************************
* _execute_onexit_table (UCRTBASE.@)
*/
int __cdecl _execute_onexit_table(_onexit_table_t *table)
{
_PVFV *func;
_onexit_table_t copy;
if (!table)
return -1;
_mlock(_EXIT_LOCK1);
if (!table->_first || table->_first >= table->_last)
{
_munlock(_EXIT_LOCK1);
return 0;
}
copy._first = table->_first;
copy._last = table->_last;
copy._end = table->_end;
memset(table, 0, sizeof(*table));
_initialize_onexit_table(table);
_munlock(_EXIT_LOCK1);
for (func = copy._last - 1; func >= copy._first; func--)
{
if (*func)
(*func)();
}
HeapFree(GetProcessHeap(), 0, copy._first);
return 0;
}