source: trunk/nom/src/nomtkinit.c@ 135

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

Added garbage collection. To use it the GC init function must be called before using any GLib (or GTK) stuff.

File size: 10.5 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#define INCL_DOS
36#define INCL_DOSERRORS
37#define INCL_DOSMEMMGR
38#include <os2.h>
39
40#include <stdarg.h>
41#include <stdio.h>
42#include <string.h>
43
44
45/* For nomToken etc. */
46#include <nom.h>
47#include "nomtk.h"
48#include "nomobj.h"
49#include "nomcls.h"
50/* #include "nomtst.h"
51 #include "nomtst2.h" */
52#include <nomclassmanager.h>
53/* Garbage collector */
54#include <gc.h>
55
56/* Undef if you want debugging msg from somEnvironmentNew() */
57#define DEBUG_NOMENVNEW
58
59/********************************************************/
60/********************************************************/
61PNOM_ENV pGlobalNomEnv;
62/* Global class manager object */
63NOMClassMgr* NOMClassMgrObject; /* Referenced from different files */
64
65
66/********************************************************/
67/* Toolkit functions, exported */
68/********************************************************/
69
70static gpointer gcMalloc(gulong ulBytes)
71{
72 //printf("Hi there...\n");
73 // return malloc(ulBytes);
74 return (gpointer) GC_malloc(ulBytes);
75}
76
77static gpointer gcRealloc(gpointer mem, gulong ulBytes)
78{
79 // printf("...and here\n");
80 // return realloc(mem, ulBytes);
81 return (gpointer) GC_realloc(mem, ulBytes);
82}
83
84static void gcFree(gpointer mem)
85{
86 // printf("free(): %x\n", mem);
87 return;
88 GC_free(mem);
89}
90
91/*
92 This is called from the EMX wrapper to set the garbage collector
93 memory functions as the GLIB default allocation function.
94 */
95void _System nomInitGarbageCollection()
96{
97 GMemVTable vtbl={0};
98
99 /* Init the garbage collector */
100 GC_init();
101
102 vtbl.malloc=(gpointer)gcMalloc;
103 vtbl.realloc=(gpointer)gcRealloc;
104 vtbl.free=(gpointer)gcFree;
105
106 g_mem_set_vtable(&vtbl);
107 fprintf(stderr, " GC memory functions set for GLIB. (%s: %d)\n", __FILE__, __LINE__);
108}
109
110/*
111
112 */
113NOMEXTERN int NOMLINK nomPrintf(string chrFormat, ...)
114{
115 long rc;
116
117 va_list arg_ptr;
118
119 va_start (arg_ptr, chrFormat);
120 rc=vprintf( chrFormat, arg_ptr);
121 va_end (arg_ptr);
122 return rc;
123}
124
125
126
127/*
128 This function is called to initialize the NOM runtime.
129 It creates NOMObject, NOMClass and NOMClassMgr classes and the global
130 NOMClassMgrObject.
131*/
132NOMEXTERN NOMClassMgr * NOMLINK nomEnvironmentNew (void)
133{
134 NOMClassPriv* ncPriv;
135 NOMClass* nomCls;
136 NOMObject *nomObj;
137#if 0
138 NOMTest* nomTst;
139 NOMTest* nomTstObj;
140 NOMTest* nomTst2Obj;
141#endif
142
143#ifdef DEBUG_NOMENVNEW
144 nomPrintf("Entering %s to initialize NOM runtime.\n\n", __FUNCTION__);
145 nomPrintf("**** Building NOMObject class...\n");
146#endif
147 nomCls=NOMObjectNewClass(NOMObject_MajorVersion, NOMObject_MinorVersion);
148
149#ifdef DEBUG_NOMENVNEW
150 nomPrintf("NOMObject class: %x\n", nomCls);
151 nomPrintf("\n**** Building NOMClass class...\n");
152#endif
153
154 nomCls=NOMClassNewClass(NOMClass_MajorVersion, NOMClass_MinorVersion);
155#ifdef DEBUG_NOMENVNEW
156 nomPrintf("NOMClass class: %x\n", nomCls);
157 nomPrintf("\n**** Building NOMClassMgr class and NOMClassMgrObject...\n");
158#endif
159 NOMClassMgrObject=NOMClassMgrNew();
160 if(!NOMClassMgrObject)
161 g_error("Can't create the NOMClassMgr class object!\n");
162
163#ifdef DEBUG_NOMENVNEW
164 nomPrintf("%s: NOMClassMgrObject: %x \n", __FUNCTION__, NOMClassMgrObject);
165#endif
166
167 /* Now register the classes we already have */
168 // _nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->defaultMetaClass->mtab, NULLHANDLE); //NOMClass
169 _nomClassReady(pGlobalNomEnv->defaultMetaClass, NULLHANDLE); //NOMClass
170 //_nomRegisterClass(NOMClassMgrObject, NOMClassMgrObject->mtab, NULLHANDLE); //NOMClassMgr
171 _nomClassReady( _NOMClassMgr, NULLHANDLE); //NOMClassMgr
172 ncPriv=(NOMClassPriv*)pGlobalNomEnv->nomObjectMetaClass->mtab->nomClsInfo;
173
174 /* Do not register the NOMObject metaclass here. It's already registered because it's
175 NOMClass in fact. */
176 //_nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->nomObjectMetaClass->mtab, NULLHANDLE); //NOMObject
177 //_nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->ncpNOMObject->mtab, NULLHANDLE); //NOMObject
178 _nomClassReady(_NOMObject, NULLHANDLE); //NOMObject
179
180
181#if 0
182 nomPrintf("\n**** Building NOMTest class...\n");
183 nomTst=NOMTestNewClass(NOMTest_MajorVersion, NOMTest_MinorVersion);
184 nomPrintf("NOMTest class: %x\n", nomTst);
185
186 nomPrintf("Now building a NOMTest object from %x...\n", _NOMTest);
187 nomTstObj= NOMTestNew();
188 nomPrintf("NOMTest object: %x\n", nomTstObj);
189#endif
190
191#if 0
192 nomPrintf("Calling _nomTestFunc() 1\n", nomTstObj);
193 _nomTestFunc(nomTstObj, NULLHANDLE);
194 nomPrintf("Calling _nomTestFuncString() 1\n", nomTstObj);
195 nomPrintf("--> %s\n",_nomTestFuncString(nomTstObj, NULLHANDLE));
196
197 nomPrintf("Calling _nomTestFunc() 2\n", nomTstObj);
198 _nomTestFunc(nomTstObj, NULLHANDLE);
199 nomPrintf("Calling _nomTestFuncString() 2\n", nomTstObj);
200 nomPrintf("--> %s\n",_nomTestFuncString(nomTstObj, NULLHANDLE));
201#endif
202
203#if 0
204 //#if 0
205 nomPrintf("\n**** Building NOMTest2 class...\n");
206 nomPrintf("\nNow building a NOMTest2 object...\n");
207 // NOMTest2NewClass(NOMTest2_MajorVersion, NOMTest2_MinorVersion);
208 nomTst2Obj= NOMTest2New();
209 nomPrintf("NOMTest2 object: %x\n", nomTst2Obj);
210 //#endif
211
212
213 nomPrintf("\nCalling _nomTestFunc_NOMTest2() 1\n", nomTst2Obj);
214 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
215 nomPrintf("\nCalling _nomTestFuncString_NOMTest2() 1\n", nomTst2Obj);
216 nomPrintf("--> %s\n",_nomTestFuncString_NOMTest2(nomTst2Obj, NULLHANDLE));
217 nomPrintf("\nCalling _nomTestFunc_NOMTest2() 2\n", nomTst2Obj);
218 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
219
220 nomPrintf("\nCalling _nomTestFunc() with NOMTest2 object: %x\n", nomTst2Obj);
221 _nomTestFunc(nomTst2Obj, NULLHANDLE);
222 nomPrintf("\nCalling _nomTestFuncString() with NOMTest2: %x\n", nomTst2Obj);
223 nomPrintf("--> %s\n",_nomTestFuncString(nomTst2Obj, NULLHANDLE));
224 nomPrintf("\n");
225 _nomTestFunc(nomTst2Obj, NULLHANDLE);
226 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
227#endif
228
229#if 0
230 _dumpMtab(nomTstObj->mtab);
231 _dumpMtab(nomTst2Obj->mtab);
232 // _dumpMtab(NOMClassMgrObject->mtab);
233 // _dumpClasses();
234 _nomTestFunc(nomTstObj, NULLHANDLE);
235 _nomTestFunc(nomTst2Obj, NULLHANDLE);
236 _nomTestFunc(nomTstObj, NULLHANDLE);
237 _nomTestFunc(nomTst2Obj, NULLHANDLE);
238#endif
239
240#if 0
241 nomPrintf("NOMTest object: %x, %x\n", nomTstObj, NOMTestClassData.classObject);
242 nomTstObj=_nomNew((_NOMTest ? _NOMTest : NOMTestNewClass(NOMTest_MajorVersion, NOMTest_MinorVersion)), (void*) 0);
243 nomPrintf("NOMTest object: %x\n", nomTstObj);
244 nomTstObj= NOMTestNew();
245 nomPrintf("NOMTest object: %x\n", nomTstObj);
246#endif
247
248 return NOMClassMgrObject;
249}
250
251
252
253NOMEXTERN PNOM_ENV NOMLINK nomTkInit(void)
254{
255
256
257 PVOID memPtr;
258 PVOID memPool;
259
260 nomPrintf("Entering %s...\n", __FUNCTION__);
261 nomPrintf("*************************************\n");
262 nomPrintf("!! This function must be rewritten !!\n");
263 nomPrintf("!! It's using OS/2 only memory !!\n");
264 nomPrintf("!! functions. !!\n");
265 nomPrintf("*************************************\n");
266
267 /* Check if we already allocated our shared mem */
268 if(NO_ERROR==DosGetNamedSharedMem(&memPtr,SOMTK_SHARED_MEM_NAME_ROOT , PAG_EXECUTE|PAG_READ|PAG_WRITE)) {
269 nomPrintf("%s: Found root shared mem: %x.\n", __FUNCTION__, memPtr);
270 /* Give process access to memory pool*/
271 DosGetSharedMem( ((PNOM_ENV)memPtr)->pMemPool, PAG_READ|PAG_WRITE|PAG_EXECUTE);
272 if(NO_ERROR!=DosSubSetMem(((PNOM_ENV)memPtr)->pMemPool, DOSSUB_SPARSE_OBJ|DOSSUB_SERIALIZE, SOMTK_SHARED_MEM_SIZE_POOL)) {
273 DosFreeMem(memPool);
274 DosFreeMem(memPtr);
275 return NULL;
276 }
277 pGlobalNomEnv=(PNOM_ENV)memPtr;
278 return (PNOM_ENV)memPtr;
279 }
280 nomPrintf("%s: No root memory yet\n", __FUNCTION__);
281
282 /* Get the mem for the root structure in a shared memory area */
283 if(NO_ERROR!=DosAllocSharedMem(&memPtr, SOMTK_SHARED_MEM_NAME_ROOT, SOMTK_SHARED_MEM_SIZE_ROOT,
284 PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE))
285 return NULL;
286
287 nomPrintf("%s: Got root memory: %x\n", __FUNCTION__, memPtr);
288
289 /* Get our shared memory pool */
290 if(NO_ERROR!=DosAllocSharedMem(&memPool, NULL, SOMTK_SHARED_MEM_SIZE_POOL,OBJ_GETTABLE | PAG_EXECUTE|PAG_READ|PAG_WRITE)) {
291 DosFreeMem(memPtr);
292 return NULL;
293 }
294 nomPrintf("%s: Got shared memory pool: %x\n", __FUNCTION__, memPool);
295
296 /* Now init the structure */
297 memset(memPtr, 0, 1 /*SOMTK_SHARED_MEM_SIZE_ROOT*/);
298 nomPrintf("%s: zeroed memory\n", __FUNCTION__);
299
300 ((PNOM_ENV)memPtr)->cbSize=sizeof(NOM_ENV);
301 /* Init memory pool */
302 if(NO_ERROR!=DosSubSetMem(memPool, DOSSUB_INIT|DOSSUB_SPARSE_OBJ|DOSSUB_SERIALIZE, SOMTK_SHARED_MEM_SIZE_POOL)) {
303 DosFreeMem(memPool);
304 DosFreeMem(memPtr);
305 return NULL;
306 }
307 ((PNOM_ENV)memPtr)->pMemPool=memPool;
308 if(NO_ERROR!=DosCreateMutexSem(NULL, &((PNOM_ENV)memPtr)->hmtx, DC_SEM_SHARED, FALSE))
309 {
310 DosFreeMem(memPool);
311 DosFreeMem(memPtr);
312 return NULL;
313 }
314 pGlobalNomEnv=(PNOM_ENV)memPtr;
315
316 return (PNOM_ENV)memPtr;
317}
318
319
320
321
322
323
324
325
326
327
328
Note: See TracBrowser for help on using the repository browser.