wined3d: Set the volume.c GLINFO_LOCATION to *gl_info.
[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
22 #include "wine/debug.h"
23
24 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
25
26 /* Index to TLS */
27 DWORD msvcrt_tls_index;
28
29 static const char* msvcrt_get_reason(DWORD reason)
30 {
31   switch (reason)
32   {
33   case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
34   case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
35   case DLL_THREAD_ATTACH:  return "DLL_THREAD_ATTACH";
36   case DLL_THREAD_DETACH:  return "DLL_THREAD_DETACH";
37   }
38   return "UNKNOWN";
39 }
40
41 static inline BOOL msvcrt_init_tls(void)
42 {
43   msvcrt_tls_index = TlsAlloc();
44
45   if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
46   {
47     ERR("TlsAlloc() failed!\n");
48     return FALSE;
49   }
50   return TRUE;
51 }
52
53 static inline BOOL msvcrt_free_tls(void)
54 {
55   if (!TlsFree(msvcrt_tls_index))
56   {
57     ERR("TlsFree() failed!\n");
58     return FALSE;
59   }
60   return TRUE;
61 }
62
63 static inline void msvcrt_free_tls_mem(void)
64 {
65   thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
66   if (tls)
67   {
68     HeapFree(GetProcessHeap(),0,tls->efcvt_buffer);
69     HeapFree(GetProcessHeap(),0,tls->asctime_buffer);
70     HeapFree(GetProcessHeap(),0,tls->wasctime_buffer);
71     HeapFree(GetProcessHeap(),0,tls->strerror_buffer);
72     MSVCRT__free_locale(tls->locale);
73   }
74   HeapFree(GetProcessHeap(), 0, tls);
75 }
76
77 /*********************************************************************
78  *                  Init
79  */
80 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
81 {
82   TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
83         hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
84         GetCurrentProcessId(), GetCurrentThreadId(),
85         msvcrt_tls_index);
86
87   switch (fdwReason)
88   {
89   case DLL_PROCESS_ATTACH:
90     if (!msvcrt_init_tls())
91       return FALSE;
92     msvcrt_init_mt_locks();
93     if(!MSVCRT_setlocale(0, "C")) {
94         msvcrt_free_mt_locks();
95         msvcrt_free_tls_mem();
96         return FALSE;
97     }
98     msvcrt_init_io();
99     msvcrt_init_console();
100     msvcrt_init_args();
101     msvcrt_init_signals();
102     _setmbcp(_MB_CP_LOCALE);
103     TRACE("finished process init\n");
104     break;
105   case DLL_THREAD_ATTACH:
106     break;
107   case DLL_PROCESS_DETACH:
108     msvcrt_free_mt_locks();
109     msvcrt_free_io();
110     msvcrt_free_console();
111     msvcrt_free_args();
112     msvcrt_free_signals();
113     msvcrt_free_tls_mem();
114     if (!msvcrt_free_tls())
115       return FALSE;
116     MSVCRT__free_locale(MSVCRT_locale);
117     TRACE("finished process free\n");
118     break;
119   case DLL_THREAD_DETACH:
120     msvcrt_free_tls_mem();
121     TRACE("finished thread free\n");
122     break;
123   }
124   return TRUE;
125 }