VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/compiler/vcc/atexit-vcc.cpp@ 95870

Last change on this file since 95870 was 95870, checked in by vboxsync, 3 years ago

IPRT/nocrt: Split up the startup code and added the basic DLL startup code. bugref:10261

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.3 KB
Line 
1/* $Id: atexit-vcc.cpp 95870 2022-07-27 02:38:01Z vboxsync $ */
2/** @file
3 * IPRT - Visual C++ Compiler - Simple atexit implementation.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "internal/iprt.h"
32
33#include <iprt/asm.h>
34#include <iprt/mem.h>
35#include <iprt/nocrt/stdlib.h>
36
37#include "internal/compiler-vcc.h"
38
39
40/*********************************************************************************************************************************
41* Structures and Typedefs *
42*********************************************************************************************************************************/
43typedef struct RTNOCRTATEXITCHUNK
44{
45 PFNRTNOCRTATEXITCALLBACK apfnCallbacks[256];
46} RTNOCRTATEXITCHUNK;
47
48
49/*********************************************************************************************************************************
50* Global Variables *
51*********************************************************************************************************************************/
52/** The first atexit() registration chunk. */
53static RTNOCRTATEXITCHUNK g_aAtExitPrealloc;
54/** Array of atexit() callback chunk pointers. */
55static RTNOCRTATEXITCHUNK *g_apAtExit[8192 / 256] = { &g_aAtExitPrealloc, };
56/** Chunk and callback index in one. */
57static volatile uint32_t g_idxNextAtExit = 0;
58
59
60/* Note! not using atexit here because it'll clash with built-in prototype. */
61extern "C" int rtnocrt_atexit(PFNRTNOCRTATEXITCALLBACK pfnCallback) RT_NOEXCEPT
62{
63 AssertPtr(pfnCallback);
64
65 /*
66 * Allocate a table index.
67 */
68 uint32_t idx = ASMAtomicIncU32(&g_idxNextAtExit) - 1;
69 AssertReturnStmt(idx < RT_ELEMENTS(g_apAtExit) * RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks),
70 ASMAtomicDecU32(&g_idxNextAtExit), -1);
71
72 /*
73 * Make sure the table chunk is there.
74 */
75 uint32_t idxChunk = idx / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
76 RTNOCRTATEXITCHUNK *pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
77 if (!pChunk)
78 {
79 pChunk = (RTNOCRTATEXITCHUNK *)RTMemAllocZ(sizeof(*pChunk)); /* ASSUMES that the allocator works w/o initialization! */
80 AssertReturn(pChunk, -1); /* don't try decrement, someone could be racing us... */
81
82 if (!ASMAtomicCmpXchgPtr(&g_apAtExit[idxChunk], pChunk, NULL))
83 {
84 RTMemFree(pChunk);
85
86 pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
87 Assert(pChunk);
88 }
89 }
90
91 /*
92 * Add our callback.
93 */
94 pChunk->apfnCallbacks[idxChunk % RT_ELEMENTS(pChunk->apfnCallbacks)] = pfnCallback;
95 return 0;
96}
97
98
99void rtVccTermRunAtExit(void) RT_NOEXCEPT
100{
101 uint32_t idxAtExit = ASMAtomicReadU32(&g_idxNextAtExit);
102 if (idxAtExit-- > 0)
103 {
104 uint32_t idxChunk = idxAtExit / RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
105 uint32_t idxCallback = idxAtExit % RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
106 for (;;)
107 {
108 RTNOCRTATEXITCHUNK *pChunk = ASMAtomicReadPtrT(&g_apAtExit[idxChunk], RTNOCRTATEXITCHUNK *);
109 if (pChunk)
110 {
111 do
112 {
113 g_idxNextAtExit = idxAtExit--; /* Make sure we don't try problematic atexit callbacks! */
114
115 PFNRTNOCRTATEXITCALLBACK pfnCallback = pChunk->apfnCallbacks[idxCallback];
116 if (pfnCallback) /* Can be NULL see registration code */
117 {
118 pfnCallback();
119 pChunk->apfnCallbacks[idxCallback] = NULL;
120 }
121 } while (idxCallback-- > 0);
122 }
123 else
124 idxAtExit -= RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks);
125 if (idxChunk == 0)
126 break;
127 idxChunk--;
128 idxCallback = RT_ELEMENTS(g_apAtExit[0]->apfnCallbacks) - 1;
129 }
130
131 g_idxNextAtExit = 0;
132 }
133}
134
Note: See TracBrowser for help on using the repository browser.

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