blob: b8660bb12fe5d8003e1c8d8608ba3932a9b87fb7 [file] [log] [blame]
Jon Griffiths1db20bf2001-01-10 23:59:25 +00001/*
2 * msvcrt.dll initialisation functions
3 *
4 * Copyright 2000 Jon Griffiths
Alexandre Julliard0799c1a2002-03-09 23:29:33 +00005 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Jon Griffiths1db20bf2001-01-10 23:59:25 +000019 */
20#include "msvcrt.h"
21
Alexandre Julliardbd1689e2002-01-22 00:57:16 +000022#include "wine/debug.h"
23
24WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000025
26/* Index to TLS */
Dimitrie O. Paun03774622004-06-25 01:19:15 +000027DWORD msvcrt_tls_index;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000028
Francois Gouget203a8f82001-04-10 21:16:07 +000029static inline BOOL msvcrt_init_tls(void);
30static inline BOOL msvcrt_free_tls(void);
Alexandre Julliard03250ff2004-09-01 04:55:52 +000031const char* msvcrt_get_reason(DWORD reason);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000032
Jon Griffiths1db20bf2001-01-10 23:59:25 +000033/*********************************************************************
34 * Init
35 */
Alexandre Julliard1e1313d2002-11-04 23:53:41 +000036BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000037{
Dimitrie O. Paun03774622004-06-25 01:19:15 +000038 thread_data_t *tls;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000039
Alexandre Julliardac13d2f2003-02-18 23:29:47 +000040 TRACE("(%p, %s, %p) pid(%lx), tid(%lx), tls(%ld)\n",
Francois Gouget203a8f82001-04-10 21:16:07 +000041 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
Alexandre Julliardac13d2f2003-02-18 23:29:47 +000042 GetCurrentProcessId(), GetCurrentThreadId(),
Dimitrie O. Paun03774622004-06-25 01:19:15 +000043 (long)msvcrt_tls_index);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000044
45 switch (fdwReason)
46 {
47 case DLL_PROCESS_ATTACH:
Francois Gouget203a8f82001-04-10 21:16:07 +000048 if (!msvcrt_init_tls())
Jon Griffiths1db20bf2001-01-10 23:59:25 +000049 return FALSE;
Peter Hunnisettd1a79ea2002-02-21 20:22:00 +000050 msvcrt_init_mt_locks();
Francois Gouget203a8f82001-04-10 21:16:07 +000051 msvcrt_init_io();
52 msvcrt_init_console();
53 msvcrt_init_args();
Juan Lange5b4a692005-05-13 17:44:28 +000054 msvcrt_init_signals();
Jon Griffiths1db20bf2001-01-10 23:59:25 +000055 MSVCRT_setlocale(0, "C");
56 TRACE("finished process init\n");
Alexandre Julliard44b42352002-07-19 03:24:50 +000057 break;
Jon Griffiths1db20bf2001-01-10 23:59:25 +000058 case DLL_THREAD_ATTACH:
Jon Griffiths1db20bf2001-01-10 23:59:25 +000059 break;
60 case DLL_PROCESS_DETACH:
Peter Hunnisettd1a79ea2002-02-21 20:22:00 +000061 msvcrt_free_mt_locks();
Alexandre Julliardaf0d2062002-07-05 21:23:07 +000062 msvcrt_free_io();
Francois Gouget203a8f82001-04-10 21:16:07 +000063 msvcrt_free_console();
64 msvcrt_free_args();
Juan Lange5b4a692005-05-13 17:44:28 +000065 msvcrt_free_signals();
Francois Gouget203a8f82001-04-10 21:16:07 +000066 if (!msvcrt_free_tls())
Jon Griffiths1db20bf2001-01-10 23:59:25 +000067 return FALSE;
68 TRACE("finished process free\n");
69 break;
70 case DLL_THREAD_DETACH:
71 /* Free TLS */
Dimitrie O. Paun03774622004-06-25 01:19:15 +000072 tls = TlsGetValue(msvcrt_tls_index);
Michael Stefaniuc5ad7d852004-12-23 17:06:43 +000073 HeapFree(GetProcessHeap(), 0, tls);
Jon Griffiths1db20bf2001-01-10 23:59:25 +000074 TRACE("finished thread free\n");
75 break;
76 }
77 return TRUE;
78}
79
Francois Gouget203a8f82001-04-10 21:16:07 +000080static inline BOOL msvcrt_init_tls(void)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000081{
Dimitrie O. Paun03774622004-06-25 01:19:15 +000082 msvcrt_tls_index = TlsAlloc();
Jon Griffiths1db20bf2001-01-10 23:59:25 +000083
Dimitrie O. Paun03774622004-06-25 01:19:15 +000084 if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000085 {
86 ERR("TlsAlloc() failed!\n");
87 return FALSE;
88 }
89 return TRUE;
90}
91
Francois Gouget203a8f82001-04-10 21:16:07 +000092static inline BOOL msvcrt_free_tls(void)
Jon Griffiths1db20bf2001-01-10 23:59:25 +000093{
Dimitrie O. Paun03774622004-06-25 01:19:15 +000094 if (!TlsFree(msvcrt_tls_index))
Jon Griffiths1db20bf2001-01-10 23:59:25 +000095 {
96 ERR("TlsFree() failed!\n");
97 return FALSE;
98 }
99 return TRUE;
100}
101
Francois Gouget203a8f82001-04-10 21:16:07 +0000102const char* msvcrt_get_reason(DWORD reason)
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000103{
104 switch (reason)
105 {
106 case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
107 case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
108 case DLL_THREAD_ATTACH: return "DLL_THREAD_ATTACH";
109 case DLL_THREAD_DETACH: return "DLL_THREAD_DETACH";
110 }
111 return "UNKNOWN";
112}
113
114
115/*********************************************************************
Patrik Stridvall044855c2001-07-11 18:56:41 +0000116 * $I10_OUTPUT (MSVCRT.@)
117 * Function not really understood but needed to make the DLL work
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000118 */
119void MSVCRT_I10_OUTPUT(void)
120{
121 /* FIXME: This is probably data, not a function */
Rein Klazes03d9cf22004-12-10 15:28:25 +0000122 /* no it is a function. I10 is an Int of 10 bytes */
123 /* also known as 80 bit flotaing point (long double */
124 /* for some compilers, not MSVC) */
Jon Griffiths1db20bf2001-01-10 23:59:25 +0000125}
Rein Klazes03d9cf22004-12-10 15:28:25 +0000126
127/*********************************************************************
128 * _adjust_fdiv (MSVCRT.@)
129 * Used by the MSVC compiler to work around the Pentium FDIV bug.
130 */
131int MSVCRT__adjust_fdiv = 0;