wined3d: Merge wined3d_surface_upload_data() into texture2d_upload_data().
[wine.git] / dlls / msvcrt / main.c
blob0be8dd36b5f90981a24ea3bb7c9d9c2ed571a8c6
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 MSVCRT_free(tls->efcvt_buffer);
71 MSVCRT_free(tls->asctime_buffer);
72 MSVCRT_free(tls->wasctime_buffer);
73 MSVCRT_free(tls->strerror_buffer);
74 MSVCRT_free(tls->wcserror_buffer);
75 MSVCRT_free(tls->time_buffer);
76 MSVCRT_free(tls->tmpnam_buffer);
77 MSVCRT_free(tls->wtmpnam_buffer);
78 if(tls->have_locale) {
79 free_locinfo(tls->locinfo);
80 free_mbcinfo(tls->mbcinfo);
83 HeapFree(GetProcessHeap(), 0, tls);
86 /*********************************************************************
87 * Init
89 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
91 TRACE("(%p, %s, %p) pid(%x), tid(%x), tls(%u)\n",
92 hinstDLL, msvcrt_get_reason(fdwReason), lpvReserved,
93 GetCurrentProcessId(), GetCurrentThreadId(),
94 msvcrt_tls_index);
96 switch (fdwReason)
98 case DLL_PROCESS_ATTACH:
99 msvcrt_init_exception(hinstDLL);
100 if(!msvcrt_init_heap())
101 return FALSE;
102 if(!msvcrt_init_tls()) {
103 msvcrt_destroy_heap();
104 return FALSE;
106 msvcrt_init_mt_locks();
107 if(!msvcrt_init_locale()) {
108 msvcrt_free_locks();
109 msvcrt_free_tls_mem();
110 msvcrt_destroy_heap();
111 return FALSE;
113 msvcrt_init_math();
114 msvcrt_init_io();
115 msvcrt_init_console();
116 msvcrt_init_args();
117 msvcrt_init_signals();
118 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
119 msvcrt_init_scheduler(hinstDLL);
120 #endif
121 #if _MSVCR_VER == 0
122 /* don't allow unloading msvcrt, we can't setup file handles twice */
123 LdrAddRefDll( LDR_ADDREF_DLL_PIN, hinstDLL );
124 #elif _MSVCR_VER >= 80
125 MSVCRT__set_printf_count_output(0);
126 #endif
127 TRACE("finished process init\n");
128 break;
129 case DLL_THREAD_ATTACH:
130 break;
131 case DLL_PROCESS_DETACH:
132 msvcrt_free_io();
133 if (lpvReserved) break;
134 msvcrt_free_popen_data();
135 msvcrt_free_locks();
136 msvcrt_free_console();
137 msvcrt_free_args();
138 msvcrt_free_signals();
139 msvcrt_free_tls_mem();
140 if (!msvcrt_free_tls())
141 return FALSE;
142 MSVCRT__free_locale(MSVCRT_locale);
143 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
144 msvcrt_free_scheduler_thread();
145 msvcrt_free_scheduler();
146 #endif
147 msvcrt_destroy_heap();
148 TRACE("finished process free\n");
149 break;
150 case DLL_THREAD_DETACH:
151 msvcrt_free_tls_mem();
152 #if _MSVCR_VER >= 100 && _MSVCR_VER <= 120
153 msvcrt_free_scheduler_thread();
154 #endif
155 TRACE("finished thread free\n");
156 break;
158 return TRUE;