Added heap initialization for malloc/calloc/realloc/free.

Added debug messages.
Removed fixed count for environment.

svn path=/trunk/; revision=2800
This commit is contained in:
Hartmut Birr 2002-04-01 21:55:09 +00:00
parent e129158330
commit 0ea281268b

View file

@ -1,4 +1,4 @@
/* $Id: dllmain.c,v 1.10 2001/07/29 16:42:35 ekohl Exp $ /* $Id: dllmain.c,v 1.11 2002/04/01 21:55:09 hbirr Exp $
* *
* ReactOS MSVCRT.DLL Compatibility Library * ReactOS MSVCRT.DLL Compatibility Library
*/ */
@ -7,6 +7,9 @@
#include <msvcrt/internal/tls.h> #include <msvcrt/internal/tls.h>
#include <msvcrt/stdlib.h> #include <msvcrt/stdlib.h>
#define NDEBUG
#include <msvcrt/msvcrtdbg.h>
static int nAttachCount = 0; static int nAttachCount = 0;
unsigned int _osver = 0; unsigned int _osver = 0;
@ -27,36 +30,48 @@ int __app_type = 0; //_UNKNOWN_APP; /* application type */
int __mb_cur_max = 1; int __mb_cur_max = 1;
static int envAlloced = 0; HANDLE hHeap = NULL; /* handle for heap */
/* FUNCTIONS **************************************************************/ /* FUNCTIONS **************************************************************/
int BlockEnvToEnviron() int BlockEnvToEnviron()
{ {
char * ptr; char * ptr, * ptr2;
int i; int i, len;
if (!envAlloced) DPRINT("BlockEnvToEnviron()\n");
if (_environ)
{ {
envAlloced = 50; FreeEnvironmentStringsA(_environ[0]);
_environ = malloc (envAlloced * sizeof (char **)); free(_environ);
if (_environ == NULL) _environ = NULL;
return -1;
_environ[0] = NULL;
} }
ptr = (char *)GetEnvironmentStringsA(); ptr2 = ptr = (char*)GetEnvironmentStringsA();
if (!ptr) if (ptr == NULL)
{
DPRINT("GetEnvironmentStringsA() returnd NULL\n");
return -1; return -1;
}
for (i = 0 ; *ptr && (i < envAlloced) ; i++) len = 0;
while (*ptr2)
{
len++;
while (*ptr2++);
}
_environ = malloc((len + 1) * sizeof(char*));
if (_environ == NULL)
{
FreeEnvironmentStringsA(ptr);
return -1;
}
for (i = 0; i < len && *ptr; i++)
{ {
_environ[i] = ptr; _environ[i] = ptr;
while(*ptr) ptr++; while (*ptr++);
ptr++;
} }
_environ[i] = 0; _environ[i] = NULL;
return 0; return 0;
} }
@ -69,12 +84,22 @@ DllMain(PVOID hinstDll,
{ {
case DLL_PROCESS_ATTACH://1 case DLL_PROCESS_ATTACH://1
/* initialize version info */ /* initialize version info */
DPRINT("Attach %d\n", nAttachCount);
_osver = GetVersion(); _osver = GetVersion();
_winmajor = (_osver >> 8) & 0xFF; _winmajor = (_osver >> 8) & 0xFF;
_winminor = _osver & 0xFF; _winminor = _osver & 0xFF;
_winver = (_winmajor << 8) + _winminor; _winver = (_winmajor << 8) + _winminor;
_osver = (_osver >> 16) & 0xFFFF; _osver = (_osver >> 16) & 0xFFFF;
if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
{
hHeap = HeapCreate(0, 0, 0);
if (hHeap == NULL || hHeap == INVALID_HANDLE_VALUE)
{
return FALSE;
}
}
/* create tls stuff */ /* create tls stuff */
if (!CreateThreadData()) if (!CreateThreadData())
return FALSE; return FALSE;
@ -98,6 +123,7 @@ DllMain(PVOID hinstDll,
break; break;
case DLL_PROCESS_DETACH://0 case DLL_PROCESS_DETACH://0
DPRINT("Detach %d\n", nAttachCount);
if (nAttachCount > 0) if (nAttachCount > 0)
{ {
nAttachCount--; nAttachCount--;
@ -106,6 +132,22 @@ DllMain(PVOID hinstDll,
/* destroy tls stuff */ /* destroy tls stuff */
DestroyThreadData(); DestroyThreadData();
/* destroy heap */
if (nAttachCount == 0)
{
if (_environ)
{
FreeEnvironmentStringsA(_environ[0]);
free(_environ);
_environ = NULL;
}
#if 1
HeapDestroy(hHeap);
hHeap = NULL;
#endif
}
} }
break; break;
} }