source: trunk/nom/src/nomgc.c@ 242

Last change on this file since 242 was 195, checked in by cinc, 19 years ago

Class DLLs register themselves with the garbage collector now. Some clean ups.

File size: 9.6 KB
Line 
1/* ***** BEGIN LICENSE BLOCK *****
2* Version: CDDL 1.0/LGPL 2.1
3*
4* The contents of this file are subject to the COMMON DEVELOPMENT AND
5* DISTRIBUTION LICENSE (CDDL) Version 1.0 (the "License"); you may not use
6* this file except in compliance with the License. You may obtain a copy of
7* the License at http://www.sun.com/cddl/
8*
9* Software distributed under the License is distributed on an "AS IS" basis,
10* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11* for the specific language governing rights and limitations under the
12* License.
13*
14* The Original Code is "NOM" Netlabs Object Model
15*
16* The Initial Developer of the Original Code is
17* netlabs.org: Chris Wohlgemuth <cinc-ml@netlabs.org>.
18* Portions created by the Initial Developer are Copyright (C) 2005-2006
19* the Initial Developer. All Rights Reserved.
20*
21* Contributor(s):
22*
23* Alternatively, the contents of this file may be used under the terms of
24* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
25* case the provisions of the LGPL are applicable instead of those above. If
26* you wish to allow use of your version of this file only under the terms of
27* the LGPL, and not to allow others to use your version of this file under
28* the terms of the CDDL, indicate your decision by deleting the provisions
29* above and replace them with the notice and other provisions required by the
30* LGPL. If you do not delete the provisions above, a recipient may use your
31* version of this file under the terms of any one of the CDDL or the LGPL.
32*
33* ***** END LICENSE BLOCK ***** */
34
35/*
36 This file contains functions for working with the garbage collector.
37 */
38
39#define INCL_DOS
40#define INCL_DOSERRORS
41#define INCL_DOSMEMMGR
42#include <os2.h>
43
44#include <stdio.h>
45#include <string.h>
46#include <stdlib.h>
47
48/* For nomToken etc. */
49#include <nom.h>
50#include "nomtk.h"
51#include "nomgc.h"
52
53/* Garbage collector */
54#include <gc.h>
55
56gboolean bUseGC=FALSE; /* Mark if we use the garbage collector */
57GTree* treeRegisteredDLL;
58
59static gpointer gcMalloc(gulong ulBytes)
60{
61 //printf("Hi there...\n");
62 // return malloc(ulBytes);
63 return (gpointer) GC_malloc(ulBytes);
64}
65
66static gpointer gcRealloc(gpointer mem, gulong ulBytes)
67{
68 // printf("...and here\n");
69 // return realloc(mem, ulBytes);
70 return (gpointer) GC_realloc(mem, ulBytes);
71}
72
73static void gcFree(gpointer mem)
74{
75 // printf("free(): %x\n", mem);
76 return;
77 GC_free(mem);
78}
79
80
81/*
82 This is called from the EMX wrapper to set the garbage collector
83 memory functions as the GLIB default allocation function.
84 */
85void _System nomInitGarbageCollection(void* pMemInExe)
86{
87 GMemVTable vtbl={0};
88
89 /* Init the garbage collector */
90 GC_init();
91
92 vtbl.malloc=(gpointer)gcMalloc;
93 vtbl.realloc=(gpointer)gcRealloc;
94 vtbl.free=(gpointer)gcFree;
95
96 g_mem_set_vtable(&vtbl);
97 /* fprintf(stderr, " GC memory functions set for GLIB. (%s: %d)\n", __FILE__, __LINE__); */
98
99 /* Cretea tree holding the already registered DLLs */
100 treeRegisteredDLL=g_tree_new((GCompareFunc)stricmp);
101
102 bUseGC=TRUE;
103}
104
105
106void test()
107{
108 gulong ulObj, ulOffset;
109 gchar thePath[CCHMAXPATH];
110 HMODULE hModule;
111
112 if(DosQueryModFromEIP( &hModule, &ulObj, CCHMAXPATH, thePath, &ulOffset, (ULONG)test)!=0) {
113 hModule=0;
114 return NULLHANDLE; /* Error */
115 }
116
117}
118
119NOMEXTERN void NOMLINK nomRegisterDataAreaForGC(char* pStart, char* pEnd)
120{
121 GC_add_roots(pStart, pEnd);
122}
123
124static void qsAddDLLToList(HREGDLL hReg, qsLrec_t* rec)
125{
126 if(NULLHANDLE==g_slist_find(hReg->dllList, rec))
127 hReg->dllList=g_slist_append(hReg->dllList, rec);
128}
129
130#if 0
131static void qsPrintDLLList(HREGDLL hReg)
132{
133 GSList* lTemp;
134 int a=0;
135
136 lTemp=hReg->dllList;
137 while(lTemp)
138 {
139 qsLrec_t* rec;
140 rec=(qsLrec_t*)lTemp->data;
141 a++;
142 g_message(" %d: %s", a, rec->pName);
143 lTemp=g_slist_next(lTemp);
144 }
145}
146#endif
147
148/*
149 Find a library record in the buffer filled by DosQuerySysState().
150 */
151static qsLrec_t* qsFindModuleRec(const qsPtrRec_t * hRegisterDLL, USHORT hMod){
152 qsLrec_t * pModRec;
153 int a=0;
154
155 pModRec=hRegisterDLL->pLibRec;
156 while(NULL!=pModRec)
157 {
158 a++;
159 /* printf("%d Checking: %x -> %04X (%s)\n", a, pModRec, pModRec->hmte, pModRec->pName); */
160
161 if (NULLHANDLE==pModRec->pObjInfo && pModRec->ctObj > 0)
162 {
163 pModRec->pObjInfo = (qsLObjrec_t*)((char*)pModRec
164 + ((sizeof(qsLrec_t)
165 + pModRec->ctImpMod * sizeof(short)
166 + strlen((char*)pModRec->pName) + 1 /* filename */
167 + 3) & ~3));
168 pModRec->pNextRec = (void*)((char*)pModRec->pObjInfo
169 + sizeof(qsLObjrec_t) * pModRec->ctObj);
170 }
171 if(pModRec->hmte==hMod)
172 break;
173
174 pModRec=(qsLrec_t *)pModRec->pNextRec;
175 }
176
177 return pModRec;
178}
179
180#define BUFSIZE 1024*1024
181NOMEXTERN HREGDLL NOMLINK nomBeginRegisterDLLWithGC(void)
182{
183 ULONG rc;
184 HREGDLL hReg=NULLHANDLE;
185 PTIB ptib;
186 PPIB ppib;
187 char * buf;
188 HREGDLL pRegDLL=NULLHANDLE;
189
190 rc = DosGetInfoBlocks(&ptib, &ppib);
191 if (rc!=NO_ERROR)
192 return NULLHANDLE;
193
194 buf = malloc(BUFSIZE);
195 if(!buf)
196 return NULLHANDLE;
197
198 pRegDLL =(HREGDLL) malloc(sizeof(REGDLL));
199 if(!pRegDLL){
200 free(buf);
201 return NULLHANDLE;
202 }
203 pRegDLL->dllList=NULLHANDLE;
204
205 memset(buf,0,BUFSIZE);
206
207 rc = DosQuerySysState(QS_PROCESS | QS_SEMAPHORE | QS_MTE | QS_FILESYS | QS_SHMEMORY ,
208 QS_MTE, /*0x96*/ ppib->pib_ulpid , 1UL, (PCHAR)buf, BUFSIZE);
209 if (rc==NO_ERROR) {
210 qsPrec_t * p;
211 GSList* lTemp;
212
213 pRegDLL->pMainAnchor=(qsPtrRec_t*) buf;
214
215 p=pRegDLL->pMainAnchor->pProcRec;
216
217 while(p && p->RecType == 1)
218 {
219
220 if (p->cLib) {
221 int i;
222
223 for (i=0; i<p->cLib; i++){
224 qsLrec_t * pModRec;
225
226 pModRec=qsFindModuleRec(pRegDLL->pMainAnchor, p->pLibRec[i]);
227
228 if(pModRec){
229 //if(pModRec->pName)
230 //g_message("%s", pModRec->pName);
231 qsAddDLLToList(pRegDLL, pModRec);
232 }
233 }/* for() */
234 }/* if(p->clib) */
235 break;
236 };/* while() */
237
238 /* Ok, got directly imported DLLs. Now go over these and check them for additional imports.
239 Every import is added to the end of the list (except duplicates). So while going over
240 the list we touch every DLL and check every import. Import cycles are no problem, because
241 later duplicates are ignored. */
242 lTemp=pRegDLL->dllList;
243 while(lTemp)
244 {
245 qsLrec_t* rec;
246
247 rec=(qsLrec_t*)lTemp->data;
248
249 /* Check the imports of this DLL if any */
250 if(rec->ctImpMod >0)
251 {
252 int iImps;
253 PUSHORT pImpHmte;
254
255 pImpHmte=(PUSHORT)((void*)rec + sizeof(qsLrec_t));
256 for(iImps=0; iImps < rec->ctImpMod; iImps++)
257 {
258 qsLrec_t * pModImp;
259
260 pModImp=qsFindModuleRec(pRegDLL->pMainAnchor, pImpHmte[iImps]);
261 if(pModImp)
262 qsAddDLLToList(pRegDLL, pModImp);
263 }/* for()*/
264 }/* if() */
265 lTemp=g_slist_next(lTemp);
266 };/* while() */
267 //qsPrintDLLList();
268 hReg=pRegDLL;
269 }
270 else{
271 free(pRegDLL);
272 free(buf);
273 }
274 return hReg;
275}
276
277NOMEXTERN void NOMLINK nomEndRegisterDLLWithGC(const HREGDLL hRegisterDLL )
278{
279 g_slist_free(hRegisterDLL->dllList);
280 free((char*)hRegisterDLL->pMainAnchor);
281 free((char*)hRegisterDLL);
282}
283
284#define OBJREAD 0x0001L
285#define OBJWRITE 0x0002L
286#define OBJINVALID 0x0080L
287NOMEXTERN BOOL NOMLINK nomRegisterDLLByName(const HREGDLL hRegisterDLL, const char* chrDLLName)
288{
289 GSList* lTemp;
290
291 //g_message("Trying to register DLL %s\n", chrDLLName);
292 lTemp=hRegisterDLL->dllList;
293 while(lTemp)
294 {
295 qsLrec_t* pModRec;
296
297 pModRec=(qsLrec_t*)lTemp->data;
298 if(pModRec){
299 //g_message("DLL name: %s\n", pModRec->pName);
300 if(pModRec->pName && (NULLHANDLE!=strstr( pModRec->pName, chrDLLName)))
301 {
302 qsLObjrec_t *pObjInfo;
303 //g_message(" --> Found DLL %s", pModRec->pName);
304 pObjInfo=pModRec->pObjInfo;
305 if(NULLHANDLE!=pObjInfo)
306 {
307 int iObj;
308 for(iObj=0; iObj<pModRec->ctObj ;iObj++)
309 {
310 if (!(pObjInfo[iObj].oflags & OBJWRITE)) continue;
311 if (!(pObjInfo[iObj].oflags & OBJREAD)) continue;
312 if ((pObjInfo[iObj].oflags & OBJINVALID)) continue;
313 //g_message(" #%d: %04lX, size: %04lX %04lX",
314 // iObj, pObjInfo[iObj].oaddr, pObjInfo[iObj].osize, pObjInfo[iObj].oflags);
315 nomRegisterDataAreaForGC((char*)pObjInfo[iObj].oaddr,
316 (char*)(pObjInfo[iObj].oaddr+pObjInfo[iObj].osize));
317 g_tree_insert(treeRegisteredDLL, g_strdup(chrDLLName), GUINT_TO_POINTER((guint)pModRec->hmte));
318 }
319 }
320 return TRUE;
321 }
322 }
323 lTemp=g_slist_next(lTemp);
324 };/* while() */
325 return FALSE;
326}
327
328
329NOMEXTERN BOOL NOMLINK nomQueryUsingNameIsDLLRegistered(const gchar *chrName)
330{
331 if(NULLHANDLE!=g_tree_lookup(treeRegisteredDLL, chrName))
332 return TRUE;
333
334 return FALSE;
335}
Note: See TracBrowser for help on using the repository browser.