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

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

Added functions to register DLLs with the garbage collector.

File size: 9.2 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
52/* Garbage collector */
53#include <gc.h>
54
55gboolean bUseGC=FALSE; /* MArk if we use the garbage collector */
56
57static gpointer gcMalloc(gulong ulBytes)
58{
59 //printf("Hi there...\n");
60 // return malloc(ulBytes);
61 return (gpointer) GC_malloc(ulBytes);
62}
63
64static gpointer gcRealloc(gpointer mem, gulong ulBytes)
65{
66 // printf("...and here\n");
67 // return realloc(mem, ulBytes);
68 return (gpointer) GC_realloc(mem, ulBytes);
69}
70
71static void gcFree(gpointer mem)
72{
73 // printf("free(): %x\n", mem);
74 return;
75 GC_free(mem);
76}
77
78
79/*
80 This is called from the EMX wrapper to set the garbage collector
81 memory functions as the GLIB default allocation function.
82 */
83void _System nomInitGarbageCollection(void* pMemInExe)
84{
85 GMemVTable vtbl={0};
86
87 /* Init the garbage collector */
88 GC_init();
89
90 vtbl.malloc=(gpointer)gcMalloc;
91 vtbl.realloc=(gpointer)gcRealloc;
92 vtbl.free=(gpointer)gcFree;
93
94 g_mem_set_vtable(&vtbl);
95 fprintf(stderr, " GC memory functions set for GLIB. (%s: %d)\n", __FILE__, __LINE__);
96
97 bUseGC=TRUE;
98}
99
100NOMEXTERN void NOMLINK nomRegisterDataAreaForGC(char* pStart, char* pEnd)
101{
102 GC_add_roots(pStart, pEnd);
103}
104
105
106/*
107 Find a library record in the buffer filled by DosQuerySysState().
108 */
109static qsLrec_t* qsFindModuleRec(const HREGDLL hRegisterDLL, USHORT hMod){
110 qsLrec_t * pModRec;
111 int a=0;
112
113 pModRec=hRegisterDLL->pLibRec;
114 while(NULL!=pModRec)
115 {
116 a++;
117 // printf("%d Checking: %x -> %04X (%s)\n", a, pModRec, pModRec->hmte, pModRec->pName);
118
119 if (NULLHANDLE==pModRec->pObjInfo && pModRec->ctObj > 0)
120 {
121 pModRec->pObjInfo = (qsLObjrec_t*)((char*)pModRec
122 + ((sizeof(qsLrec_t)
123 + pModRec->ctImpMod * sizeof(short)
124 + strlen((char*)pModRec->pName) + 1 /* filename */
125 + 3) & ~3));
126 pModRec->pNextRec = (void*)((char*)pModRec->pObjInfo
127 + sizeof(qsLObjrec_t) * pModRec->ctObj);
128 }
129 if(pModRec->hmte==hMod)
130 break;
131
132 pModRec=(qsLrec_t *)pModRec->pNextRec;
133 }
134 return pModRec;
135}
136
137
138#define BUFSIZE 1024*1024
139NOMEXTERN HREGDLL NOMLINK nomBeginRegisterDLLWithGC(void)
140{
141 ULONG rc;
142 HREGDLL hReg=NULLHANDLE;
143 PTIB ptib;
144 PPIB ppib;
145 char * buf;
146
147 rc = DosGetInfoBlocks(&ptib, &ppib);
148 if (rc!=NO_ERROR)
149 return NULLHANDLE;
150
151 buf = malloc(BUFSIZE);
152 if(!buf)
153 return NULLHANDLE;
154
155 memset(buf,0,BUFSIZE);
156
157 rc = DosQuerySysState(QS_PROCESS | QS_SEMAPHORE | QS_MTE | QS_FILESYS | QS_SHMEMORY ,
158 QS_MTE, /*0x96*/ ppib->pib_ulpid , 1UL, (PCHAR)buf, BUFSIZE);
159 if (rc==NO_ERROR) {
160 hReg=(qsPtrRec_t*) buf;
161 }
162 else
163 free(buf);
164
165 return hReg;
166}
167
168NOMEXTERN void NOMLINK nomEndRegisterDLLWithGC(const HREGDLL hRegisterDLL )
169{
170 free((char*)hRegisterDLL);
171}
172
173
174/*
175 FIXME:
176
177 This function will not find every given DLL because it doesn't follow every import of
178 each DLL.
179 It's only meant for registering the GTK+, GLIB and friends DLLs. This works because GTK2.DLL
180 is directly loaded by the exe and all the friends DLLs are imported by it.
181
182 Feel free to make this function really useful...
183
184 Oh, and some refactoring would be nice, too.
185
186 */
187#define OBJREAD 0x0001L
188#define OBJWRITE 0x0002L
189#define OBJINVALID 0x0080L
190NOMEXTERN BOOL NOMLINK nomRegisterDLLByName(const HREGDLL hRegisterDLL, const char* chrDLLName)
191{
192 qsPrec_t * p;
193 int a=0;
194
195 printf("Trying to register DLL %s\n", chrDLLName);
196
197 p=hRegisterDLL->pProcRec;
198 while(p && p->RecType == 1)
199 {
200 a++;
201 if (p->cLib) {
202 int i;
203
204 for (i=0; i<p->cLib; i++){
205 qsLrec_t * pModRec;
206
207 //printf("%d %04X (p: %04x %04X, %04X) ",i, p->pLibRec[i], p, &p->pLibRec[i], &p->pLibRec);
208
209 pModRec=qsFindModuleRec(hRegisterDLL, p->pLibRec[i]);
210 if(pModRec){
211 // printf("DLL name: %s\n", pModRec->pName);
212 if(NULLHANDLE!=strstr( pModRec->pName, chrDLLName))
213 {
214 qsLObjrec_t *pObjInfo;
215 printf(" --> Found DLL %s\n", pModRec->pName);
216 pObjInfo=pModRec->pObjInfo;
217 if(NULLHANDLE!=pObjInfo)
218 {
219 int iObj;
220 for(iObj=0; iObj<pModRec->ctObj ;iObj++)
221 {
222 if (!(pObjInfo[iObj].oflags & OBJWRITE)) continue;
223 if (!(pObjInfo[iObj].oflags & OBJREAD)) continue;
224 if ((pObjInfo[iObj].oflags & OBJINVALID)) continue;
225 printf(" #%d: %04lX, size: %04lX %04lX\n",
226 iObj, pObjInfo[iObj].oaddr, pObjInfo[iObj].osize, pObjInfo[iObj].oflags);
227 nomRegisterDataAreaForGC((char*)pObjInfo[iObj].oaddr,
228 (char*)(pObjInfo[iObj].oaddr+pObjInfo[iObj].osize));
229
230 }
231 }
232 return TRUE;
233 }
234 /* Check the imports of this DLL if any */
235 if(pModRec->ctImpMod >0)
236 {
237 int iImps;
238 PUSHORT pImpHmte;
239
240 pImpHmte=(PUSHORT)((void*)pModRec + sizeof(qsLrec_t));
241
242 for(iImps=0; iImps < pModRec->ctImpMod; iImps++)
243 {
244 qsLrec_t * pModImp;
245 // printf(" Trying import #%d (%04X)\n", iImps, pImpHmte[iImps]);
246 pModImp=qsFindModuleRec(hRegisterDLL, pImpHmte[iImps]);
247 if(pModImp){
248 //printf(" DLL name: %s\n", pModImp->pName);
249 if(NULLHANDLE!=strstr( pModImp->pName, chrDLLName))
250 {
251 qsLObjrec_t *pObjInfo;
252 printf(" --> Found DLL %s\n", pModImp->pName);
253 pObjInfo=pModImp->pObjInfo;
254 if(NULLHANDLE!=pObjInfo)
255 {
256 int iObj;
257 for(iObj=0; iObj<pModImp->ctObj ;iObj++)
258 {
259 if (!(pObjInfo[iObj].oflags & OBJWRITE)) continue;
260 if (!(pObjInfo[iObj].oflags & OBJREAD)) continue;
261 if ((pObjInfo[iObj].oflags & OBJINVALID)) continue;
262
263 printf(" #%d: %04lX, size: %04lX %04lX\n",
264 iObj, pObjInfo[iObj].oaddr, pObjInfo[iObj].osize, pObjInfo[iObj].oflags);
265 nomRegisterDataAreaForGC((char*)pObjInfo[iObj].oaddr,
266 (char*)(pObjInfo[iObj].oaddr+pObjInfo[iObj].osize));
267 }
268 }
269 return TRUE;
270 }
271 }/* for() */
272 }/* for()*/
273 }/* if() */
274 }
275 }/* For() */
276 }
277 break;
278 }
279 return NULLHANDLE;
280}
Note: See TracBrowser for help on using the repository browser.