1 /* src/native/vm/gnuclasspath/java_lang_VMRuntime.cpp
3 Copyright (C) 1996-2005, 2006, 2007, 2008
4 CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
6 This file is part of CACAO.
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.
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.
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
34 #include "mm/memory.hpp"
37 #include "native/jni.hpp"
38 #include "native/native.hpp"
40 #if defined(ENABLE_JNI_HEADERS)
41 # include "native/vm/include/java_lang_VMRuntime.h"
44 #include "vm/jit/builtin.hpp"
45 #include "vm/exceptions.hpp"
47 #include "vm/string.hpp"
52 static bool finalizeOnExit = false;
55 // Native functions are exported as C functions.
59 * Class: java/lang/VMRuntime
63 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
73 * Class: java/lang/VMRuntime
77 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
79 return gc_get_free_bytes();
84 * Class: java/lang/VMRuntime
88 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
90 return gc_get_heap_size();
95 * Class: java/lang/VMRuntime
99 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
101 return gc_get_max_heap_size();
106 * Class: java/lang/VMRuntime
110 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
117 * Class: java/lang/VMRuntime
118 * Method: runFinalization
121 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
123 gc_invoke_finalizers();
128 * Class: java/lang/VMRuntime
129 * Method: runFinalizersOnExit
132 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
136 finalizeOnExit = value;
141 * Class: java/lang/VMRuntime
142 * Method: runFinalizationsForExit
145 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
147 /* if (finalizeOnExit) { */
149 /* gc_finalize_all(); */
151 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
152 /*gc_finalize_all();*/
153 /*gc_invoke_finalizers();*/
159 * Class: java/lang/VMRuntime
160 * Method: traceInstructions
163 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
170 * Class: java/lang/VMRuntime
171 * Method: traceMethodCalls
174 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
181 * Class: java/lang/VMRuntime
182 * Method: availableProcessors
185 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
187 return os::processors_online();
192 * Class: java/lang/VMRuntime
194 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
196 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
198 classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
200 /* REMOVEME When we use Java-strings internally. */
202 if (libname == NULL) {
203 exceptions_throw_nullpointerexception();
207 utf* name = javastring_toutf((java_handle_t *) libname, false);
209 NativeLibrary library(name, cl);
210 return library.load(env);
215 * Class: java/lang/VMRuntime
216 * Method: mapLibraryName
217 * Signature: (Ljava/lang/String;)Ljava/lang/String;
219 JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
226 if (libname == NULL) {
227 exceptions_throw_nullpointerexception();
231 u = javastring_toutf((java_handle_t *) libname, false);
233 /* calculate length of library name */
236 strlen(NATIVE_LIBRARY_PREFIX) +
238 strlen(NATIVE_LIBRARY_SUFFIX) +
241 buffer = MNEW(char, buffer_len);
243 /* generate library name */
245 strcpy(buffer, NATIVE_LIBRARY_PREFIX);
247 strcat(buffer, NATIVE_LIBRARY_SUFFIX);
249 o = javastring_new_from_utf_string(buffer);
253 MFREE(buffer, char, buffer_len);
261 /* native methods implemented by this file ************************************/
263 static JNINativeMethod methods[] = {
264 { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
265 { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
266 { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
267 { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
268 { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
269 { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
270 { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
271 { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
272 { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
273 { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
274 { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
275 { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
276 { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
280 /* _Jv_java_lang_VMRuntime_init ************************************************
282 Register native functions.
284 *******************************************************************************/
286 void _Jv_java_lang_VMRuntime_init(void)
288 utf* u = utf_new_char("java/lang/VMRuntime");
290 NativeMethods& nm = VM::get_current()->get_nativemethods();
291 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
296 * These are local overrides for various environment variables in Emacs.
297 * Please do not remove this and leave it at the end of the file, where
298 * Emacs will automagically detect them.
299 * ---------------------------------------------------------------------
302 * indent-tabs-mode: t
306 * vim:noexpandtab:sw=4:ts=4: