| 1 | /* $Id: initterm.cpp,v 1.28 1999-11-23 20:01:18 sandervl Exp $ */
|
|---|
| 2 |
|
|---|
| 3 | /*
|
|---|
| 4 | * KERNEL32 DLL entry point
|
|---|
| 5 | *
|
|---|
| 6 | * Copyright 1998 Sander van Leeuwen
|
|---|
| 7 | * Copyright 1998 Peter Fitzsimmons
|
|---|
| 8 | *
|
|---|
| 9 | *
|
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT
|
|---|
| 11 | *
|
|---|
| 12 | */
|
|---|
| 13 |
|
|---|
| 14 | /*-------------------------------------------------------------*/
|
|---|
| 15 | /* INITERM.C -- Source for a custom dynamic link library */
|
|---|
| 16 | /* initialization and termination (_DLL_InitTerm) */
|
|---|
| 17 | /* function. */
|
|---|
| 18 | /* */
|
|---|
| 19 | /* When called to perform initialization, this sample function */
|
|---|
| 20 | /* gets storage for an array of integers, and initializes its */
|
|---|
| 21 | /* elements with random integers. At termination time, it */
|
|---|
| 22 | /* frees the array. Substitute your own special processing. */
|
|---|
| 23 | /*-------------------------------------------------------------*/
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 | /* Include files */
|
|---|
| 27 | #define INCL_DOSMODULEMGR
|
|---|
| 28 | #define INCL_DOSMISC
|
|---|
| 29 | #define INCL_DOSPROCESS
|
|---|
| 30 | #define INCL_DOSSEMAPHORES
|
|---|
| 31 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 32 | #include <stdlib.h>
|
|---|
| 33 | #include <stdio.h>
|
|---|
| 34 | #include <string.h>
|
|---|
| 35 | #include <misc.h>
|
|---|
| 36 | #include <wprocess.h>
|
|---|
| 37 | #include "handlemanager.h"
|
|---|
| 38 | #include "profile.h"
|
|---|
| 39 | #include "initterm.h"
|
|---|
| 40 | #include <win32type.h>
|
|---|
| 41 | #include <odinlx.h>
|
|---|
| 42 | #include "oslibmisc.h"
|
|---|
| 43 | #include <heapshared.h>
|
|---|
| 44 | #include "mmap.h"
|
|---|
| 45 | #include "directory.h"
|
|---|
| 46 | #include "hmdevio.h"
|
|---|
| 47 | #include <windllbase.h>
|
|---|
| 48 | #include "cpuhlp.h"
|
|---|
| 49 |
|
|---|
| 50 | /*-------------------------------------------------------------------*/
|
|---|
| 51 | /* A clean up routine registered with DosExitList must be used if */
|
|---|
| 52 | /* runtime calls are required and the runtime is dynamically linked. */
|
|---|
| 53 | /* This will guarantee that this clean up routine is run before the */
|
|---|
| 54 | /* library DLL is terminated. */
|
|---|
| 55 | /*-------------------------------------------------------------------*/
|
|---|
| 56 | static void APIENTRY cleanup(ULONG reason);
|
|---|
| 57 | extern void APIENTRY Win32DllExitList(ULONG reason);
|
|---|
| 58 |
|
|---|
| 59 | extern "C" {
|
|---|
| 60 | void CDECL _ctordtorInit( void );
|
|---|
| 61 | void CDECL _ctordtorTerm( void );
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | //Global DLL Data
|
|---|
| 65 | #pragma data_seg(_GLOBALDATA)
|
|---|
| 66 | int globLoadNr = 0;
|
|---|
| 67 | #pragma data_seg()
|
|---|
| 68 |
|
|---|
| 69 | /* Tue 03.03.1998: knut */
|
|---|
| 70 | ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
|
|---|
| 71 | ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
|
|---|
| 72 | int loadNr = 0;
|
|---|
| 73 | char kernel32Path[CCHMAXPATH] = "";
|
|---|
| 74 |
|
|---|
| 75 | /****************************************************************************/
|
|---|
| 76 | /* _DLL_InitTerm is the function that gets called by the operating system */
|
|---|
| 77 | /* loader when it loads and frees this DLL for each process that accesses */
|
|---|
| 78 | /* this DLL. However, it only gets called the first time the DLL is loaded */
|
|---|
| 79 | /* and the last time it is freed for a particular process. The system */
|
|---|
| 80 | /* linkage convention MUST be used because the operating system loader is */
|
|---|
| 81 | /* calling this function. */
|
|---|
| 82 | /****************************************************************************/
|
|---|
| 83 | unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
|
|---|
| 84 | ulFlag)
|
|---|
| 85 | {
|
|---|
| 86 | size_t i;
|
|---|
| 87 | APIRET rc;
|
|---|
| 88 | ULONG ulSysinfo;
|
|---|
| 89 |
|
|---|
| 90 | /*-------------------------------------------------------------------------*/
|
|---|
| 91 | /* If ulFlag is zero then the DLL is being loaded so initialization should */
|
|---|
| 92 | /* be performed. If ulFlag is 1 then the DLL is being freed so */
|
|---|
| 93 | /* termination should be performed. */
|
|---|
| 94 | /*-------------------------------------------------------------------------*/
|
|---|
| 95 |
|
|---|
| 96 | switch (ulFlag)
|
|---|
| 97 | {
|
|---|
| 98 | case 0 :
|
|---|
| 99 | {
|
|---|
| 100 | loadNr = globLoadNr++;
|
|---|
| 101 |
|
|---|
| 102 | strcpy(kernel32Path, OSLibGetDllName(hModule));
|
|---|
| 103 | char *endofpath = strrchr(kernel32Path, '\\');
|
|---|
| 104 | *(endofpath+1) = 0;
|
|---|
| 105 | dprintf(("kernel32 init\n"));
|
|---|
| 106 | _ctordtorInit();
|
|---|
| 107 |
|
|---|
| 108 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
|
|---|
| 109 |
|
|---|
| 110 | if(InitializeSharedHeap() == FALSE)
|
|---|
| 111 | return 0UL;
|
|---|
| 112 |
|
|---|
| 113 | PROFILE_LoadOdinIni();
|
|---|
| 114 | if(RegisterLxDll(hModule, 0, 0) == FALSE)
|
|---|
| 115 | return 0UL;
|
|---|
| 116 |
|
|---|
| 117 | /*******************************************************************/
|
|---|
| 118 | /* A DosExitList routine must be used to clean up if runtime calls */
|
|---|
| 119 | /* are required and the runtime is dynamically linked. */
|
|---|
| 120 | /*******************************************************************/
|
|---|
| 121 |
|
|---|
| 122 | rc = DosExitList(0x0000F000|EXLST_ADD, cleanup);
|
|---|
| 123 | if (rc)
|
|---|
| 124 | return 0UL;
|
|---|
| 125 |
|
|---|
| 126 | /* knut: check for high memory support */
|
|---|
| 127 | rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
|
|---|
| 128 | if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
|
|---|
| 129 | {
|
|---|
| 130 | flAllocMem = PAG_ANY; // high memory support. Let's use it!
|
|---|
| 131 | ulMaxAddr = ulSysinfo * (1024*1024);
|
|---|
| 132 | }
|
|---|
| 133 | else
|
|---|
| 134 | flAllocMem = 0; // no high memory support
|
|---|
| 135 |
|
|---|
| 136 | InitializeTIB(TRUE);
|
|---|
| 137 | //SvL: Do it here instead of during the exe object creation
|
|---|
| 138 | //(std handles can be used in win32 dll initialization routines
|
|---|
| 139 | HMInitialize(); /* store standard handles within HandleManager */
|
|---|
| 140 | InitDirectories();
|
|---|
| 141 | RegisterDevices();
|
|---|
| 142 | Win32DllBase::setDefaultRenaming();
|
|---|
| 143 | rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
|
|---|
| 144 | if (rc != 0)
|
|---|
| 145 | ulSysinfo = 1;
|
|---|
| 146 | InitSystemInfo(ulSysinfo);
|
|---|
| 147 |
|
|---|
| 148 | break;
|
|---|
| 149 | }
|
|---|
| 150 | case 1 :
|
|---|
| 151 | UnregisterLxDll(hModule);
|
|---|
| 152 | break;
|
|---|
| 153 | default :
|
|---|
| 154 | return 0UL;
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | /***********************************************************/
|
|---|
| 158 | /* A non-zero value must be returned to indicate success. */
|
|---|
| 159 | /***********************************************************/
|
|---|
| 160 | return 1UL;
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 |
|
|---|
| 164 | static void APIENTRY cleanup(ULONG ulReason)
|
|---|
| 165 | {
|
|---|
| 166 | dprintf(("kernel32 exit %d\n", ulReason));
|
|---|
| 167 | //Flush and delete all open memory mapped files
|
|---|
| 168 | Win32MemMap::deleteAll();
|
|---|
| 169 |
|
|---|
| 170 | WriteOutProfiles();
|
|---|
| 171 | DestroyTIB();
|
|---|
| 172 | DestroySharedHeap();
|
|---|
| 173 | _ctordtorTerm();
|
|---|
| 174 |
|
|---|
| 175 | //NOTE: Must be done after DestroyTIB
|
|---|
| 176 | CloseLogFile();
|
|---|
| 177 |
|
|---|
| 178 | DosExitList(EXLST_EXIT, cleanup);
|
|---|
| 179 | return ;
|
|---|
| 180 | }
|
|---|