summaryrefslogtreecommitdiff
path: root/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemMemory.c
blob: d10787b1b43669e7da8023606fb54da137407d84 (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
/*
 *	@file 	ejsSystemMemory.c
 *	@brief 	System.Memory class
 */
/********************************** Copyright *********************************/
/*
 *	Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved.
 */
/********************************** Includes **********************************/

#include	"ejs.h"

/****************************** Forward Declarations***************************/

static uint getUsedMemory(Ejs *ejs);

/******************************************************************************/
/*********************************** Methods *********************************/
/******************************************************************************/

static int getUsedMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ejs, getUsedMemory(ejs));
	return 0;
}

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

static int getUsedStackProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv)
{
	ejsSetReturnValueToInteger(ejs, mprStackSize(ejs));
	return 0;
}

/******************************************************************************/
/*
 *	Public function
 */

uint ejsGetAvailableMemory(Ejs *ejs)
{
	EjsVar			*memoryClass;
	uint			ram;

	memoryClass =  ejsGetClass(ejs, 0, "System.Memory");

	ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram");
	return ram - getUsedMemory(ejs);
}

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

static int getAvailableMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc, 
	EjsVar **argv)
{
	EjsVar			*memoryClass;
	uint			ram;

	memoryClass = ejsGetClass(ejs, 0, "System.Memory");

	ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram");
#if BREW
	ejsSetReturnValueToInteger(ejs, ram - getUsedMemory(ejs));
#else
	ejsSetReturnValueToInteger(ejs, 0);
#endif
	return 0;
}

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

static uint getUsedMemory(Ejs *ejs)
{
#if BREW
	MprApp			*app;
	IHeap			*heap;
	uint			memInUse;
	void			*ptr;

	app = mprGetApp(ejs);
	ptr = (void*) &heap;
	if (ISHELL_CreateInstance(app->shell, AEECLSID_HEAP, (void**) ptr) 
			== SUCCESS) {
		memInUse = IHEAP_GetMemStats(heap);
		IHEAP_Release(heap); 
	} else {
		memInUse = 0;
	}

	return memInUse;
#else
	return 0;
#endif
}

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

int ejsDefineMemoryClass(Ejs *ejs)
{
	EjsVar			*memoryClass;
	uint			used;

#if BREW
	MprApp			*app;
	AEEDeviceInfo	*info;

	/*
	 * Get needed information for class properties.
	 */
	info = mprAllocType(ejs, AEEDeviceInfo);
	if (info == 0) {
		return MPR_ERR_CANT_ALLOCATE;
	}
	info->wStructSize = sizeof(AEEDeviceInfo);
	app = mprGetApp(ejs);
	ISHELL_GetDeviceInfo(app->shell, info);
	used = getUsedMemory(ejs);
#else
	used = 0;
#endif

	/*
	 *	Create the class
	 */
	memoryClass =  ejsDefineClass(ejs, "System.Memory", "Object", 0);
	if (memoryClass == 0) {
		return MPR_ERR_CANT_INITIALIZE;
	}

	/*
	 *	Define the class methods
	 *	MOB -- change to accessors
	 */
	ejsDefineCMethod(ejs, memoryClass, "getUsedStack", getUsedStackProc, 
		EJS_NO_LOCAL);
	ejsDefineCMethod(ejs, memoryClass, "getUsedMemory", getUsedMemoryProc, 
		EJS_NO_LOCAL);
	ejsDefineCMethod(ejs, memoryClass, "getAvailableMemory", 
		getAvailableMemoryProc, EJS_NO_LOCAL);

	/*
	 *	Define properties
	 */
#if BREW
	ejsSetPropertyToInteger(ejs, memoryClass, "ram", info->dwRAM);

#if UNUSED
	/* MOB -- delete this */
	ejsSetPropertyToInteger(ejs, memoryClass, "available", 
		info->dwRAM - used);
#endif
#endif

#if UNUSED
	ejsSetPropertyToInteger(ejs, memoryClass, "used", used);
	ejsSetPropertyToInteger(ejs, memoryClass, "flash", 0);
#endif

	return ejsObjHasErrors(memoryClass) ? 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
 */