Line data Source code
1 : /* src/native/vm/gnuclasspath/java_lang_VMRuntime.cpp
2 :
3 : Copyright (C) 1996-2013
4 : CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5 :
6 : This file is part of CACAO.
7 :
8 : This program is free software; you can redistribute it and/or
9 : modify it under the terms of the GNU General Public License as
10 : published by the Free Software Foundation; either version 2, or (at
11 : your option) any later version.
12 :
13 : This program is distributed in the hope that it will be useful, but
14 : WITHOUT ANY WARRANTY; without even the implied warranty of
15 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 : General Public License for more details.
17 :
18 : You should have received a copy of the GNU General Public License
19 : along with this program; if not, write to the Free Software
20 : Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 : 02110-1301, USA.
22 :
23 : */
24 :
25 :
26 : #include "config.h"
27 :
28 : #include <assert.h>
29 : #include <string.h>
30 : #include <stdint.h>
31 : #include <stdlib.h>
32 : #include <unistd.h>
33 :
34 : #include "mm/memory.hpp"
35 : #include "mm/gc.hpp"
36 :
37 : #include "native/jni.hpp"
38 : #include "native/native.hpp"
39 :
40 : #if defined(ENABLE_JNI_HEADERS)
41 : # include "native/vm/include/java_lang_VMRuntime.h"
42 : #endif
43 :
44 : #include "vm/jit/builtin.hpp"
45 : #include "vm/exceptions.hpp"
46 : #include "vm/os.hpp"
47 : #include "vm/string.hpp"
48 : #include "vm/utf8.hpp"
49 : #include "vm/vm.hpp"
50 :
51 : #include "toolbox/buffer.hpp"
52 :
53 : static bool finalizeOnExit = false;
54 :
55 :
56 : // Native functions are exported as C functions.
57 : extern "C" {
58 :
59 : /*
60 : * Class: java/lang/VMRuntime
61 : * Method: exit
62 : * Signature: (I)V
63 : */
64 141 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
65 : {
66 141 : if (finalizeOnExit)
67 0 : gc_finalize_all();
68 :
69 141 : vm_shutdown(status);
70 0 : }
71 :
72 :
73 : /*
74 : * Class: java/lang/VMRuntime
75 : * Method: freeMemory
76 : * Signature: ()J
77 : */
78 0 : JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
79 : {
80 0 : return gc_get_free_bytes();
81 : }
82 :
83 :
84 : /*
85 : * Class: java/lang/VMRuntime
86 : * Method: totalMemory
87 : * Signature: ()J
88 : */
89 0 : JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
90 : {
91 0 : return gc_get_heap_size();
92 : }
93 :
94 :
95 : /*
96 : * Class: java/lang/VMRuntime
97 : * Method: maxMemory
98 : * Signature: ()J
99 : */
100 1 : JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
101 : {
102 1 : return gc_get_max_heap_size();
103 : }
104 :
105 :
106 : /*
107 : * Class: java/lang/VMRuntime
108 : * Method: gc
109 : * Signature: ()V
110 : */
111 2 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
112 : {
113 2 : gc_call();
114 2 : }
115 :
116 :
117 : /*
118 : * Class: java/lang/VMRuntime
119 : * Method: runFinalization
120 : * Signature: ()V
121 : */
122 1 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
123 : {
124 1 : gc_invoke_finalizers();
125 1 : }
126 :
127 :
128 : /*
129 : * Class: java/lang/VMRuntime
130 : * Method: runFinalizersOnExit
131 : * Signature: (Z)V
132 : */
133 0 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
134 : {
135 : /* XXX threading */
136 :
137 0 : finalizeOnExit = value;
138 0 : }
139 :
140 :
141 : /*
142 : * Class: java/lang/VMRuntime
143 : * Method: runFinalizationsForExit
144 : * Signature: ()V
145 : */
146 141 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
147 : {
148 : /* if (finalizeOnExit) { */
149 : /* gc_call(); */
150 : /* gc_finalize_all(); */
151 : /* } */
152 : /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
153 : /*gc_finalize_all();*/
154 : /*gc_invoke_finalizers();*/
155 : /*gc_call();*/
156 141 : }
157 :
158 :
159 : /*
160 : * Class: java/lang/VMRuntime
161 : * Method: traceInstructions
162 : * Signature: (Z)V
163 : */
164 0 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
165 : {
166 : /* not supported */
167 0 : }
168 :
169 :
170 : /*
171 : * Class: java/lang/VMRuntime
172 : * Method: traceMethodCalls
173 : * Signature: (Z)V
174 : */
175 0 : JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
176 : {
177 : /* not supported */
178 0 : }
179 :
180 :
181 : /*
182 : * Class: java/lang/VMRuntime
183 : * Method: availableProcessors
184 : * Signature: ()I
185 : */
186 0 : JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
187 : {
188 0 : return os::processors_online();
189 : }
190 :
191 :
192 : /*
193 : * Class: java/lang/VMRuntime
194 : * Method: nativeLoad
195 : * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
196 : */
197 593 : JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
198 : {
199 593 : classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
200 :
201 : /* REMOVEME When we use Java-strings internally. */
202 :
203 593 : if (libname == NULL) {
204 0 : exceptions_throw_nullpointerexception();
205 0 : return 0;
206 : }
207 :
208 593 : Utf8String name = JavaString((java_handle_t*) libname).to_utf8();
209 :
210 593 : NativeLibrary library(name, cl);
211 593 : return library.load(env);
212 : }
213 :
214 :
215 : /*
216 : * Class: java/lang/VMRuntime
217 : * Method: mapLibraryName
218 : * Signature: (Ljava/lang/String;)Ljava/lang/String;
219 : */
220 589 : JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
221 : {
222 589 : Utf8String u;
223 : java_handle_t *o;
224 :
225 589 : if (libname == NULL) {
226 0 : exceptions_throw_nullpointerexception();
227 0 : return NULL;
228 : }
229 :
230 589 : u = JavaString((java_handle_t*) libname).to_utf8();
231 :
232 : /* generate library name */
233 :
234 589 : Buffer<> buf;
235 :
236 : buf.write(NATIVE_LIBRARY_PREFIX)
237 : .write(u)
238 589 : .write(NATIVE_LIBRARY_SUFFIX);
239 :
240 589 : o = JavaString::from_utf8(buf.c_str());
241 :
242 589 : return (jstring) o;
243 : }
244 :
245 : } // extern "C"
246 :
247 :
248 : /* native methods implemented by this file ************************************/
249 :
250 : static JNINativeMethod methods[] = {
251 : { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
252 : { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
253 : { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
254 : { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
255 : { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
256 : { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
257 : { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
258 : { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
259 : { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
260 : { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
261 : { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
262 : { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
263 : { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
264 : };
265 :
266 :
267 : /* _Jv_java_lang_VMRuntime_init ************************************************
268 :
269 : Register native functions.
270 :
271 : *******************************************************************************/
272 :
273 163 : void _Jv_java_lang_VMRuntime_init(void)
274 : {
275 163 : Utf8String u = Utf8String::from_utf8("java/lang/VMRuntime");
276 :
277 163 : NativeMethods& nm = VM::get_current()->get_nativemethods();
278 163 : nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
279 163 : }
280 :
281 :
282 : /*
283 : * These are local overrides for various environment variables in Emacs.
284 : * Please do not remove this and leave it at the end of the file, where
285 : * Emacs will automagically detect them.
286 : * ---------------------------------------------------------------------
287 : * Local variables:
288 : * mode: c++
289 : * indent-tabs-mode: t
290 : * c-basic-offset: 4
291 : * tab-width: 4
292 : * End:
293 : * vim:noexpandtab:sw=4:ts=4:
294 : */
|