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

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

Reviewed memory allocation functions.

File size: 10.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#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 */
64gboolean bUseGC=FALSE; /* MArk if we use the garbage collector */
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 bUseGC=TRUE;
109}
110
111/*
112
113 */
114NOMEXTERN int NOMLINK nomPrintf(string chrFormat, ...)
115{
116 long rc;
117
118 va_list arg_ptr;
119
120 va_start (arg_ptr, chrFormat);
121 rc=vprintf( chrFormat, arg_ptr);
122 va_end (arg_ptr);
123 return rc;
124}
125
126
127
128/*
129 This function is called to initialize the NOM runtime.
130 It creates NOMObject, NOMClass and NOMClassMgr classes and the global
131 NOMClassMgrObject.
132*/
133NOMEXTERN NOMClassMgr * NOMLINK nomEnvironmentNew (void)
134{
135 NOMClassPriv* ncPriv;
136 NOMClass* nomCls;
137 NOMObject *nomObj;
138#if 0
139 NOMTest* nomTst;
140 NOMTest* nomTstObj;
141 NOMTest* nomTst2Obj;
142#endif
143
144#ifdef DEBUG_NOMENVNEW
145 nomPrintf("Entering %s to initialize NOM runtime.\n\n", __FUNCTION__);
146 nomPrintf("**** Building NOMObject class...\n");
147#endif
148 nomCls=NOMObjectNewClass(NOMObject_MajorVersion, NOMObject_MinorVersion);
149
150#ifdef DEBUG_NOMENVNEW
151 nomPrintf("NOMObject class: %x\n", nomCls);
152 nomPrintf("\n**** Building NOMClass class...\n");
153#endif
154
155 nomCls=NOMClassNewClass(NOMClass_MajorVersion, NOMClass_MinorVersion);
156#ifdef DEBUG_NOMENVNEW
157 nomPrintf("NOMClass class: %x\n", nomCls);
158 nomPrintf("\n**** Building NOMClassMgr class and NOMClassMgrObject...\n");
159#endif
160 NOMClassMgrObject=NOMClassMgrNew();
161 if(!NOMClassMgrObject)
162 g_error("Can't create the NOMClassMgr class object!\n");
163
164#ifdef DEBUG_NOMENVNEW
165 nomPrintf("%s: NOMClassMgrObject: %x \n", __FUNCTION__, NOMClassMgrObject);
166#endif
167
168 /* Now register the classes we already have */
169 // _nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->defaultMetaClass->mtab, NULLHANDLE); //NOMClass
170 _nomClassReady(pGlobalNomEnv->defaultMetaClass, NULLHANDLE); //NOMClass
171 //_nomRegisterClass(NOMClassMgrObject, NOMClassMgrObject->mtab, NULLHANDLE); //NOMClassMgr
172 _nomClassReady( _NOMClassMgr, NULLHANDLE); //NOMClassMgr
173 ncPriv=(NOMClassPriv*)pGlobalNomEnv->nomObjectMetaClass->mtab->nomClsInfo;
174
175 /* Do not register the NOMObject metaclass here. It's already registered because it's
176 NOMClass in fact. */
177 //_nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->nomObjectMetaClass->mtab, NULLHANDLE); //NOMObject
178 //_nomRegisterClass(NOMClassMgrObject, pGlobalNomEnv->ncpNOMObject->mtab, NULLHANDLE); //NOMObject
179 _nomClassReady(_NOMObject, NULLHANDLE); //NOMObject
180
181
182#if 0
183 nomPrintf("\n**** Building NOMTest class...\n");
184 nomTst=NOMTestNewClass(NOMTest_MajorVersion, NOMTest_MinorVersion);
185 nomPrintf("NOMTest class: %x\n", nomTst);
186
187 nomPrintf("Now building a NOMTest object from %x...\n", _NOMTest);
188 nomTstObj= NOMTestNew();
189 nomPrintf("NOMTest object: %x\n", nomTstObj);
190#endif
191
192#if 0
193 nomPrintf("Calling _nomTestFunc() 1\n", nomTstObj);
194 _nomTestFunc(nomTstObj, NULLHANDLE);
195 nomPrintf("Calling _nomTestFuncString() 1\n", nomTstObj);
196 nomPrintf("--> %s\n",_nomTestFuncString(nomTstObj, NULLHANDLE));
197
198 nomPrintf("Calling _nomTestFunc() 2\n", nomTstObj);
199 _nomTestFunc(nomTstObj, NULLHANDLE);
200 nomPrintf("Calling _nomTestFuncString() 2\n", nomTstObj);
201 nomPrintf("--> %s\n",_nomTestFuncString(nomTstObj, NULLHANDLE));
202#endif
203
204#if 0
205 //#if 0
206 nomPrintf("\n**** Building NOMTest2 class...\n");
207 nomPrintf("\nNow building a NOMTest2 object...\n");
208 // NOMTest2NewClass(NOMTest2_MajorVersion, NOMTest2_MinorVersion);
209 nomTst2Obj= NOMTest2New();
210 nomPrintf("NOMTest2 object: %x\n", nomTst2Obj);
211 //#endif
212
213
214 nomPrintf("\nCalling _nomTestFunc_NOMTest2() 1\n", nomTst2Obj);
215 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
216 nomPrintf("\nCalling _nomTestFuncString_NOMTest2() 1\n", nomTst2Obj);
217 nomPrintf("--> %s\n",_nomTestFuncString_NOMTest2(nomTst2Obj, NULLHANDLE));
218 nomPrintf("\nCalling _nomTestFunc_NOMTest2() 2\n", nomTst2Obj);
219 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
220
221 nomPrintf("\nCalling _nomTestFunc() with NOMTest2 object: %x\n", nomTst2Obj);
222 _nomTestFunc(nomTst2Obj, NULLHANDLE);
223 nomPrintf("\nCalling _nomTestFuncString() with NOMTest2: %x\n", nomTst2Obj);
224 nomPrintf("--> %s\n",_nomTestFuncString(nomTst2Obj, NULLHANDLE));
225 nomPrintf("\n");
226 _nomTestFunc(nomTst2Obj, NULLHANDLE);
227 _nomTestFunc_NOMTest2(nomTst2Obj, NULLHANDLE);
228#endif
229
230#if 0
231 _dumpMtab(nomTstObj->mtab);
232 _dumpMtab(nomTst2Obj->mtab);
233 // _dumpMtab(NOMClassMgrObject->mtab);
234 // _dumpClasses();
235 _nomTestFunc(nomTstObj, NULLHANDLE);
236 _nomTestFunc(nomTst2Obj, NULLHANDLE);
237 _nomTestFunc(nomTstObj, NULLHANDLE);
238 _nomTestFunc(nomTst2Obj, NULLHANDLE);
239#endif
240
241#if 0
242 nomPrintf("NOMTest object: %x, %x\n", nomTstObj, NOMTestClassData.classObject);
243 nomTstObj=_nomNew((_NOMTest ? _NOMTest : NOMTestNewClass(NOMTest_MajorVersion, NOMTest_MinorVersion)), (void*) 0);
244 nomPrintf("NOMTest object: %x\n", nomTstObj);
245 nomTstObj= NOMTestNew();
246 nomPrintf("NOMTest object: %x\n", nomTstObj);
247#endif
248
249 return NOMClassMgrObject;
250}
251
252
253
254NOMEXTERN PNOM_ENV NOMLINK nomTkInit(void)
255{
256
257
258 PVOID memPtr;
259 PVOID memPool;
260
261 nomPrintf("Entering %s...\n", __FUNCTION__);
262 nomPrintf("*************************************\n");
263 nomPrintf("!! This function must be rewritten !!\n");
264 nomPrintf("!! It's using OS/2 only memory !!\n");
265 nomPrintf("!! functions. !!\n");
266 nomPrintf("*************************************\n");
267
268 /* Check if we already allocated our shared mem */
269 if(NO_ERROR==DosGetNamedSharedMem(&memPtr,SOMTK_SHARED_MEM_NAME_ROOT , PAG_EXECUTE|PAG_READ|PAG_WRITE)) {
270 nomPrintf("%s: Found root shared mem: %x.\n", __FUNCTION__, memPtr);
271 /* Give process access to memory pool*/
272 DosGetSharedMem( ((PNOM_ENV)memPtr)->pMemPool, PAG_READ|PAG_WRITE|PAG_EXECUTE);
273 if(NO_ERROR!=DosSubSetMem(((PNOM_ENV)memPtr)->pMemPool, DOSSUB_SPARSE_OBJ|DOSSUB_SERIALIZE, SOMTK_SHARED_MEM_SIZE_POOL)) {
274 DosFreeMem(memPool);
275 DosFreeMem(memPtr);
276 return NULL;
277 }
278 pGlobalNomEnv=(PNOM_ENV)memPtr;
279 return (PNOM_ENV)memPtr;
280 }
281 nomPrintf("%s: No root memory yet\n", __FUNCTION__);
282
283 /* Get the mem for the root structure in a shared memory area */
284 if(NO_ERROR!=DosAllocSharedMem(&memPtr, SOMTK_SHARED_MEM_NAME_ROOT, SOMTK_SHARED_MEM_SIZE_ROOT,
285 PAG_COMMIT | PAG_EXECUTE | PAG_READ | PAG_WRITE))
286 return NULL;
287
288 nomPrintf("%s: Got root memory: %x\n", __FUNCTION__, memPtr);
289
290 /* Get our shared memory pool */
291 if(NO_ERROR!=DosAllocSharedMem(&memPool, NULL, SOMTK_SHARED_MEM_SIZE_POOL,OBJ_GETTABLE | PAG_EXECUTE|PAG_READ|PAG_WRITE)) {
292 DosFreeMem(memPtr);
293 return NULL;
294 }
295 nomPrintf("%s: Got shared memory pool: %x\n", __FUNCTION__, memPool);
296
297 /* Now init the structure */
298 memset(memPtr, 0, 1 /*SOMTK_SHARED_MEM_SIZE_ROOT*/);
299 nomPrintf("%s: zeroed memory\n", __FUNCTION__);
300
301 ((PNOM_ENV)memPtr)->cbSize=sizeof(NOM_ENV);
302 /* Init memory pool */
303 if(NO_ERROR!=DosSubSetMem(memPool, DOSSUB_INIT|DOSSUB_SPARSE_OBJ|DOSSUB_SERIALIZE, SOMTK_SHARED_MEM_SIZE_POOL)) {
304 DosFreeMem(memPool);
305 DosFreeMem(memPtr);
306 return NULL;
307 }
308 ((PNOM_ENV)memPtr)->pMemPool=memPool;
309 if(NO_ERROR!=DosCreateMutexSem(NULL, &((PNOM_ENV)memPtr)->hmtx, DC_SEM_SHARED, FALSE))
310 {
311 DosFreeMem(memPool);
312 DosFreeMem(memPtr);
313 return NULL;
314 }
315 pGlobalNomEnv=(PNOM_ENV)memPtr;
316
317 return (PNOM_ENV)memPtr;
318}
319
320
321
322
323
324
325
326
327
328
329
Note: See TracBrowser for help on using the repository browser.