3 * @brief Garbage collector class for the EJS Object Model
5 /********************************** Copyright *********************************/
7 * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
9 /********************************** Includes **********************************/
13 /******************************************************************************/
14 /************************************ Methods *********************************/
15 /******************************************************************************/
16 #if (WIN || BREW_SIMULATOR) && BLD_DEBUG
18 static int checkProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
25 /******************************************************************************/
27 static int debugProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
30 ejsError(ep, EJS_ARG_ERROR, "Bad args: debug(debugLevel)");
34 ejsSetGCDebugLevel(ep, ejsVarToInteger(argv[0]));
38 /******************************************************************************/
40 * Print stats and dump objects
43 static int printStatsProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
48 leakStats = ejsVarToInteger(argv[0]);
53 #if BLD_FEATURE_ALLOC_STATS
54 ejsPrintAllocReport(ep, 0);
56 mprPrintAllocReport(mprGetApp(ep), leakStats, 0);
66 /******************************************************************************/
68 static int runProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
71 ejsError(ep, EJS_ARG_ERROR, "Bad args: run([quick])");
76 ejsIncrementalCollectGarbage(ep);
78 ejsCollectGarbage(ep, -1);
83 /******************************************************************************/
85 static int usedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
87 ejsSetReturnValueToInteger(ep, ejsGetUsedMemory(ep));
91 /******************************************************************************/
93 static int allocatedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc,
96 #if BLD_FEATURE_ALLOC_STATS
97 ejsSetReturnValueToInteger(ep, ejsGetAllocatedMemory(ep));
102 /******************************************************************************/
104 static int mprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
106 #if BLD_FEATURE_ALLOC_STATS
107 ejsSetReturnValueToInteger(ep, mprGetAllocatedMemory(ep));
112 /******************************************************************************/
114 static int peakMprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
116 #if BLD_FEATURE_ALLOC_STATS
117 ejsSetReturnValueToInteger(ep, mprGetPeakAllocatedMemory(ep));
122 /******************************************************************************/
124 static int getDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
126 ejsSetReturnValueToInteger(ep, ep->gc.debugLevel);
130 /******************************************************************************/
132 static int setDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
135 ejsArgError(ep, "Bad arguments");
138 ep->gc.debugLevel= ejsVarToInteger(argv[0]);
142 /******************************************************************************/
144 static int getEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
146 ejsSetReturnValueToBoolean(ep, ep->gc.enable);
150 /******************************************************************************/
152 static int setEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
155 ejsArgError(ep, "Bad arguments");
158 ep->gc.enable= ejsVarToBoolean(argv[0]);
162 /******************************************************************************/
164 static int getDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
166 ejsSetReturnValueToBoolean(ep, ep->gc.enableDemandCollect);
170 /******************************************************************************/
172 static int setDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
175 ejsArgError(ep, "Bad arguments");
178 ep->gc.enableDemandCollect = ejsVarToBoolean(argv[0]);
182 /******************************************************************************/
184 static int getIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
186 ejsSetReturnValueToBoolean(ep, ep->gc.enableIdleCollect);
190 /******************************************************************************/
192 static int setIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
195 ejsArgError(ep, "Bad arguments");
198 ep->gc.enableIdleCollect = ejsVarToBoolean(argv[0]);
202 /******************************************************************************/
204 static int getWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
206 ejsSetReturnValueToInteger(ep, ep->gc.workQuota);
210 /******************************************************************************/
212 static int setWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
217 ejsArgError(ep, "Bad arguments");
220 quota = ejsVarToInteger(argv[0]);
221 if (quota < EJS_GC_MIN_WORK_QUOTA && quota != 0) {
222 ejsArgError(ep, "Bad work quota");
226 ep->gc.workQuota = quota;
230 /******************************************************************************/
232 static int getMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
234 ejsSetReturnValueToInteger(ep, ep->gc.maxMemory);
238 /******************************************************************************/
240 static int setMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
245 ejsArgError(ep, "Bad arguments");
248 maxMemory = ejsVarToInteger(argv[0]);
250 ejsArgError(ep, "Bad maxMemory");
254 ep->gc.maxMemory = maxMemory;
258 /******************************************************************************/
259 /******************************** Initialization ******************************/
260 /******************************************************************************/
262 int ejsDefineGCClass(Ejs *ep)
267 flags = EJS_NO_LOCAL;
270 * NOTE: We create the GC class and define static methods on it. There
271 * is no object instance
273 gcClass = ejsDefineClass(ep, "System.GC", "Object", 0);
275 return MPR_ERR_CANT_INITIALIZE;
279 * MOB -- convert these to properties with accessors when available
281 ejsDefineCMethod(ep, gcClass, "printStats", printStatsProc, flags);
282 ejsDefineCMethod(ep, gcClass, "run", runProc, flags);
284 ejsDefineCMethod(ep, gcClass, "getUsedMemory", usedMemoryProc, flags);
285 ejsDefineCMethod(ep, gcClass, "getAllocatedMemory", allocatedMemoryProc,
287 ejsDefineCMethod(ep, gcClass, "getMprMemory", mprMemoryProc, flags);
288 ejsDefineCMethod(ep, gcClass, "getPeakMprMemory", peakMprMemoryProc, flags);
289 ejsDefineCMethod(ep, gcClass, "debug", debugProc, flags);
291 #if (WIN || BREW_SIMULATOR) && BLD_DEBUG
292 ejsDefineCMethod(ep, gcClass, "check", checkProc, flags);
295 ejsDefineCAccessors(ep, gcClass, "debugLevel",
296 getDebugLevel, setDebugLevel, flags);
298 ejsDefineCAccessors(ep, gcClass, "enable",
299 getEnable, setEnable, flags);
301 ejsDefineCAccessors(ep, gcClass, "demandCollect",
302 getDemandCollect, setDemandCollect, flags);
304 ejsDefineCAccessors(ep, gcClass, "idleCollect",
305 getIdleCollect, setIdleCollect, flags);
307 ejsDefineCAccessors(ep, gcClass, "workQuota",
308 getWorkQuota, setWorkQuota, flags);
310 ejsDefineCAccessors(ep, gcClass, "maxMemory",
311 getMaxMemory, setMaxMemory, flags);
313 return ejsObjHasErrors(gcClass) ? MPR_ERR_CANT_INITIALIZE : 0;
316 /******************************************************************************/
324 * vim600: sw=4 ts=4 fdm=marker