https://git.reactos.org/?p=reactos.git;a=commitdiff;h=2c791cdde79d8d7bc770980e98d502144be316ac

commit 2c791cdde79d8d7bc770980e98d502144be316ac
Author:     Timo Kreuzer <[email protected]>
AuthorDate: Thu Oct 22 17:11:02 2020 +0200
Commit:     Timo Kreuzer <[email protected]>
CommitDate: Sun Nov 1 09:33:14 2020 +0100

    [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.
---
 sdk/lib/crt/crt.cmake          |  2 +-
 sdk/lib/crt/startup/atonexit.c | 36 +++++++++++++++++++++---
 sdk/lib/crt/startup/crtexe.c   |  3 ++
 sdk/lib/crt/stdlib/atexit.c    | 63 ------------------------------------------
 4 files changed, 36 insertions(+), 68 deletions(-)

diff --git a/sdk/lib/crt/crt.cmake b/sdk/lib/crt/crt.cmake
index 1148d12d289..3997ea778a9 100644
--- a/sdk/lib/crt/crt.cmake
+++ b/sdk/lib/crt/crt.cmake
@@ -197,7 +197,7 @@ list(APPEND CRT_SOURCE
     startup/natstart.c
     startup/charmax.c
     #startup/merr.c
-    #startup/atonexit.c
+    startup/atonexit.c
     #startup/txtmode.c
     startup/pesect.c
     startup/tlsmcrt.c
diff --git a/sdk/lib/crt/startup/atonexit.c b/sdk/lib/crt/startup/atonexit.c
index e5fb98a2d63..1774e90f1b3 100644
--- a/sdk/lib/crt/startup/atonexit.c
+++ b/sdk/lib/crt/startup/atonexit.c
@@ -24,13 +24,31 @@
 _PVFV *__onexitbegin;
 _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);
 
+/* INTERNAL: call atexit functions */
+void __call_atexit(void)
+{
+    /* Note: should only be called with the exit lock held */
+    _PVFV *first, *last;
+
+    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 mingw_onexit(_onexit_t func);
+_onexit_t __cdecl _onexit(_onexit_t func);
 
-_onexit_t __cdecl mingw_onexit(_onexit_t func)
+_onexit_t __cdecl _onexit(_onexit_t func)
 {
   _PVFV *onexitbegin;
   _PVFV *onexitend;
@@ -39,7 +57,17 @@ _onexit_t __cdecl mingw_onexit(_onexit_t func)
   onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
 
   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);
+#endif
   _lock (_EXIT_LOCK1);
   onexitbegin = (_PVFV *) _decode_pointer (__onexitbegin);
   onexitend = (_PVFV *) _decode_pointer (__onexitend);
@@ -55,5 +83,5 @@ _onexit_t __cdecl mingw_onexit(_onexit_t func)
 int __cdecl
 atexit (_PVFV func)
 {
-  return (mingw_onexit((_onexit_t)func) == NULL) ? -1 : 0;
+  return (_onexit((_onexit_t)func) == NULL) ? -1 : 0;
 }
diff --git a/sdk/lib/crt/startup/crtexe.c b/sdk/lib/crt/startup/crtexe.c
index d66db0bf2e7..efc29191191 100644
--- a/sdk/lib/crt/startup/crtexe.c
+++ b/sdk/lib/crt/startup/crtexe.c
@@ -211,6 +211,8 @@ int __cdecl mainCRTStartup (void)
   return ret;
 }
 
+void __call_atexit();
+
 static
 __declspec(noinline)
 int __cdecl
@@ -324,6 +326,7 @@ __tmainCRTStartup (void)
 #endif
     mainret = main (argc, argv, envp);
 #endif
+    __call_atexit();
     if (!managedapp)
       exit (mainret);
 
diff --git a/sdk/lib/crt/stdlib/atexit.c b/sdk/lib/crt/stdlib/atexit.c
index 32c5f7effaf..88fa1ff0f95 100644
--- a/sdk/lib/crt/stdlib/atexit.c
+++ b/sdk/lib/crt/stdlib/atexit.c
@@ -1,26 +1,6 @@
 /* taken from wine exit.c */
 #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.@)
  */
@@ -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);
   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;
-}
-

Reply via email to