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 #if defined(__DARWIN__)
35 # if defined(__POWERPC__)
36 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
38 # include <mach/mach.h>
41 #include "mm/memory.hpp"
44 #include "native/jni.hpp"
45 #include "native/native.hpp"
47 #if defined(ENABLE_JNI_HEADERS)
48 # include "native/vm/include/java_lang_VMRuntime.h"
51 #include "vm/jit/builtin.hpp"
52 #include "vm/exceptions.hpp"
54 #include "vm/string.hpp"
59 static bool finalizeOnExit = false;
62 // Native functions are exported as C functions.
66 * Class: java/lang/VMRuntime
70 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
80 * Class: java/lang/VMRuntime
84 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
86 return gc_get_free_bytes();
91 * Class: java/lang/VMRuntime
95 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
97 return gc_get_heap_size();
102 * Class: java/lang/VMRuntime
106 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
108 return gc_get_max_heap_size();
113 * Class: java/lang/VMRuntime
117 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
124 * Class: java/lang/VMRuntime
125 * Method: runFinalization
128 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
130 gc_invoke_finalizers();
135 * Class: java/lang/VMRuntime
136 * Method: runFinalizersOnExit
139 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
143 finalizeOnExit = value;
148 * Class: java/lang/VMRuntime
149 * Method: runFinalizationsForExit
152 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
154 /* if (finalizeOnExit) { */
156 /* gc_finalize_all(); */
158 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
159 /*gc_finalize_all();*/
160 /*gc_invoke_finalizers();*/
166 * Class: java/lang/VMRuntime
167 * Method: traceInstructions
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
177 * Class: java/lang/VMRuntime
178 * Method: traceMethodCalls
181 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
188 * Class: java/lang/VMRuntime
189 * Method: availableProcessors
192 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
194 return os::processors_online();
199 * Class: java/lang/VMRuntime
201 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
203 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
205 classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
207 /* REMOVEME When we use Java-strings internally. */
209 if (libname == NULL) {
210 exceptions_throw_nullpointerexception();
214 utf* name = javastring_toutf((java_handle_t *) libname, false);
216 NativeLibrary library(name, cl);
217 return library.load(env);
222 * Class: java/lang/VMRuntime
223 * Method: mapLibraryName
224 * Signature: (Ljava/lang/String;)Ljava/lang/String;
226 JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
233 if (libname == NULL) {
234 exceptions_throw_nullpointerexception();
238 u = javastring_toutf((java_handle_t *) libname, false);
240 /* calculate length of library name */
243 strlen(NATIVE_LIBRARY_PREFIX) +
245 strlen(NATIVE_LIBRARY_SUFFIX) +
248 buffer = MNEW(char, buffer_len);
250 /* generate library name */
252 strcpy(buffer, NATIVE_LIBRARY_PREFIX);
254 strcat(buffer, NATIVE_LIBRARY_SUFFIX);
256 o = javastring_new_from_utf_string(buffer);
260 MFREE(buffer, char, buffer_len);
268 /* native methods implemented by this file ************************************/
270 static JNINativeMethod methods[] = {
271 { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
272 { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
273 { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
274 { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
275 { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
276 { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
277 { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
278 { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
279 { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
280 { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
281 { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
282 { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
283 { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
287 /* _Jv_java_lang_VMRuntime_init ************************************************
289 Register native functions.
291 *******************************************************************************/
293 void _Jv_java_lang_VMRuntime_init(void)
295 utf* u = utf_new_char("java/lang/VMRuntime");
297 NativeMethods& nm = VM::get_current()->get_nativemethods();
298 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
303 * These are local overrides for various environment variables in Emacs.
304 * Please do not remove this and leave it at the end of the file, where
305 * Emacs will automagically detect them.
306 * ---------------------------------------------------------------------
309 * indent-tabs-mode: t
313 * vim:noexpandtab:sw=4:ts=4: