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/dumpmemory.h"
45 #include "native/jni.h"
46 #include "native/native.h"
48 #include "native/include/java_lang_ClassLoader.h"
49 #include "native/include/java_lang_String.h"
50 #include "native/include/java_lang_Process.h"
54 #include "native/include/java_lang_VMRuntime.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.hpp"
59 #include "vm/string.hpp"
62 #include "vmcore/os.hpp"
63 #include "vmcore/utf8.h"
66 static bool finalizeOnExit = false;
69 // Native functions are exported as C functions.
73 * Class: java/lang/VMRuntime
77 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
87 * Class: java/lang/VMRuntime
91 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
93 return gc_get_free_bytes();
98 * Class: java/lang/VMRuntime
102 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
104 return gc_get_heap_size();
109 * Class: java/lang/VMRuntime
113 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
115 return gc_get_max_heap_size();
120 * Class: java/lang/VMRuntime
124 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
131 * Class: java/lang/VMRuntime
132 * Method: runFinalization
135 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
137 gc_invoke_finalizers();
142 * Class: java/lang/VMRuntime
143 * Method: runFinalizersOnExit
146 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
150 finalizeOnExit = value;
155 * Class: java/lang/VMRuntime
156 * Method: runFinalizationsForExit
159 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
161 /* if (finalizeOnExit) { */
163 /* gc_finalize_all(); */
165 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
166 /*gc_finalize_all();*/
167 /*gc_invoke_finalizers();*/
173 * Class: java/lang/VMRuntime
174 * Method: traceInstructions
177 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
184 * Class: java/lang/VMRuntime
185 * Method: traceMethodCalls
188 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
195 * Class: java/lang/VMRuntime
196 * Method: availableProcessors
199 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
201 return os::processors_online();
206 * Class: java/lang/VMRuntime
208 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
210 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
215 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
217 /* REMOVEME When we use Java-strings internally. */
219 if (libname == NULL) {
220 exceptions_throw_nullpointerexception();
224 name = javastring_toutf((java_handle_t *) libname, false);
226 return native_library_load(env, name, cl);
231 * Class: java/lang/VMRuntime
232 * Method: mapLibraryName
233 * Signature: (Ljava/lang/String;)Ljava/lang/String;
235 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
243 if (libname == NULL) {
244 exceptions_throw_nullpointerexception();
248 u = javastring_toutf((java_handle_t *) libname, false);
250 /* calculate length of library name */
253 strlen(NATIVE_LIBRARY_PREFIX) +
255 strlen(NATIVE_LIBRARY_SUFFIX) +
260 buffer = DMNEW(char, buffer_len);
262 /* generate library name */
264 strcpy(buffer, NATIVE_LIBRARY_PREFIX);
266 strcat(buffer, NATIVE_LIBRARY_SUFFIX);
268 o = javastring_new_from_utf_string(buffer);
274 return (java_lang_String *) o;
280 /* native methods implemented by this file ************************************/
282 static JNINativeMethod methods[] = {
283 { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
284 { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
285 { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
286 { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
287 { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
288 { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
289 { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
290 { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
291 { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
292 { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
293 { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
294 { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
295 { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
299 /* _Jv_java_lang_VMRuntime_init ************************************************
301 Register native functions.
303 *******************************************************************************/
307 void _Jv_java_lang_VMRuntime_init(void)
311 u = utf_new_char("java/lang/VMRuntime");
313 native_method_register(u, methods, NATIVE_METHODS_COUNT);
319 * These are local overrides for various environment variables in Emacs.
320 * Please do not remove this and leave it at the end of the file, where
321 * Emacs will automagically detect them.
322 * ---------------------------------------------------------------------
325 * indent-tabs-mode: t
329 * vim:noexpandtab:sw=4:ts=4: