test setinfo FULL_EA_INFORMATION in gentest
[gd/samba/.git] / source4 / lib / appweb / ejs-2.0 / ejs / system / ejsGC.c
1 /*
2  *      @file   ejsGC.c
3  *      @brief  Garbage collector class for the EJS Object Model
4  */
5 /********************************** Copyright *********************************/
6 /*
7  *      Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
8  */
9 /********************************** Includes **********************************/
10
11 #include        "ejs.h"
12
13 /******************************************************************************/
14 /************************************ Methods *********************************/
15 /******************************************************************************/
16 #if (WIN || BREW_SIMULATOR) && BLD_DEBUG
17
18 static int checkProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
19 {
20         _CrtCheckMemory();
21         return 0;
22 }
23
24 #endif
25 /******************************************************************************/
26
27 static int debugProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
28 {
29         if (argc != 1) {
30                 ejsError(ep, EJS_ARG_ERROR, "Bad args: debug(debugLevel)");
31                 return -1;
32         }
33
34         ejsSetGCDebugLevel(ep, ejsVarToInteger(argv[0]));
35         return 0;
36 }
37
38 /******************************************************************************/
39 /*
40  *      Print stats and dump objects
41  */
42
43 static int printStatsProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
44 {
45         bool    leakStats;
46
47         if (argc > 1) {
48                 leakStats = ejsVarToInteger(argv[0]);
49         } else {
50                 leakStats = 0;
51         }
52
53 #if BLD_FEATURE_ALLOC_STATS
54         ejsPrintAllocReport(ep, 0);
55
56         mprPrintAllocReport(mprGetApp(ep), leakStats, 0);
57 #endif
58
59 #if BLD_DEBUG
60         ejsDumpObjects(ep);
61 #endif
62
63         return 0;
64 }
65
66 /******************************************************************************/
67
68 static int runProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
69 {
70         if (argc > 1) {
71                 ejsError(ep, EJS_ARG_ERROR, "Bad args: run([quick])");
72                 return -1;
73         }
74
75         if (argc == 1) {
76                 ejsIncrementalCollectGarbage(ep);
77         } else {
78                 ejsCollectGarbage(ep, -1);
79         }
80         return 0;
81 }
82
83 /******************************************************************************/
84
85 static int usedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
86 {
87         ejsSetReturnValueToInteger(ep, ejsGetUsedMemory(ep));
88         return 0;
89 }
90
91 /******************************************************************************/
92
93 static int allocatedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, 
94         EjsVar **argv)
95 {
96 #if BLD_FEATURE_ALLOC_STATS
97         ejsSetReturnValueToInteger(ep, ejsGetAllocatedMemory(ep));
98 #endif
99         return 0;
100 }
101
102 /******************************************************************************/
103
104 static int mprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
105 {
106 #if BLD_FEATURE_ALLOC_STATS
107         ejsSetReturnValueToInteger(ep, mprGetAllocatedMemory(ep));
108 #endif
109         return 0;
110 }
111
112 /******************************************************************************/
113
114 static int peakMprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
115 {
116 #if BLD_FEATURE_ALLOC_STATS
117         ejsSetReturnValueToInteger(ep, mprGetPeakAllocatedMemory(ep));
118 #endif
119         return 0;
120 }
121
122 /******************************************************************************/
123
124 static int getDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
125 {
126         ejsSetReturnValueToInteger(ep, ep->gc.debugLevel);
127         return 0;
128 }
129
130 /******************************************************************************/
131
132 static int setDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
133 {
134         if (argc != 1) {
135                 ejsArgError(ep, "Bad arguments");
136                 return -1;
137         }
138         ep->gc.debugLevel= ejsVarToInteger(argv[0]);
139         return 0;
140 }
141
142 /******************************************************************************/
143
144 static int getEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
145 {
146         ejsSetReturnValueToBoolean(ep, ep->gc.enable);
147         return 0;
148 }
149
150 /******************************************************************************/
151
152 static int setEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
153 {
154         if (argc != 1) {
155                 ejsArgError(ep, "Bad arguments");
156                 return -1;
157         }
158         ep->gc.enable= ejsVarToBoolean(argv[0]);
159         return 0;
160 }
161
162 /******************************************************************************/
163
164 static int getDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
165 {
166         ejsSetReturnValueToBoolean(ep, ep->gc.enableDemandCollect);
167         return 0;
168 }
169
170 /******************************************************************************/
171
172 static int setDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
173 {
174         if (argc != 1) {
175                 ejsArgError(ep, "Bad arguments");
176                 return -1;
177         }
178         ep->gc.enableDemandCollect = ejsVarToBoolean(argv[0]);
179         return 0;
180 }
181
182 /******************************************************************************/
183
184 static int getIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
185 {
186         ejsSetReturnValueToBoolean(ep, ep->gc.enableIdleCollect);
187         return 0;
188 }
189
190 /******************************************************************************/
191
192 static int setIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
193 {
194         if (argc != 1) {
195                 ejsArgError(ep, "Bad arguments");
196                 return -1;
197         }
198         ep->gc.enableIdleCollect = ejsVarToBoolean(argv[0]);
199         return 0;
200 }
201
202 /******************************************************************************/
203
204 static int getWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
205 {
206         ejsSetReturnValueToInteger(ep, ep->gc.workQuota);
207         return 0;
208 }
209
210 /******************************************************************************/
211
212 static int setWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
213 {
214         int             quota;
215
216         if (argc != 1) {
217                 ejsArgError(ep, "Bad arguments");
218                 return -1;
219         }
220         quota = ejsVarToInteger(argv[0]);
221         if (quota < EJS_GC_MIN_WORK_QUOTA && quota != 0) {
222                 ejsArgError(ep, "Bad work quota");
223                 return -1;
224         }
225
226         ep->gc.workQuota = quota;
227         return 0;
228 }
229
230 /******************************************************************************/
231
232 static int getMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
233 {
234         ejsSetReturnValueToInteger(ep, ep->gc.maxMemory);
235         return 0;
236 }
237
238 /******************************************************************************/
239
240 static int setMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
241 {
242         int             maxMemory;
243
244         if (argc != 1) {
245                 ejsArgError(ep, "Bad arguments");
246                 return -1;
247         }
248         maxMemory = ejsVarToInteger(argv[0]);
249         if (maxMemory < 0) {
250                 ejsArgError(ep, "Bad maxMemory");
251                 return -1;
252         }
253
254         ep->gc.maxMemory = maxMemory;
255         return 0;
256 }
257
258 /******************************************************************************/
259 /******************************** Initialization ******************************/
260 /******************************************************************************/
261
262 int ejsDefineGCClass(Ejs *ep)
263 {
264         EjsVar  *gcClass;
265         int             flags;
266
267         flags = EJS_NO_LOCAL;
268
269         /*
270          *      NOTE: We create the GC class and define static methods on it. There 
271          *      is no object instance
272          */
273         gcClass =  ejsDefineClass(ep, "System.GC", "Object", 0);
274         if (gcClass == 0) {
275                 return MPR_ERR_CANT_INITIALIZE;
276         }
277
278         /*
279          *      MOB -- convert these to properties with accessors when available
280          */
281         ejsDefineCMethod(ep, gcClass, "printStats", printStatsProc, flags);
282         ejsDefineCMethod(ep, gcClass, "run", runProc, flags);
283
284         ejsDefineCMethod(ep, gcClass, "getUsedMemory", usedMemoryProc, flags);
285         ejsDefineCMethod(ep, gcClass, "getAllocatedMemory", allocatedMemoryProc,
286                 flags);
287         ejsDefineCMethod(ep, gcClass, "getMprMemory", mprMemoryProc, flags);
288         ejsDefineCMethod(ep, gcClass, "getPeakMprMemory", peakMprMemoryProc, flags);
289         ejsDefineCMethod(ep, gcClass, "debug", debugProc, flags);
290
291 #if (WIN || BREW_SIMULATOR) && BLD_DEBUG
292         ejsDefineCMethod(ep, gcClass, "check", checkProc, flags);
293 #endif
294
295         ejsDefineCAccessors(ep, gcClass, "debugLevel", 
296                 getDebugLevel, setDebugLevel, flags);
297
298         ejsDefineCAccessors(ep, gcClass, "enable", 
299                 getEnable, setEnable, flags);
300
301         ejsDefineCAccessors(ep, gcClass, "demandCollect", 
302                 getDemandCollect, setDemandCollect, flags);
303
304         ejsDefineCAccessors(ep, gcClass, "idleCollect", 
305                 getIdleCollect, setIdleCollect, flags);
306
307         ejsDefineCAccessors(ep, gcClass, "workQuota", 
308                 getWorkQuota, setWorkQuota, flags);
309
310         ejsDefineCAccessors(ep, gcClass, "maxMemory", 
311                 getMaxMemory, setMaxMemory, flags);
312
313         return ejsObjHasErrors(gcClass) ? MPR_ERR_CANT_INITIALIZE : 0;
314 }
315
316 /******************************************************************************/
317
318 /*
319  * Local variables:
320  * tab-width: 4
321  * c-basic-offset: 4
322  * End:
323  * vim:tw=78
324  * vim600: sw=4 ts=4 fdm=marker
325  * vim<600: sw=4 ts=4
326  */