Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

786 lines
24 KiB

  1. /***
  2. *crt0dat.c - 32-bit C run-time initialization/termination routines
  3. *
  4. * Copyright (c) 1986-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * This module contains the routines _cinit, exit, and _exit
  8. * for C run-time startup and termination. _cinit and exit
  9. * are called from the _astart code in crt0.asm.
  10. * This module also defines several data variables used by the
  11. * runtime.
  12. *
  13. * [NOTE: Lock segment definitions are at end of module.]
  14. *
  15. *Revision History:
  16. * 06-28-89 PHG Module created, based on asm version
  17. * 04-09-90 GJF Added #include <cruntime.h>. Made calling type
  18. * explicit (_CALLTYPE1 or _CALLTYPE4). Also, fixed
  19. * the copyright.
  20. * 04-10-90 GJF Fixed compiler warnings (-W3).
  21. * 05-21-90 GJF Added #undef _NFILE_ (temporary hack) and fixed the
  22. * indents.
  23. * 08-31-90 GJF Removed 32 from API names.
  24. * 09-25-90 GJF Merged tree version with local (8-31 and 5-21 changes).
  25. * 10-08-90 GJF New-style function declarators.
  26. * 10-12-90 GJF Removed divide by 0 stuff.
  27. * 10-18-90 GJF Added _pipech[] array.
  28. * 11-05-90 GJF Added _umaskval.
  29. * 12-04-90 GJF Added _osfinfo[] definition for Win32 target. Note that
  30. * the Win32 support is still incomplete!
  31. * 12-04-90 SRW Changed to include <oscalls.h> instead of <doscalls.h>
  32. * 12-04-90 SRW Added _osfile back for win32. Changed _osfinfo from
  33. * an array of structures to an array of 32-bit handles
  34. * (_osfhnd)
  35. * 12-28-90 SRW Added _CRUISER_ conditional around pack pragmas
  36. * 01-29-91 GJF ANSI naming.
  37. * 01-29-91 SRW Added call to GetFileType [_WIN32_]
  38. * 02-18-91 SRW Removed duplicate defintion of _NFILE_ (see mtdll.h)
  39. * 04-04-91 GJF Added definitions for _base[version|major|minor]
  40. * (_WIN32_).
  41. * 04-08-91 GJF Temporary hack for Win32/DOS folks - added HeapDestroy
  42. * call to doexit to tear down the OS heap used by C
  43. * heap.
  44. * 04-09-91 PNT Added _MAC_ conditional
  45. * 04-26-91 SRW Removed level 3 warnings
  46. * 07-16-91 GJF Added fp initialization test-and-call [_WIN32_].
  47. * 07-26-91 GJF Revised initialization and termination stuff. In
  48. * particular, removed need for win32ini.c [_WIN32_].
  49. * 08-07-91 GJF Added init. for FORTRAN runtime, if present [_WIN32_].
  50. * 08-21-91 GJF Test _prmtmp against NULL, not _prmtmp().
  51. * 08-21-91 JCR Added _exitflag, _endstdio, _cpumode, etc.
  52. * 09-09-91 GJF Revised _doinitterm for C++ init. support and to make
  53. * _onexit/atexit compatible with C++ needs.
  54. * 09-16-91 GJF Must test __onexitend before calling _doinitterm.
  55. * 10-29-91 GJF Force in floating point initialization for MIPS
  56. * compiler [_WIN32_].
  57. * 11-13-91 GJF FORTRAN needs _onexit/atexit init. before call thru
  58. * _pFFinit.
  59. * 12-29-91 RID MAC module created, based on OS2 version
  60. * 01-10-92 GJF Merged. Also, added _C_Termination_Done [_WIN32_].
  61. * 02-13-92 GJF Moved all lowio initialization to ioinit.c for Win32.
  62. * 03-12-92 SKS Major changes to initialization/termination scheme
  63. * 04-01-92 XY implemented new intialization/termination schema (MAC)
  64. * 04-16-92 DJM POSIX support.
  65. * 04-17-92 SKS Export _initterm() for CRTDLL model
  66. * 05-07-92 DJM Removed _exit() from POSIX build.
  67. * 06-03-92 GJF Temporarily restored call to FORTRAN init.
  68. * 08-26-92 SKS Add _osver, _winver, _winmajor, _winminor
  69. * 08-28-92 GJF Use unistd.h for POSIX build.
  70. * 09-02-92 SKS Fix _onexit table traversal to be LIFO.
  71. * Since table is built forward (my changes 03-12-92)
  72. * the table must be traversed in reverse order.
  73. * 11-12-92 SKS Remove hard-coded call to FORTRAN initializer
  74. * 03-20-93 SKS Remove obsolete variables _osmode, _cpumode, etc.
  75. * 04-06-93 SKS Replace _CRTAPI* with __cdecl
  76. * 04-07-93 SKS Change to __declspec(dllexport) for CRT DLL model
  77. * 04-19-93 SKS Remove obsolete variable _child.
  78. * 04-20-93 SKS _C_Termination_Done is now used by DLLs in LIBC/LIBCMT
  79. * models, not just in MSVCRT10.DLL.
  80. * 07-16-93 SRW ALPHA Merge
  81. * 09-21-93 CFW Move _initmbctable call to _cinit().
  82. * 10-19-93 GJF Merged NT and Cuda versions. Cleaned out a lot of old
  83. * Cruiser and Dosx32 support. Replaced MTHREAD with
  84. * _MT, _MIPS_ with _M_MRX000, _ALPHA_ with _M_ALPHA.
  85. * 11-09-93 GJF Moved _initmbctable call back into crt0.c.
  86. * 11-09-93 GJF Replaced PF with _PVFV (defined in internal.h).
  87. * 11-19-93 CFW Add _wargv, _wpgmptr.
  88. * 11-29-93 CFW Add _wenviron.
  89. * 12-15-93 CFW Set _pgmptr, _wpgmptr to NULL (MIPS compiler bug).
  90. * 02-04-94 CFW Add _[w]initenv.
  91. * 03-25-94 GJF Made definitions of:
  92. * __argc,
  93. * __argv, __wargv,
  94. * _C_Termination_Done,
  95. * _environ, _wenviron,
  96. * _exitflag,
  97. * __initenv, __winitenv,
  98. * __onexitbegin, __onexitend,
  99. * _osver,
  100. * _pgmptr, _wpgmptr,
  101. * _winmajor,
  102. * _winminor
  103. * _winver
  104. * conditional on ndef DLL_FOR_WIN32S.
  105. * 10-02-94 BWT Add PPC changes
  106. * 12-03-94 SKS Clean up OS/2 references
  107. * 02-11-95 CFW PPC -> _M_MPPC.
  108. * 02-16-95 JWM Spliced _WIN32 & Mac versions.
  109. * 02-24-95 CFW Call _CrtDumpMemoryLeaks.
  110. * 02-27-95 CFW Make _CrtDumpMemoryLeaks call conditional
  111. * 04-06-95 CFW Only check for static libs, avoid infinite loop.
  112. * 07-24-95 CFW Call _CrtDumpMemoryLeaks for PMac.
  113. * 12-18-95 JWM doexit() can no longer be called recursively.
  114. * 08-01-96 RDK For PMac, define _osflagfiles, cleaned up Gestalt test,
  115. * and make termination parallel x86 functionality.
  116. * 07-24-97 GJF Added __env_initialized flag.
  117. * 08-06-97 GJF Moved __mbctype_initialized flag here from crt0.c.
  118. * 09-26-97 BWT Fix POSIX
  119. * 10-07-97 RDL Added IA64.
  120. * 01-16-98 RDL Removed IA64 from fpinit #if for _fltused support.
  121. * 10-02-98 GJF Added _osplatform.
  122. * 11-13-98 KBF Only do an atexit(RTC_Terminate) - moved Init to after
  123. * _heap_init
  124. * 02-01-99 GJF Slight change to terminator execution loop to allow
  125. * terminators to register more terminators.
  126. * 04-28-99 PML Wrap __declspec(allocate()) in _CRTALLOC macro.
  127. * 05-11-99 KBF Wrap RTC support in #ifdef.
  128. * 05-17-99 PML Remove all Macintosh support.
  129. * 03-06-00 PML Add __crtExitProcess for COM+ exit processing.
  130. * 04-28-00 BWT Fix Posix
  131. * 08-04-00 PML COM+ -> managed (VS7#117746).
  132. * 03-27-01 PML .CRT$XI funcs now return an error status (VS7#231220).
  133. * 05-01-01 BWT Remove TerminateProcess call. It's not necessary when
  134. * simply exiting would serve the same purpose.
  135. *
  136. *******************************************************************************/
  137. #include <cruntime.h>
  138. #ifdef _POSIX_
  139. #include <unistd.h>
  140. #else
  141. #include <msdos.h>
  142. #include <rtcapi.h>
  143. #endif
  144. #include <dos.h>
  145. #include <oscalls.h>
  146. #include <mtdll.h>
  147. #include <internal.h>
  148. #include <stdio.h>
  149. #include <stdlib.h>
  150. #include <process.h>
  151. #include <dbgint.h>
  152. #include <sect_attribs.h>
  153. /* define errno */
  154. #ifndef _MT
  155. int errno = 0; /* libc error value */
  156. unsigned long _doserrno = 0; /* OS system error value */
  157. #endif /* _MT */
  158. /* define umask */
  159. int _umaskval = 0;
  160. /* define version info variables */
  161. _CRTIMP unsigned int _osplatform = 0;
  162. _CRTIMP unsigned int _osver = 0;
  163. _CRTIMP unsigned int _winver = 0;
  164. _CRTIMP unsigned int _winmajor = 0;
  165. _CRTIMP unsigned int _winminor = 0;
  166. /* argument vector and environment */
  167. _CRTIMP int __argc = 0;
  168. _CRTIMP char **__argv = NULL;
  169. _CRTIMP wchar_t **__wargv = NULL;
  170. #ifdef _POSIX_
  171. char **environ = NULL;
  172. #else
  173. _CRTIMP char **_environ = NULL;
  174. _CRTIMP char **__initenv = NULL;
  175. _CRTIMP wchar_t **_wenviron = NULL;
  176. _CRTIMP wchar_t **__winitenv = NULL;
  177. #endif
  178. _CRTIMP char *_pgmptr = NULL; /* ptr to program name */
  179. _CRTIMP wchar_t *_wpgmptr = NULL; /* ptr to wide program name */
  180. /* callable exit flag */
  181. char _exitflag = 0;
  182. /*
  183. * flag indicating if C runtime termination has been done. set if exit,
  184. * _exit, _cexit or _c_exit has been called. checked when _CRTDLL_INIT
  185. * is called with DLL_PROCESS_DETACH.
  186. */
  187. int _C_Termination_Done = FALSE;
  188. int _C_Exit_Done = FALSE;
  189. #ifndef CRTDLL
  190. /*
  191. * Flag checked by getenv() and _putenv() to determine if the environment has
  192. * been initialized.
  193. */
  194. int __env_initialized;
  195. #endif
  196. #ifdef _MBCS
  197. /*
  198. * Flag to ensure multibyte ctype table is only initialized once
  199. */
  200. int __mbctype_initialized;
  201. #endif /* _MBCS */
  202. /*
  203. * NOTE: THE USE OF THE POINTERS DECLARED BELOW DEPENDS ON THE PROPERTIES
  204. * OF C COMMUNAL VARIABLES. SPECIFICALLY, THEY ARE NON-NULL IFF THERE EXISTS
  205. * A DEFINITION ELSEWHERE INITIALIZING THEM TO NON-NULL VALUES.
  206. */
  207. /*
  208. * pointers to initialization functions
  209. */
  210. _PVFV _FPinit; /* floating point init. */
  211. /*
  212. * pointers to initialization sections
  213. */
  214. extern _CRTALLOC(".CRT$XIA") _PIFV __xi_a[];
  215. extern _CRTALLOC(".CRT$XIZ") _PIFV __xi_z[]; /* C initializers */
  216. extern _CRTALLOC(".CRT$XCA") _PVFV __xc_a[];
  217. extern _CRTALLOC(".CRT$XCZ") _PVFV __xc_z[]; /* C++ initializers */
  218. extern _CRTALLOC(".CRT$XPA") _PVFV __xp_a[];
  219. extern _CRTALLOC(".CRT$XPZ") _PVFV __xp_z[]; /* C pre-terminators */
  220. extern _CRTALLOC(".CRT$XTA") _PVFV __xt_a[];
  221. extern _CRTALLOC(".CRT$XTZ") _PVFV __xt_z[]; /* C terminators */
  222. #if defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC) || defined(_M_IA64)
  223. /*
  224. * For MIPS compiler, must explicitly force in and call the floating point
  225. * initialization.
  226. */
  227. extern void __cdecl _fpmath(void);
  228. #endif
  229. /*
  230. * pointers to the start and finish of the _onexit/atexit table
  231. */
  232. _PVFV *__onexitbegin;
  233. _PVFV *__onexitend;
  234. /*
  235. * static (internal) functions that walk a table of function pointers,
  236. * calling each entry between the two pointers, skipping NULL entries
  237. *
  238. * _initterm needs to be exported for CRT DLL so that C++ initializers in the
  239. * client EXE / DLLs can be initialized.
  240. *
  241. * _initterm_e calls function pointers that return a nonzero error code to
  242. * indicate an initialization failed fatally.
  243. */
  244. #ifdef CRTDLL
  245. void __cdecl _initterm(_PVFV *, _PVFV *);
  246. #else
  247. static void __cdecl _initterm(_PVFV *, _PVFV *);
  248. #endif
  249. static int __cdecl _initterm_e(_PIFV *, _PIFV *);
  250. /***
  251. *_cinit - C initialization
  252. *
  253. *Purpose:
  254. * This routine performs the shared DOS and Windows initialization.
  255. * The following order of initialization must be preserved -
  256. *
  257. * 1. Check for devices for file handles 0 - 2
  258. * 2. Integer divide interrupt vector setup
  259. * 3. General C initializer routines
  260. *
  261. *Entry:
  262. * No parameters: Called from __crtstart and assumes data
  263. * set up correctly there.
  264. *
  265. *Exit:
  266. * Initializes C runtime data.
  267. * Returns 0 if all .CRT$XI internal initializations succeeded, else
  268. * the _RT_* fatal error code encountered.
  269. *
  270. *Exceptions:
  271. *
  272. *******************************************************************************/
  273. int __cdecl _cinit (
  274. void
  275. )
  276. {
  277. int initret;
  278. /*
  279. * initialize floating point package, if present
  280. */
  281. #if defined(_M_MRX000) || defined(_M_ALPHA) || defined(_M_PPC)
  282. /*
  283. * MIPS compiler doesn't emit external reference to _fltused. Therefore,
  284. * must always force in the floating point initialization.
  285. */
  286. _fpmath();
  287. #else
  288. if ( _FPinit != NULL )
  289. (*_FPinit)();
  290. #endif
  291. /*
  292. * do initializations
  293. */
  294. initret = _initterm_e( __xi_a, __xi_z );
  295. if ( initret != 0 )
  296. return initret;
  297. #ifdef _RTC
  298. atexit(_RTC_Terminate);
  299. #endif
  300. /*
  301. * do C++ initializations
  302. */
  303. _initterm( __xc_a, __xc_z );
  304. return 0;
  305. }
  306. /***
  307. *exit(status), _exit(status), _cexit(void), _c_exit(void) - C termination
  308. *
  309. *Purpose:
  310. *
  311. * Entry points:
  312. *
  313. * exit(code): Performs all the C termination functions
  314. * and terminates the process with the return code
  315. * supplied by the user.
  316. *
  317. * _exit(code): Performs a quick exit routine that does not
  318. * do certain 'high-level' exit processing. The _exit
  319. * routine terminates the process with the return code
  320. * supplied by the user.
  321. *
  322. * _cexit(): Performs the same C lib termination processing
  323. * as exit(code) but returns control to the caller
  324. * when done (i.e., does NOT terminate the process).
  325. *
  326. * _c_exit(): Performs the same C lib termination processing
  327. * as _exit(code) but returns control to the caller
  328. * when done (i.e., does NOT terminate the process).
  329. *
  330. * Termination actions:
  331. *
  332. * exit(), _cexit():
  333. *
  334. * 1. call user's terminator routines
  335. * 2. call C runtime preterminators
  336. *
  337. * _exit(), _c_exit():
  338. *
  339. * 3. call C runtime terminators
  340. * 4. return to DOS or caller
  341. *
  342. * Notes:
  343. *
  344. * The termination sequence is complicated due to the multiple entry
  345. * points sharing the common code body while having different entry/exit
  346. * sequences.
  347. *
  348. * Multi-thread notes:
  349. *
  350. * 1. exit() should NEVER be called when mthread locks are held.
  351. * The exit() routine can make calls that try to get mthread locks.
  352. *
  353. * 2. _exit()/_c_exit() can be called from anywhere, with or without locks held.
  354. * Thus, _exit() can NEVER try to get locks (otherwise, deadlock
  355. * may occur). _exit() should always 'work' (i.e., the process
  356. * should always terminate successfully).
  357. *
  358. * 3. Only one thread is allowed into the exit code (see _lockexit()
  359. * and _unlockexit() routines).
  360. *
  361. *Entry:
  362. * exit(), _exit()
  363. * int status - exit status (0-255)
  364. *
  365. * _cexit(), _c_exit()
  366. * <no input>
  367. *
  368. *Exit:
  369. * exit(), _exit()
  370. * <EXIT to DOS>
  371. *
  372. * _cexit(), _c_exit()
  373. * Return to caller
  374. *
  375. *Uses:
  376. *
  377. *Exceptions:
  378. *
  379. *******************************************************************************/
  380. /* worker routine prototype */
  381. static void __cdecl doexit (int code, int quick, int retcaller);
  382. void __cdecl exit (
  383. int code
  384. )
  385. {
  386. doexit(code, 0, 0); /* full term, kill process */
  387. }
  388. #ifndef _POSIX_
  389. void __cdecl _exit (
  390. int code
  391. )
  392. {
  393. doexit(code, 1, 0); /* quick term, kill process */
  394. }
  395. void __cdecl _cexit (
  396. void
  397. )
  398. {
  399. doexit(0, 0, 1); /* full term, return to caller */
  400. }
  401. void __cdecl _c_exit (
  402. void
  403. )
  404. {
  405. doexit(0, 1, 1); /* quick term, return to caller */
  406. }
  407. #endif /* _POSIX_ */
  408. static void __cdecl doexit (
  409. int code,
  410. int quick,
  411. int retcaller
  412. )
  413. {
  414. #ifdef _DEBUG
  415. static int fExit = 0;
  416. #endif /* _DEBUG */
  417. #ifdef _MT
  418. _lockexit(); /* assure only 1 thread in exit path */
  419. #endif
  420. #ifndef _POSIX_
  421. if (_C_Exit_Done == TRUE) /* if doexit() is being called recursively */
  422. goto ExitBranch;
  423. #endif
  424. _C_Termination_Done = TRUE;
  425. /* save callable exit flag (for use by terminators) */
  426. _exitflag = (char) retcaller; /* 0 = term, !0 = callable exit */
  427. if (!quick) {
  428. /*
  429. * do _onexit/atexit() terminators
  430. * (if there are any)
  431. *
  432. * These terminators MUST be executed in reverse order (LIFO)!
  433. *
  434. * NOTE:
  435. * This code assumes that __onexitbegin points
  436. * to the first valid onexit() entry and that
  437. * __onexitend points past the last valid entry.
  438. * If __onexitbegin == __onexitend, the table
  439. * is empty and there are no routines to call.
  440. */
  441. if (__onexitbegin) {
  442. while ( --__onexitend >= __onexitbegin )
  443. /*
  444. * if current table entry is non-NULL,
  445. * call thru it.
  446. */
  447. if ( *__onexitend != NULL )
  448. (**__onexitend)();
  449. }
  450. /*
  451. * do pre-terminators
  452. */
  453. _initterm(__xp_a, __xp_z);
  454. }
  455. /*
  456. * do terminators
  457. */
  458. _initterm(__xt_a, __xt_z);
  459. #ifndef CRTDLL
  460. #ifdef _DEBUG
  461. /* Dump all memory leaks */
  462. if (!fExit && _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & _CRTDBG_LEAK_CHECK_DF)
  463. {
  464. fExit = 1;
  465. _CrtDumpMemoryLeaks();
  466. }
  467. #endif
  468. #endif
  469. #ifndef _POSIX_
  470. ExitBranch:
  471. #endif
  472. /* return to OS or to caller */
  473. if (retcaller) {
  474. #ifdef _MT
  475. _unlockexit(); /* unlock the exit code path */
  476. #endif
  477. return;
  478. }
  479. #ifdef _POSIX_
  480. _exit(code);
  481. }
  482. #else /* ndef _POSIX_ */
  483. _C_Exit_Done = TRUE;
  484. __crtExitProcess(code);
  485. }
  486. /***
  487. * __crtExitProcess - CRT wrapper for ExitProcess
  488. *
  489. *Purpose:
  490. * If we're part of a managed app, then call the CorExitProcess,
  491. * otherwise call ExitProcess. For managed apps, calling ExitProcess can
  492. * be problematic, because it doesn't give the managed FinalizerThread a
  493. * chance to clean up.
  494. *
  495. * To determine if we're a managed app, we check if mscoree.dll is loaded.
  496. * Then, if CorExitProcess is available, we call it.
  497. *
  498. *Entry:
  499. * int status - exit code
  500. *
  501. *Exit:
  502. * Does not return
  503. *
  504. *Exceptions:
  505. *
  506. *******************************************************************************/
  507. typedef void (WINAPI * PFN_EXIT_PROCESS)(UINT uExitCode);
  508. void __cdecl __crtExitProcess (
  509. int status
  510. )
  511. {
  512. HMODULE hmod;
  513. PFN_EXIT_PROCESS pfn;
  514. hmod = GetModuleHandle("mscoree.dll");
  515. if (hmod != NULL) {
  516. pfn = (PFN_EXIT_PROCESS)GetProcAddress(hmod, "CorExitProcess");
  517. if (pfn != NULL) {
  518. pfn(status);
  519. }
  520. }
  521. /*
  522. * Either mscoree.dll isn't loaded,
  523. * or CorExitProcess isn't exported from mscoree.dll,
  524. * or CorExitProcess returned (should never happen).
  525. * Just call ExitProcess.
  526. */
  527. ExitProcess(status);
  528. }
  529. #endif /* _POSIX_ */
  530. #ifdef _MT
  531. /***
  532. * _lockexit - Aquire the exit code lock
  533. *
  534. *Purpose:
  535. * Makes sure only one thread is in the exit code at a time.
  536. * If a thread is already in the exit code, it must be allowed
  537. * to continue. All other threads must pend.
  538. *
  539. * Notes:
  540. *
  541. * (1) It is legal for a thread that already has the lock to
  542. * try and get it again(!). That is, consider the following
  543. * sequence:
  544. *
  545. * (a) program calls exit()
  546. * (b) thread locks exit code
  547. * (c) user onexit() routine calls _exit()
  548. * (d) same thread tries to lock exit code
  549. *
  550. * Since _exit() must ALWAYS be able to work (i.e., can be called
  551. * from anywhere with no regard for locking), we must make sure the
  552. * program does not deadlock at step (d) above.
  553. *
  554. * (2) If a thread executing exit() or _exit() aquires the exit lock,
  555. * other threads trying to get the lock will pend forever. That is,
  556. * since exit() and _exit() terminate the process, there is not need
  557. * for them to unlock the exit code path.
  558. *
  559. * (3) Note that onexit()/atexit() routines call _lockexit/_unlockexit
  560. * to protect mthread access to the onexit table.
  561. *
  562. * (4) The 32-bit OS semaphore calls DO allow a single thread to acquire
  563. * the same lock multiple times* thus, this version is straight forward.
  564. *
  565. *Entry: <none>
  566. *
  567. *Exit:
  568. * Calling thread has exit code path locked on return.
  569. *
  570. *Exceptions:
  571. *
  572. *******************************************************************************/
  573. void __cdecl _lockexit (
  574. void
  575. )
  576. {
  577. _mlock(_EXIT_LOCK1);
  578. }
  579. /***
  580. * _unlockexit - Release exit code lock
  581. *
  582. *Purpose:
  583. * [See _lockexit() description above.]
  584. *
  585. * This routine is called by _cexit(), _c_exit(), and onexit()/atexit().
  586. * The exit() and _exit() routines never unlock the exit code path since
  587. * they are terminating the process.
  588. *
  589. *Entry:
  590. * Exit code path is unlocked.
  591. *
  592. *Exit:
  593. *
  594. *Exceptions:
  595. *
  596. *******************************************************************************/
  597. void __cdecl _unlockexit (
  598. void
  599. )
  600. {
  601. _munlock(_EXIT_LOCK1);
  602. }
  603. #endif /* _MT */
  604. /***
  605. * static void _initterm(_PVFV * pfbegin, _PVFV * pfend) - call entries in
  606. * function pointer table
  607. *
  608. *Purpose:
  609. * Walk a table of function pointers, calling each entry, as follows:
  610. *
  611. * 1. walk from beginning to end, pfunctbl is assumed to point
  612. * to the beginning of the table, which is currently a null entry,
  613. * as is the end entry.
  614. * 2. skip NULL entries
  615. * 3. stop walking when the end of the table is encountered
  616. *
  617. *Entry:
  618. * _PVFV *pfbegin - pointer to the beginning of the table (first
  619. * valid entry).
  620. * _PVFV *pfend - pointer to the end of the table (after last
  621. * valid entry).
  622. *
  623. *Exit:
  624. * No return value
  625. *
  626. *Notes:
  627. * This routine must be exported in the CRT DLL model so that the client
  628. * EXE and client DLL(s) can call it to initialize their C++ constructors.
  629. *
  630. *Exceptions:
  631. * If either pfbegin or pfend is NULL, or invalid, all bets are off!
  632. *
  633. *******************************************************************************/
  634. #ifdef CRTDLL
  635. void __cdecl _initterm (
  636. #else
  637. static void __cdecl _initterm (
  638. #endif
  639. _PVFV * pfbegin,
  640. _PVFV * pfend
  641. )
  642. {
  643. /*
  644. * walk the table of function pointers from the bottom up, until
  645. * the end is encountered. Do not skip the first entry. The initial
  646. * value of pfbegin points to the first valid entry. Do not try to
  647. * execute what pfend points to. Only entries before pfend are valid.
  648. */
  649. while ( pfbegin < pfend )
  650. {
  651. /*
  652. * if current table entry is non-NULL, call thru it.
  653. */
  654. if ( *pfbegin != NULL )
  655. (**pfbegin)();
  656. ++pfbegin;
  657. }
  658. }
  659. /***
  660. * static int _initterm_e(_PIFV * pfbegin, _PIFV * pfend) - call entries in
  661. * function pointer table, return error code on any failure
  662. *
  663. *Purpose:
  664. * Walk a table of function pointers in the same way as _initterm, but
  665. * here the functions return an error code. If an error is returned, it
  666. * will be a nonzero value equal to one of the _RT_* codes.
  667. *
  668. *Entry:
  669. * _PIFV *pfbegin - pointer to the beginning of the table (first
  670. * valid entry).
  671. * _PIFV *pfend - pointer to the end of the table (after last
  672. * valid entry).
  673. *
  674. *Exit:
  675. * No return value
  676. *
  677. *Notes:
  678. * This routine must be exported in the CRT DLL model so that the client
  679. * EXE and client DLL(s) can call it.
  680. *
  681. *Exceptions:
  682. * If either pfbegin or pfend is NULL, or invalid, all bets are off!
  683. *
  684. *******************************************************************************/
  685. static int __cdecl _initterm_e (
  686. _PIFV * pfbegin,
  687. _PIFV * pfend
  688. )
  689. {
  690. int ret = 0;
  691. /*
  692. * walk the table of function pointers from the bottom up, until
  693. * the end is encountered. Do not skip the first entry. The initial
  694. * value of pfbegin points to the first valid entry. Do not try to
  695. * execute what pfend points to. Only entries before pfend are valid.
  696. */
  697. while ( pfbegin < pfend && ret == 0)
  698. {
  699. /*
  700. * if current table entry is non-NULL, call thru it.
  701. */
  702. if ( *pfbegin != NULL )
  703. ret = (**pfbegin)();
  704. ++pfbegin;
  705. }
  706. return ret;
  707. }