dbghelp: Implement SymEnumSourceFilesW.
[wine] / dlls / msvcrt / main.c
1 /*
2  * msvcrt.dll initialisation functions
3  *
4  * Copyright 2000 Jon Griffiths
5  *
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20 #include "msvcrt.h"
21 #include "winternl.h"
22
23 #include "wine/debug.h"
24
25 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
26
27 /* Index to TLS */
28 DWORD msvcrt_tls_index;
29
30 static const char* msvcrt_get_reason(DWORD reason)
31 {
32   switch (reason)
33   {
34   case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
35   case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
36   case DLL_THREAD_ATTACH:  return "DLL_THREAD_ATTACH";
37   case DLL_THREAD_DETACH:  return "DLL_THREAD_DETACH";
38   }
39   return "UNKNOWN";
40 }
41
42 static inline BOOL msvcrt_init_tls(void)
43 {
44   msvcrt_tls_index = TlsAlloc();
45
46   if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
47   {
48     ERR("TlsAlloc() failed!\n");
49     return FALSE;
50   }
51   return TRUE;
52 }
53
54 static inline BOOL msvcrt_free_tls(void)
55 {
56   if (!TlsFree(msvcrt_tls_index))
57   {
58     ERR("TlsFree() failed!\n");
59     return FALSE;
60   }
61   return TRUE;
62 }
63
64 static inline void msvcrt_free_tls_mem(void)
65 {
66   thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
67
68   if (tls)
69   {
70     CloseHandle(tls->handle);
71     HeapFree(GetProcessHeap(),0,tls->efcvt_buffer);
72     HeapFree(GetProcessHeap(),0,tls->asctime_buffer);
73     HeapFree(GetProcessHeap(),0,tls->wasctime_buffer);
74     HeapFree(GetProcessHeap(),0,tls->strerror_buffer);
75     HeapFree(GetProcessHeap(),0,tls->wcserror_buffer);
76     HeapFree(GetProcessHeap(),0,tls->time_buffer);
77     if(tls->have_locale) {
78         free_locinfo(tls->locinfo);
79         free_mbcinfo(tls->mbcinfo);
80     }
81   }
82   HeapFree(GetProcessHeap(), 0, tls);
83 }
84
85 /*********************************************************************
86  *                  Init
87  */
88 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
89 {
90   TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
91         hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
92         GetCurrentProcessId(), GetCurrentThreadId(),
93         msvcrt_tls_index);
94
95   switch (fdwReason)
96   {
97   case DLL_PROCESS_ATTACH:
98     if (!msvcrt_init_tls())
99       return FALSE;
100     msvcrt_init_mt_locks();
101     if(!msvcrt_init_locale()) {
102         msvcrt_free_mt_locks();
103         msvcrt_free_tls_mem();
104         return FALSE;
105     }
106     msvcrt_init_math();
107     msvcrt_init_io();
108     msvcrt_init_console();
109     msvcrt_init_args();
110     msvcrt_init_signals();
111     _setmbcp(_MB_CP_LOCALE);
112     /* don't allow unloading msvcrt, we can't setup file handles twice */
113     LdrAddRefDll( 0, hinstDLL );
114     TRACE("finished process init\n");
115     break;
116   case DLL_THREAD_ATTACH:
117     break;
118   case DLL_PROCESS_DETACH:
119     msvcrt_free_io();
120     msvcrt_free_mt_locks();
121     msvcrt_free_console();
122     msvcrt_free_args();
123     msvcrt_free_signals();
124     msvcrt_free_tls_mem();
125     if (!msvcrt_free_tls())
126       return FALSE;
127     MSVCRT__free_locale(MSVCRT_locale);
128     TRACE("finished process free\n");
129     break;
130   case DLL_THREAD_DETACH:
131     msvcrt_free_tls_mem();
132     TRACE("finished thread free\n");
133     break;
134   }
135   return TRUE;
136 }