main.c 3.84 KB
Newer Older
1 2 3 4
/*
 * msvcrt.dll initialisation functions
 *
 * Copyright 2000 Jon Griffiths
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 24 25
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(msvcrt);
26 27

/* Index to TLS */
28
DWORD msvcrt_tls_index;
29

30 31 32 33 34 35 36 37 38 39 40
static const char* msvcrt_get_reason(DWORD reason)
{
  switch (reason)
  {
  case DLL_PROCESS_ATTACH: return "DLL_PROCESS_ATTACH";
  case DLL_PROCESS_DETACH: return "DLL_PROCESS_DETACH";
  case DLL_THREAD_ATTACH:  return "DLL_THREAD_ATTACH";
  case DLL_THREAD_DETACH:  return "DLL_THREAD_DETACH";
  }
  return "UNKNOWN";
}
41

42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
static inline BOOL msvcrt_init_tls(void)
{
  msvcrt_tls_index = TlsAlloc();

  if (msvcrt_tls_index == TLS_OUT_OF_INDEXES)
  {
    ERR("TlsAlloc() failed!\n");
    return FALSE;
  }
  return TRUE;
}

static inline BOOL msvcrt_free_tls(void)
{
  if (!TlsFree(msvcrt_tls_index))
  {
    ERR("TlsFree() failed!\n");
    return FALSE;
  }
  return TRUE;
}

64 65 66
static inline void msvcrt_free_tls_mem(void)
{
  thread_data_t *tls = TlsGetValue(msvcrt_tls_index);
67

68 69
  if (tls)
  {
70 71 72 73 74 75 76 77
    MSVCRT_free(tls->efcvt_buffer);
    MSVCRT_free(tls->asctime_buffer);
    MSVCRT_free(tls->wasctime_buffer);
    MSVCRT_free(tls->strerror_buffer);
    MSVCRT_free(tls->wcserror_buffer);
    MSVCRT_free(tls->time_buffer);
    MSVCRT_free(tls->tmpnam_buffer);
    MSVCRT_free(tls->wtmpnam_buffer);
78 79 80 81
    if(tls->have_locale) {
        free_locinfo(tls->locinfo);
        free_mbcinfo(tls->mbcinfo);
    }
82 83 84 85
  }
  HeapFree(GetProcessHeap(), 0, tls);
}

86 87 88
/*********************************************************************
 *                  Init
 */
89
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
90
{
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);
95 96 97 98

  switch (fdwReason)
  {
  case DLL_PROCESS_ATTACH:
99
    msvcrt_init_exception(hinstDLL);
100 101 102 103
    if(!msvcrt_init_heap())
        return FALSE;
    if(!msvcrt_init_tls()) {
      msvcrt_destroy_heap();
104
      return FALSE;
105
    }
106
    msvcrt_init_mt_locks();
107
    if(!msvcrt_init_locale()) {
108
        msvcrt_free_locks();
109
        msvcrt_free_tls_mem();
110
        msvcrt_destroy_heap();
111 112
        return FALSE;
    }
113
    msvcrt_init_math();
114 115 116
    msvcrt_init_io();
    msvcrt_init_console();
    msvcrt_init_args();
Juan Lang's avatar
Juan Lang committed
117
    msvcrt_init_signals();
118
#if _MSVCR_VER == 0
119
    /* don't allow unloading msvcrt, we can't setup file handles twice */
120
    LdrAddRefDll( LDR_ADDREF_DLL_PIN, hinstDLL );
121 122 123
#elif _MSVCR_VER >= 80
    MSVCRT__set_printf_count_output(0);
#endif
124
    TRACE("finished process init\n");
125
    break;
126 127 128
  case DLL_THREAD_ATTACH:
    break;
  case DLL_PROCESS_DETACH:
129
    msvcrt_free_io();
130 131
    if (lpvReserved) break;
    msvcrt_free_popen_data();
132
    msvcrt_free_locks();
133 134
    msvcrt_free_console();
    msvcrt_free_args();
Juan Lang's avatar
Juan Lang committed
135
    msvcrt_free_signals();
136
    msvcrt_free_tls_mem();
137
    if (!msvcrt_free_tls())
138
      return FALSE;
139
    MSVCRT__free_locale(MSVCRT_locale);
140
    msvcrt_destroy_heap();
141 142 143
    TRACE("finished process free\n");
    break;
  case DLL_THREAD_DETACH:
144
    msvcrt_free_tls_mem();
145 146 147 148 149
    TRACE("finished thread free\n");
    break;
  }
  return TRUE;
}