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
33 #include <sys/utsname.h>
35 #if defined(__DARWIN__)
36 # if defined(__POWERPC__)
37 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
39 # include <mach/mach.h>
42 #include "mm/memory.hpp"
45 #include "native/jni.hpp"
46 #include "native/native.hpp"
48 #if defined(ENABLE_JNI_HEADERS)
49 # include "native/vm/include/java_lang_VMRuntime.h"
52 #include "vm/jit/builtin.hpp"
53 #include "vm/exceptions.hpp"
55 #include "vm/string.hpp"
60 static bool finalizeOnExit = false;
63 // Native functions are exported as C functions.
67 * Class: java/lang/VMRuntime
71 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
81 * Class: java/lang/VMRuntime
85 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
87 return gc_get_free_bytes();
92 * Class: java/lang/VMRuntime
96 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
98 return gc_get_heap_size();
103 * Class: java/lang/VMRuntime
107 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
109 return gc_get_max_heap_size();
114 * Class: java/lang/VMRuntime
118 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
125 * Class: java/lang/VMRuntime
126 * Method: runFinalization
129 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
131 gc_invoke_finalizers();
136 * Class: java/lang/VMRuntime
137 * Method: runFinalizersOnExit
140 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
144 finalizeOnExit = value;
149 * Class: java/lang/VMRuntime
150 * Method: runFinalizationsForExit
153 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
155 /* if (finalizeOnExit) { */
157 /* gc_finalize_all(); */
159 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
160 /*gc_finalize_all();*/
161 /*gc_invoke_finalizers();*/
167 * Class: java/lang/VMRuntime
168 * Method: traceInstructions
171 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
178 * Class: java/lang/VMRuntime
179 * Method: traceMethodCalls
182 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
189 * Class: java/lang/VMRuntime
190 * Method: availableProcessors
193 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
195 return os::processors_online();
200 * Class: java/lang/VMRuntime
202 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
204 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
206 classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
208 /* REMOVEME When we use Java-strings internally. */
210 if (libname == NULL) {
211 exceptions_throw_nullpointerexception();
215 utf* name = javastring_toutf((java_handle_t *) libname, false);
217 NativeLibrary library(name, cl);
218 return library.load(env);
223 * Class: java/lang/VMRuntime
224 * Method: mapLibraryName
225 * Signature: (Ljava/lang/String;)Ljava/lang/String;
227 JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
234 if (libname == NULL) {
235 exceptions_throw_nullpointerexception();
239 u = javastring_toutf((java_handle_t *) libname, false);
241 /* calculate length of library name */
244 strlen(NATIVE_LIBRARY_PREFIX) +
246 strlen(NATIVE_LIBRARY_SUFFIX) +
249 buffer = MNEW(char, buffer_len);
251 /* generate library name */
253 strcpy(buffer, NATIVE_LIBRARY_PREFIX);
255 strcat(buffer, NATIVE_LIBRARY_SUFFIX);
257 o = javastring_new_from_utf_string(buffer);
261 MFREE(buffer, char, buffer_len);
269 /* native methods implemented by this file ************************************/
271 static JNINativeMethod methods[] = {
272 { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
273 { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
274 { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
275 { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
276 { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
277 { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
278 { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
279 { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
280 { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
281 { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
282 { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
283 { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
284 { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
288 /* _Jv_java_lang_VMRuntime_init ************************************************
290 Register native functions.
292 *******************************************************************************/
294 void _Jv_java_lang_VMRuntime_init(void)
296 utf* u = utf_new_char("java/lang/VMRuntime");
298 NativeMethods& nm = VM::get_current()->get_nativemethods();
299 nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
304 * These are local overrides for various environment variables in Emacs.
305 * Please do not remove this and leave it at the end of the file, where
306 * Emacs will automagically detect them.
307 * ---------------------------------------------------------------------
310 * indent-tabs-mode: t
314 * vim:noexpandtab:sw=4:ts=4: