summaryrefslogtreecommitdiff
path: root/source4/lib/appweb/ejs-2.0/ejs/system/ejsGC.c
blob: 411975f80e0b53997aa6cd515617b51d3c58561f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
/*
 *	@file 	ejsGC.c
 *	@brief 	Garbage collector class for the EJS Object Model
 */
/********************************** Copyright *********************************/
/*
 *	Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
 */
/********************************** Includes **********************************/

#include	"ejs.h"

/******************************************************************************/
/************************************ Methods *********************************/
/******************************************************************************/
#if (WIN || BREW_SIMULATOR) && BLD_DEBUG

static int checkProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	_CrtCheckMemory();
	return 0;
}

#endif
/******************************************************************************/

static int debugProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 1) {
		ejsError(ep, EJS_ARG_ERROR, "Bad args: debug(debugLevel)");
		return -1;
	}

	ejsSetGCDebugLevel(ep, ejsVarToInteger(argv[0]));
	return 0;
}

/******************************************************************************/
/*
 *	Print stats and dump objects
 */

static int printStatsProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	bool	leakStats;

	if (argc > 1) {
		leakStats = ejsVarToInteger(argv[0]);
	} else {
		leakStats = 0;
	}

#if BLD_FEATURE_ALLOC_STATS
	ejsPrintAllocReport(ep, 0);

	mprPrintAllocReport(mprGetApp(ep), leakStats, 0);
#endif

#if BLD_DEBUG
	ejsDumpObjects(ep);
#endif

	return 0;
}

/******************************************************************************/

static int runProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc > 1) {
		ejsError(ep, EJS_ARG_ERROR, "Bad args: run([quick])");
		return -1;
	}

	if (argc == 1) {
		ejsIncrementalCollectGarbage(ep);
	} else {
		ejsCollectGarbage(ep, -1);
	}
	return 0;
}

/******************************************************************************/

static int usedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ep, ejsGetUsedMemory(ep));
	return 0;
}

/******************************************************************************/

static int allocatedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, 
	EjsVar **argv)
{
#if BLD_FEATURE_ALLOC_STATS
	ejsSetReturnValueToInteger(ep, ejsGetAllocatedMemory(ep));
#endif
	return 0;
}

/******************************************************************************/

static int mprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
#if BLD_FEATURE_ALLOC_STATS
	ejsSetReturnValueToInteger(ep, mprGetAllocatedMemory(ep));
#endif
	return 0;
}

/******************************************************************************/

static int peakMprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
#if BLD_FEATURE_ALLOC_STATS
	ejsSetReturnValueToInteger(ep, mprGetPeakAllocatedMemory(ep));
#endif
	return 0;
}

/******************************************************************************/

static int getDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ep, ep->gc.debugLevel);
	return 0;
}

/******************************************************************************/

static int setDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	ep->gc.debugLevel= ejsVarToInteger(argv[0]);
	return 0;
}

/******************************************************************************/

static int getEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToBoolean(ep, ep->gc.enable);
	return 0;
}

/******************************************************************************/

static int setEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	ep->gc.enable= ejsVarToBoolean(argv[0]);
	return 0;
}

/******************************************************************************/

static int getDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToBoolean(ep, ep->gc.enableDemandCollect);
	return 0;
}

/******************************************************************************/

static int setDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	ep->gc.enableDemandCollect = ejsVarToBoolean(argv[0]);
	return 0;
}

/******************************************************************************/

static int getIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToBoolean(ep, ep->gc.enableIdleCollect);
	return 0;
}

/******************************************************************************/

static int setIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	ep->gc.enableIdleCollect = ejsVarToBoolean(argv[0]);
	return 0;
}

/******************************************************************************/

static int getWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ep, ep->gc.workQuota);
	return 0;
}

/******************************************************************************/

static int setWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	int		quota;

	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	quota = ejsVarToInteger(argv[0]);
	if (quota < EJS_GC_MIN_WORK_QUOTA && quota != 0) {
		ejsArgError(ep, "Bad work quota");
		return -1;
	}

	ep->gc.workQuota = quota;
	return 0;
}

/******************************************************************************/

static int getMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ep, ep->gc.maxMemory);
	return 0;
}

/******************************************************************************/

static int setMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv)
{
	int		maxMemory;

	if (argc != 1) {
		ejsArgError(ep, "Bad arguments");
		return -1;
	}
	maxMemory = ejsVarToInteger(argv[0]);
	if (maxMemory < 0) {
		ejsArgError(ep, "Bad maxMemory");
		return -1;
	}

	ep->gc.maxMemory = maxMemory;
	return 0;
}

/******************************************************************************/
/******************************** Initialization ******************************/
/******************************************************************************/

int ejsDefineGCClass(Ejs *ep)
{
	EjsVar	*gcClass;
	int		flags;

	flags = EJS_NO_LOCAL;

	/*
 	 *	NOTE: We create the GC class and define static methods on it. There 
	 *	is no object instance
	 */
	gcClass =  ejsDefineClass(ep, "System.GC", "Object", 0);
	if (gcClass == 0) {
		return MPR_ERR_CANT_INITIALIZE;
	}

	/*
	 *	MOB -- convert these to properties with accessors when available
	 */
	ejsDefineCMethod(ep, gcClass, "printStats", printStatsProc, flags);
	ejsDefineCMethod(ep, gcClass, "run", runProc, flags);

	ejsDefineCMethod(ep, gcClass, "getUsedMemory", usedMemoryProc, flags);
	ejsDefineCMethod(ep, gcClass, "getAllocatedMemory", allocatedMemoryProc,
		flags);
	ejsDefineCMethod(ep, gcClass, "getMprMemory", mprMemoryProc, flags);
	ejsDefineCMethod(ep, gcClass, "getPeakMprMemory", peakMprMemoryProc, flags);
	ejsDefineCMethod(ep, gcClass, "debug", debugProc, flags);

#if (WIN || BREW_SIMULATOR) && BLD_DEBUG
	ejsDefineCMethod(ep, gcClass, "check", checkProc, flags);
#endif

	ejsDefineCAccessors(ep, gcClass, "debugLevel", 
		getDebugLevel, setDebugLevel, flags);

	ejsDefineCAccessors(ep, gcClass, "enable", 
		getEnable, setEnable, flags);

	ejsDefineCAccessors(ep, gcClass, "demandCollect", 
		getDemandCollect, setDemandCollect, flags);

	ejsDefineCAccessors(ep, gcClass, "idleCollect", 
		getIdleCollect, setIdleCollect, flags);

	ejsDefineCAccessors(ep, gcClass, "workQuota", 
		getWorkQuota, setWorkQuota, flags);

	ejsDefineCAccessors(ep, gcClass, "maxMemory", 
		getMaxMemory, setMaxMemory, flags);

	return ejsObjHasErrors(gcClass) ? MPR_ERR_CANT_INITIALIZE : 0;
}

/******************************************************************************/

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim:tw=78
 * vim600: sw=4 ts=4 fdm=marker
 * vim<600: sw=4 ts=4
 */