VirtualBox

Changeset 95818 in vbox


Ignore:
Timestamp:
Jul 25, 2022 2:48:00 PM (2 years ago)
Author:
vboxsync
Message:

IPRT: More IPRT_NO_CRT work on windows. bugref:10261

Location:
trunk
Files:
3 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/nocrt/stdlib.h

    r95800 r95818  
    3535RT_C_DECLS_BEGIN
    3636
     37typedef void FNRTNOCRTATEXITCALLBACK(void) /*RT_NOEXCEPT*/;
     38typedef FNRTNOCRTATEXITCALLBACK *PFNRTNOCRTATEXITCALLBACK;
     39#if defined(_MSC_VER) && defined(RT_WITHOUT_NOCRT_WRAPPERS) /* Clashes with compiler internal prototype or smth. */
     40int          rtnocrt_atexit(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
     41# define     atexit rtnocrt_atexit
     42#else
     43int          RT_NOCRT(atexit)(PFNRTNOCRTATEXITCALLBACK) RT_NOEXCEPT;
     44#endif
     45
     46#if !defined(RT_WITHOUT_NOCRT_WRAPPERS) && !defined(RT_WITHOUT_NOCRT_WRAPPER_ALIASES)
     47# define atexit     RT_NOCRT(atexit)
     48#endif
     49
     50
    3751#ifdef IPRT_NO_CRT_FOR_3RD_PARTY
     52/*
     53 * Only for external libraries and such.
     54 */
    3855
    3956DECLINLINE(void *) RT_NOCRT(malloc)(size_t cb)
     
    6683#endif /* IPRT_NO_CRT_FOR_3RD_PARTY */
    6784
     85
    6886RT_C_DECLS_END
    6987
  • trunk/src/VBox/Runtime/Makefile.kmk

    r95815 r95818  
    22742274        r3/win/nocrt-RTLogWriteStdOut-win.cpp
    22752275 RuntimeGuestR3_SOURCES.x86 += \
     2276        r3/win/nocrt-atexit.asm \
     2277        r3/win/nocrt-mainCRTStartup.asm \
     2278        r3/win/nocrt-stack.asm \
    22762279        common/string/memcpy.asm \
    22772280        common/string/memchr.asm \
     
    22852288        common/string/strcpy.asm
    22862289 RuntimeGuestR3_SOURCES.amd64 += \
     2290        r3/win/nocrt-atexit.asm \
     2291        r3/win/nocrt-mainCRTStartup.asm \
     2292        r3/win/nocrt-stack.asm \
    22872293        common/string/memcpy.asm \
    22882294        common/string/memchr.asm \
  • trunk/src/VBox/Runtime/r3/alloc-ef.cpp

    r93115 r95818  
    3636#include <VBox/sup.h>
    3737#include <iprt/errcore.h>
    38 #include <errno.h>
    39 #include <stdio.h>
    40 #include <stdlib.h>
     38#ifndef IPRT_NO_CRT
     39# include <errno.h>
     40# include <stdio.h>
     41# include <stdlib.h>
     42#endif
    4143
    4244#include <iprt/alloc.h>
  • trunk/src/VBox/Runtime/r3/init.cpp

    r95806 r95818  
    8989static bool volatile        g_fInitializing = false;
    9090
     91#if !defined(IPRT_NO_CRT) || !defined(RT_OS_WINDOWS)
    9192/** The process path.
    9293 * This is used by RTPathExecDir and RTProcGetExecutablePath and set by rtProcInitName. */
     
    9495/** The length of g_szrtProcExePath. */
    9596DECL_HIDDEN_DATA(size_t)    g_cchrtProcExePath;
     97/** The offset of the process name into g_szrtProcExePath. */
     98DECL_HIDDEN_DATA(size_t)    g_offrtProcName;
     99#endif
    96100/** The length of directory path component of g_szrtProcExePath. */
    97101DECL_HIDDEN_DATA(size_t)    g_cchrtProcDir;
    98 /** The offset of the process name into g_szrtProcExePath. */
    99 DECL_HIDDEN_DATA(size_t)    g_offrtProcName;
    100102
    101103/** The IPRT init flags. */
  • trunk/src/VBox/Runtime/r3/win/nocrt-alloc-win.cpp

    r95802 r95818  
    104104}
    105105
    106 
    107 #undef RTMemReallocZTag
    108 RTDECL(void *) RTMemReallocZTag(void *pvOld, size_t cbOld, size_t cbNew, const char *pszTag)
    109 {
    110     RT_NOREF(pszTag, cbOld);
    111     if (pvOld)
    112         return HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, pvOld, cbNew);
    113     return HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, cbNew);
    114 }
    115 
  • trunk/src/VBox/Runtime/r3/win/nocrt-startup-exe-win.cpp

    r93115 r95818  
    4141#include <iprt/utf16.h>
    4242
     43#ifdef IPRT_NO_CRT
     44# include <iprt/asm.h>
     45# include <iprt/nocrt/stdlib.h>
     46#endif
     47
     48
     49/*********************************************************************************************************************************
     50*   Structures and Typedefs                                                                                                      *
     51*********************************************************************************************************************************/
     52#ifdef IPRT_NO_CRT
     53typedef struct RTNOCRTATEXITCHUNK
     54{
     55    PFNRTNOCRTATEXITCALLBACK apfnCallbacks[256];
     56} RTNOCRTATEXITCHUNK;
     57#endif
     58
    4359
    4460/*********************************************************************************************************************************
     
    5268RT_C_DECLS_END
    5369
     70#ifdef IPRT_NO_CRT
     71/** The first atexit() registration chunk. */
     72static RTNOCRTATEXITCHUNK   g_aAtExitPrealloc;
     73/** Array of atexit() callback chunk pointers. */
     74static RTNOCRTATEXITCHUNK  *g_apAtExit[8192 / 256] = { &g_aAtExitPrealloc, };
     75/** Chunk and callback index in one. */
     76static volatile uint32_t    g_idxNextAtExit        = 0;
     77#endif
     78
    5479
    5580/*********************************************************************************************************************************
     
    6085extern int main(int argc, char **argv, char **envp);    /* in program */
    6186
     87
     88#ifdef IPRT_NO_CRT
     89extern "C"
     90int rtnocrt_atexit(PFNRTNOCRTATEXITCALLBACK pfnCallback) RT_NOEXCEPT
     91{
     92    AssertPtr(pfnCallback);
     93
     94    /*
     95     * Allocate a table index.
     96     */
     97    uint32_t idx = ASMAtomicIncU32(&g_idxNextAtExit) - 1;
     98    AssertReturnStmt(idx < RT_ELEMENTS(g_apAtExit) * RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks),
     99                     ASMAtomicDecU32(&g_idxNextAtExit), -1);
     100
     101    /*
     102     * Make sure the table chunk is there.
     103     */
     104    uint32_t            idxChunk = idx / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
     105    RTNOCRTATEXITCHUNK *pChunk   = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
     106    if (!pChunk)
     107    {
     108        pChunk = (RTNOCRTATEXITCHUNK *)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*pChunk));
     109        AssertReturn(pChunk, -1); /* don't try decrement, someone could be racing us... */
     110
     111        if (!ASMAtomicCmpXchgPtr(&g_apAtExit[idxChunk], pChunk, NULL))
     112        {
     113            HeapFree(GetProcessHeap(), 0, pChunk);
     114
     115            pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
     116            Assert(pChunk);
     117        }
     118    }
     119
     120    /*
     121     * Add our callback.
     122     */
     123    pChunk->apfnCallbacks[idxChunk % RT_ELEMENTS(pChunk->apfnCallbacks)] = pfnCallback;
     124    return 0;
     125}
     126#endif
     127
     128
     129static int rtTerminateProcess(int32_t rcExit, bool fDoAtExit)
     130{
     131#ifdef IPRT_NO_CRT
     132    /*
     133     * Run atexit callback in reverse order.
     134     */
     135    if (fDoAtExit)
     136    {
     137        uint32_t idxAtExit = ASMAtomicReadU32(&g_idxNextAtExit);
     138        if (idxAtExit-- > 0)
     139        {
     140            uint32_t idxChunk    = idxAtExit / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
     141            uint32_t idxCallback = idxAtExit % RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
     142            for (;;)
     143            {
     144                RTNOCRTATEXITCHUNK *pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
     145                if (pChunk)
     146                {
     147                    do
     148                    {
     149                        PFNRTNOCRTATEXITCALLBACK pfnCallback = pChunk->apfnCallbacks[idxCallback];
     150                        if (pfnCallback) /* Can be NULL see registration code */
     151                            pfnCallback();
     152                    } while (idxCallback-- > 0);
     153                }
     154                if (idxChunk == 0)
     155                    break;
     156                idxChunk--;
     157                idxCallback = RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks) - 1;
     158            }
     159        }
     160    }
     161#else
     162    RT_NOREF(fDoAtExit);
     163#endif
     164
     165    /*
     166     * Terminate.
     167     */
     168    for (;;)
     169        NtTerminateProcess(NtCurrentProcess(), rcExit);
     170}
    62171
    63172
     
    79188        }
    80189        else
    81             RTMsgErrorExitFailure("initProcExecPath: RTUtf16ToUtf8Ex failed: %Rrc\n", rc);
     190            RTMsgError("initProcExecPath: RTUtf16ToUtf8Ex failed: %Rrc\n", rc);
    82191    }
    83192    else
    84         RTMsgErrorExitFailure("initProcExecPath: GetModuleFileNameW failed: %Rhrc\n", GetLastError());
    85 }
    86 
    87 
    88 void CustomMainEntrypoint(PPEB pPeb)
     193        RTMsgError("initProcExecPath: GetModuleFileNameW failed: %Rhrc\n", GetLastError());
     194}
     195
     196
     197DECLASM(void) CustomMainEntrypoint(PPEB pPeb)
    89198{
    90199    /*
     
    114223                 * Call the main function.
    115224                 */
     225                AssertCompile(sizeof(rcExit) == sizeof(int));
    116226                rcExit = (RTEXITCODE)main(cArgs, papszArgv, NULL /*envp*/);
    117227            }
     
    125235        rcExit = RTMsgErrorExitFailure("No command line\n");
    126236
    127     for (;;)
    128         NtTerminateProcess(NtCurrentProcess(), rcExit);
    129 }
    130 
     237    rtTerminateProcess(rcExit, true /*fDoAtExit*/);
     238}
     239
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette