source: trunk/src/kernel32/initterm.cpp@ 3642

Last change on this file since 3642 was 3642, checked in by sandervl, 25 years ago

Rewrote file io apis

File size: 7.0 KB
Line 
1/* $Id: initterm.cpp,v 1.42 2000-06-01 11:28:47 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 <heapcode.h>
45#include "mmap.h"
46#include "directory.h"
47#include "hmdevio.h"
48#include <windllbase.h>
49#include "winexepe2lx.h"
50#include <exitlist.h>
51#include "oslibdos.h"
52#include <cpuhlp.h>
53
54#define DBG_LOCALLOG DBG_initterm
55#include "dbglocal.h"
56
57/*-------------------------------------------------------------------*/
58/* A clean up routine registered with DosExitList must be used if */
59/* runtime calls are required and the runtime is dynamically linked. */
60/* This will guarantee that this clean up routine is run before the */
61/* library DLL is terminated. */
62/*-------------------------------------------------------------------*/
63static void APIENTRY cleanup(ULONG reason);
64
65extern "C" {
66void CDECL _ctordtorInit( void );
67void CDECL _ctordtorTerm( void );
68
69 //Win32 resource table (produced by wrc)
70 extern DWORD _Resource_PEResTab;
71}
72
73//Global DLL Data
74#pragma data_seg(_GLOBALDATA)
75int globLoadNr = 0;
76#pragma data_seg()
77
78/* Tue 03.03.1998: knut */
79ULONG flAllocMem = 0; /* flag to optimize DosAllocMem to use all the memory on SMP machines */
80ULONG ulMaxAddr = 0x20000000; /* end of user address space. */
81int loadNr = 0;
82char kernel32Path[CCHMAXPATH] = "";
83
84/****************************************************************************/
85/* _DLL_InitTerm is the function that gets called by the operating system */
86/* loader when it loads and frees this DLL for each process that accesses */
87/* this DLL. However, it only gets called the first time the DLL is loaded */
88/* and the last time it is freed for a particular process. The system */
89/* linkage convention MUST be used because the operating system loader is */
90/* calling this function. */
91/****************************************************************************/
92unsigned long SYSTEM _DLL_InitTerm(unsigned long hModule, unsigned long
93 ulFlag)
94{
95 size_t i;
96 APIRET rc;
97 ULONG ulSysinfo;
98
99 /*-------------------------------------------------------------------------*/
100 /* If ulFlag is zero then the DLL is being loaded so initialization should */
101 /* be performed. If ulFlag is 1 then the DLL is being freed so */
102 /* termination should be performed. */
103 /*-------------------------------------------------------------------------*/
104
105 switch (ulFlag)
106 {
107 case 0 :
108 {
109 ParseLogStatus();
110
111 loadNr = globLoadNr++;
112
113 strcpy(kernel32Path, OSLibGetDllName(hModule));
114 char *endofpath = strrchr(kernel32Path, '\\');
115 *(endofpath+1) = 0;
116 dprintf(("kernel32 init\n"));
117 _ctordtorInit();
118
119 CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed 98-03-18 05:28:48*/
120
121 OpenPrivateLogFiles();
122
123 if(InitializeSharedHeap() == FALSE)
124 return 0UL;
125
126 if(InitializeCodeHeap() == FALSE)
127 return 0UL;
128
129 PROFILE_LoadOdinIni();
130 if(RegisterLxDll(hModule, 0, (PVOID)&_Resource_PEResTab) == FALSE)
131 return 0UL;
132
133 //SvL: Kernel32 is a special case; pe.exe loads it, so increase
134 // the reference count here
135 Win32DllBase *module = Win32DllBase::findModule(hModule);
136 if(module) {
137 module->AddRef();
138 module->DisableUnload();
139 }
140
141 /*******************************************************************/
142 /* A DosExitList routine must be used to clean up if runtime calls */
143 /* are required and the runtime is dynamically linked. */
144 /*******************************************************************/
145
146 rc = DosExitList(EXITLIST_KERNEL32|EXLST_ADD, cleanup);
147 if (rc)
148 return 0UL;
149
150 /* knut: check for high memory support */
151 rc = DosQuerySysInfo(QSV_VIRTUALADDRESSLIMIT, QSV_VIRTUALADDRESSLIMIT, &ulSysinfo, sizeof(ulSysinfo));
152 if (rc == 0 && ulSysinfo > 512) //VirtualAddresslimit is in MB
153 {
154 flAllocMem = PAG_ANY; // high memory support. Let's use it!
155 ulMaxAddr = ulSysinfo * (1024*1024);
156 OSLibInitWSeBFileIO();
157 }
158 else
159 flAllocMem = 0; // no high memory support
160
161 OSLibDosSetInitialMaxFileHandles(ODIN_DEFAULT_MAX_FILEHANDLES);
162
163 //SvL: Do it here instead of during the exe object creation
164 //(std handles can be used in win32 dll initialization routines
165 HMInitialize(); /* store standard handles within HandleManager */
166 InitializeTIB(TRUE); //MUST be done after HMInitialize!
167 InitDirectories();
168 RegisterDevices();
169 Win32DllBase::setDefaultRenaming();
170 rc = DosQuerySysInfo(QSV_NUMPROCESSORS, QSV_NUMPROCESSORS, &ulSysinfo, sizeof(ulSysinfo));
171 if (rc != 0)
172 ulSysinfo = 1;
173
174 InitSystemInfo(ulSysinfo);
175 break;
176 }
177 case 1 :
178 UnregisterLxDll(hModule);
179 break;
180 default :
181 return 0UL;
182 }
183
184 /***********************************************************/
185 /* A non-zero value must be returned to indicate success. */
186 /***********************************************************/
187 return 1UL;
188}
189
190
191static void APIENTRY cleanup(ULONG ulReason)
192{
193 dprintf(("kernel32 exit %d\n", ulReason));
194 //Flush and delete all open memory mapped files
195 Win32MemMap::deleteAll();
196 WinExe = NULL;
197
198 WriteOutProfiles();
199 DestroyTIB();
200 DestroySharedHeap();
201 DestroyCodeHeap();
202 _ctordtorTerm();
203
204 //NOTE: Must be done after DestroyTIB
205 ClosePrivateLogFiles();
206 CloseLogFile();
207
208 DosExitList(EXLST_EXIT, cleanup);
209 return ;
210}
Note: See TracBrowser for help on using the repository browser.