[CRT] Remove duplicated atexit/onexit code

This removes the broken wine version of atexit and onexit. It keeps only dllonexit, which is implemented properly. The previous __call_atexit is moved to where the mingw onexit/atexit code is and adjusts it to work with the existing code. A call to __call_atexit is added in __tmainCRTStartup after the main function was called.
This commit is contained in:
Timo Kreuzer 2020-10-22 17:11:02 +02:00
parent d685dcee9b
commit 2c791cdde7
4 changed files with 37 additions and 69 deletions

View file

@ -197,7 +197,7 @@ list(APPEND CRT_SOURCE
startup/natstart.c startup/natstart.c
startup/charmax.c startup/charmax.c
#startup/merr.c #startup/merr.c
#startup/atonexit.c startup/atonexit.c
#startup/txtmode.c #startup/txtmode.c
startup/pesect.c startup/pesect.c
startup/tlsmcrt.c startup/tlsmcrt.c

View file

@ -24,13 +24,31 @@
_PVFV *__onexitbegin; _PVFV *__onexitbegin;
_PVFV *__onexitend; _PVFV *__onexitend;
extern _CRTIMP _onexit_t __cdecl __dllonexit (_onexit_t, _PVFV**, _PVFV**); extern _onexit_t __cdecl __dllonexit (_onexit_t, _PVFV**, _PVFV**);
extern _onexit_t (__cdecl * __MINGW_IMP_SYMBOL(_onexit)) (_onexit_t func); extern _onexit_t (__cdecl * __MINGW_IMP_SYMBOL(_onexit)) (_onexit_t func);
/* Choose a different name to prevent name conflicts. The CRT one works fine. */ /* INTERNAL: call atexit functions */
_onexit_t __cdecl mingw_onexit(_onexit_t func); void __call_atexit(void)
{
/* Note: should only be called with the exit lock held */
_PVFV *first, *last;
_onexit_t __cdecl mingw_onexit(_onexit_t func) first = (_PVFV *)_decode_pointer(__onexitbegin);
last = (_PVFV *)_decode_pointer(__onexitend);;
if (!first) return;
while (--last >= first)
if (*last)
(**last)();
free(first);
}
/* Choose a different name to prevent name conflicts. The CRT one works fine. */
_onexit_t __cdecl _onexit(_onexit_t func);
_onexit_t __cdecl _onexit(_onexit_t func)
{ {
_PVFV *onexitbegin; _PVFV *onexitbegin;
_PVFV *onexitend; _PVFV *onexitend;
@ -39,7 +57,17 @@ _onexit_t __cdecl mingw_onexit(_onexit_t func)
onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin); onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
if (onexitbegin == (_PVFV *) -1) if (onexitbegin == (_PVFV *) -1)
#ifdef __REACTOS__
{
onexitbegin = (_PVFV *)calloc(32, sizeof(_onexit_t));
if (onexitbegin == NULL)
return NULL;
__onexitbegin = _encode_pointer(onexitbegin);
__onexitend = _encode_pointer(onexitbegin + 32);
}
#else
return (* __MINGW_IMP_SYMBOL(_onexit)) (func); return (* __MINGW_IMP_SYMBOL(_onexit)) (func);
#endif
_lock (_EXIT_LOCK1); _lock (_EXIT_LOCK1);
onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin); onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
onexitend = (_PVFV *) _decode_pointer (__onexitend); onexitend = (_PVFV *) _decode_pointer (__onexitend);
@ -55,5 +83,5 @@ _onexit_t __cdecl mingw_onexit(_onexit_t func)
int __cdecl int __cdecl
atexit (_PVFV func) atexit (_PVFV func)
{ {
return (mingw_onexit((_onexit_t)func) == NULL) ? -1 : 0; return (_onexit((_onexit_t)func) == NULL) ? -1 : 0;
} }

View file

@ -211,6 +211,8 @@ int __cdecl mainCRTStartup (void)
return ret; return ret;
} }
void __call_atexit();
static static
__declspec(noinline) __declspec(noinline)
int __cdecl int __cdecl
@ -324,6 +326,7 @@ __tmainCRTStartup (void)
#endif #endif
mainret = main (argc, argv, envp); mainret = main (argc, argv, envp);
#endif #endif
__call_atexit();
if (!managedapp) if (!managedapp)
exit (mainret); exit (mainret);

View file

@ -1,26 +1,6 @@
/* taken from wine exit.c */ /* taken from wine exit.c */
#include <precomp.h> #include <precomp.h>
_onexit_t *atexit_table = NULL;
int atexit_table_size = 0;
int atexit_registered = 0; /* Points to free slot */
/* INTERNAL: call atexit functions */
void __call_atexit(void)
{
/* Note: should only be called with the exit lock held */
TRACE("%d atext functions to call\n", atexit_registered);
/* Last registered gets executed first */
while (atexit_registered > 0)
{
atexit_registered--;
TRACE("next is %p\n",atexit_table[atexit_registered]);
if (atexit_table[atexit_registered])
(*atexit_table[atexit_registered])();
TRACE("returned\n");
}
}
/********************************************************************* /*********************************************************************
* __dllonexit (MSVCRT.@) * __dllonexit (MSVCRT.@)
*/ */
@ -53,46 +33,3 @@ _onexit_t CDECL __dllonexit(_onexit_t func, _onexit_t **start, _onexit_t **end)
TRACE("new table start %p-%p, %d entries\n", *start, *end, len); TRACE("new table start %p-%p, %d entries\n", *start, *end, len);
return func; return func;
} }
/*********************************************************************
* _onexit (MSVCRT.@)
*/
_onexit_t CDECL _onexit(_onexit_t func)
{
TRACE("(%p)\n",func);
if (!func)
return NULL;
LOCK_EXIT;
if (atexit_registered > atexit_table_size - 1)
{
_onexit_t *newtable;
TRACE("expanding table\n");
newtable = calloc(atexit_table_size + 32, sizeof(_onexit_t));
if (!newtable)
{
TRACE("failed!\n");
UNLOCK_EXIT;
return NULL;
}
memcpy (newtable, atexit_table, atexit_table_size*sizeof(_onexit_t));
atexit_table_size += 32;
free (atexit_table);
atexit_table = newtable;
}
atexit_table[atexit_registered] = func;
atexit_registered++;
UNLOCK_EXIT;
return func;
}
/*********************************************************************
* atexit (MSVCRT.@)
*/
int CDECL atexit(void (*func)(void))
{
TRACE("(%p)\n", func);
return _onexit((_onexit_t)func) == (_onexit_t)func ? 0 : -1;
}