1 //
2 // shared_initialization.cpp
3 //
4 // Copyright (c) Microsoft Corporation. All rights reserved.
5 //
6 // Shared initialization logic used by both the AppCRT and the DesktopCRT.
7 //
8 #include <corecrt_internal.h>
9
__acrt_execute_initializers(__acrt_initializer const * const first,__acrt_initializer const * const last)10 extern "C" bool __cdecl __acrt_execute_initializers(
11 __acrt_initializer const* const first,
12 __acrt_initializer const* const last
13 )
14 {
15 if (first == last)
16 return true;
17
18 // Execute the initializers in [first, last), in order:
19 __acrt_initializer const* it = first;
20 for (; it != last; ++it)
21 {
22 if (it->_initialize == nullptr)
23 continue;
24
25 if (!(it->_initialize)())
26 break;
27 }
28
29 // If we reached the end, all initializers completed successfully:
30 if (it == last)
31 return true;
32
33 // Otherwise, the initializer pointed to by it failed. We need to roll back
34 // the initialization by executing the uninitializers corresponding to each
35 // of the initializers that completed successfully:
36 for (; it != first; --it)
37 {
38 // During initialization roll back, we do not execute uninitializers
39 // that have no corresponding initializer:
40 if ((it - 1)->_initialize == nullptr || (it - 1)->_uninitialize == nullptr)
41 continue;
42
43 (it - 1)->_uninitialize(false);
44 }
45
46 return false;
47 }
48
__acrt_execute_uninitializers(__acrt_initializer const * const first,__acrt_initializer const * const last)49 extern "C" bool __cdecl __acrt_execute_uninitializers(
50 __acrt_initializer const* const first,
51 __acrt_initializer const* const last
52 )
53 {
54 if (first == last)
55 return true;
56
57 // Execute the uninitializers in [first, last), in reverse order:
58 for (__acrt_initializer const* it = last; it != first; --it)
59 {
60 if ((it - 1)->_uninitialize == nullptr)
61 continue;
62
63 (it - 1)->_uninitialize(false);
64 }
65
66 return true;
67 }
68