wined3d: Use a separate STATE_TRANSFORM(WINED3D_TS_VIEW) state handler in the GLSL...
[wine/multimedia.git] / dlls / msvcrt / main.c
blob1deb0804858ded56cb6a0f98627fc5ded6d75048
1 /*
2 * msvcrt.dll initialisation functions
4 * Copyright 2000 Jon Griffiths
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.
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.
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
20 #include "msvcrt.h"
21 #include "winternl.h"
23 #include "wine/debug.h"
25 WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
27 /* Index to TLS */
28 DWORD msvcrt_tls_index;
30 static const char* msvcrt_get_reason(DWORD reason)
32 switch (reason)
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";
39 return "UNKNOWN";
42 static inline BOOL msvcrt_init_tls(void)
44 msvcrt_tls_index = TlsAlloc();
46 if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
48 ERR("TlsAlloc() failed!\n");
49 return FALSE;
51 return TRUE;
54 static inline BOOL msvcrt_free_tls(void)
56 if (!TlsFree(msvcrt_tls_index))
58 ERR("TlsFree() failed!\n");
59 return FALSE;
61 return TRUE;
64 static inline void msvcrt_free_tls_mem(void)
66 thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
68 if (tls)
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 HeapFree(GetProcessHeap(),0,tls->tmpnam_buffer);
78 HeapFree(GetProcessHeap(),0,tls->wtmpnam_buffer);
79 if(tls->have_locale) {
80 free_locinfo(tls->locinfo);
81 free_mbcinfo(tls->mbcinfo);
84 HeapFree(GetProcessHeap(), 0, tls);
87 /*********************************************************************
88 * Init
90 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
92 TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
93 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
94 GetCurrentProcessId(), GetCurrentThreadId(),
95 msvcrt_tls_index);
97 switch (fdwReason)
99 case DLL_PROCESS_ATTACH:
100 msvcrt_init_exception(hinstDLL);
101 if(!msvcrt_init_heap())
102 return FALSE;
103 if(!msvcrt_init_tls()) {
104 msvcrt_destroy_heap();
105 return FALSE;
107 msvcrt_init_mt_locks();
108 if(!msvcrt_init_locale()) {
109 msvcrt_free_locks();
110 msvcrt_free_tls_mem();
111 msvcrt_destroy_heap();
112 return FALSE;
114 msvcrt_init_math();
115 msvcrt_init_io();
116 msvcrt_init_console();
117 msvcrt_init_args();
118 msvcrt_init_signals();
119 #if _MSVCR_VER == 0
120 /* don't allow unloading msvcrt, we can't setup file handles twice */
121 LdrAddRefDll( LDR_ADDREF_DLL_PIN, hinstDLL );
122 #elif _MSVCR_VER >= 80
123 MSVCRT__set_printf_count_output(0);
124 #endif
125 TRACE("finished process init\n");
126 break;
127 case DLL_THREAD_ATTACH:
128 break;
129 case DLL_PROCESS_DETACH:
130 msvcrt_free_io();
131 if (lpvReserved) break;
132 msvcrt_free_popen_data();
133 msvcrt_free_locks();
134 msvcrt_free_console();
135 msvcrt_free_args();
136 msvcrt_free_signals();
137 msvcrt_free_tls_mem();
138 if (!msvcrt_free_tls())
139 return FALSE;
140 MSVCRT__free_locale(MSVCRT_locale);
141 msvcrt_destroy_heap();
142 TRACE("finished process free\n");
143 break;
144 case DLL_THREAD_DETACH:
145 msvcrt_free_tls_mem();
146 TRACE("finished thread free\n");
147 break;
149 return TRUE;