1 /* src/native/vm/gnu/java_lang_VMRuntime.c
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 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
37 # include <mach/mach.h>
40 #include "mm/dumpmemory.h"
41 #include "mm/gc-common.h"
43 #include "native/jni.h"
44 #include "native/native.h"
46 #include "native/include/java_io_File.h"
47 #include "native/include/java_lang_ClassLoader.h"
48 #include "native/include/java_lang_String.h"
49 #include "native/include/java_lang_Process.h"
51 #include "native/include/java_lang_VMRuntime.h"
53 #include "vm/builtin.h"
54 #include "vm/exceptions.h"
55 #include "vm/stringlocal.h"
58 #include "vmcore/system.h"
59 #include "vmcore/utf8.h"
62 /* native methods implemented by this file ************************************/
64 static JNINativeMethod methods[] = {
65 { "exit", "(I)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_exit },
66 { "freeMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_freeMemory },
67 { "totalMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_totalMemory },
68 { "maxMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_maxMemory },
69 { "gc", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_gc },
70 { "runFinalization", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalization },
71 { "runFinalizersOnExit", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
72 { "runFinalizationForExit", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
73 { "traceInstructions", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceInstructions },
74 { "traceMethodCalls", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
75 { "availableProcessors", "()I", (void *) (intptr_t) &Java_java_lang_VMRuntime_availableProcessors },
76 { "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (intptr_t) &Java_java_lang_VMRuntime_nativeLoad },
77 { "mapLibraryName", "(Ljava/lang/String;)Ljava/lang/String;", (void *) (intptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
81 /* _Jv_java_lang_VMRuntime_init ************************************************
83 Register native functions.
85 *******************************************************************************/
87 void _Jv_java_lang_VMRuntime_init(void)
91 u = utf_new_char("java/lang/VMRuntime");
93 native_method_register(u, methods, NATIVE_METHODS_COUNT);
97 static bool finalizeOnExit = false;
101 * Class: java/lang/VMRuntime
105 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
115 * Class: java/lang/VMRuntime
119 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
121 return gc_get_free_bytes();
126 * Class: java/lang/VMRuntime
127 * Method: totalMemory
130 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
132 return gc_get_heap_size();
137 * Class: java/lang/VMRuntime
141 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
143 return gc_get_max_heap_size();
148 * Class: java/lang/VMRuntime
152 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
159 * Class: java/lang/VMRuntime
160 * Method: runFinalization
163 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
165 gc_invoke_finalizers();
170 * Class: java/lang/VMRuntime
171 * Method: runFinalizersOnExit
174 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
178 finalizeOnExit = value;
183 * Class: java/lang/VMRuntime
184 * Method: runFinalizationsForExit
187 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
189 /* if (finalizeOnExit) { */
191 /* gc_finalize_all(); */
193 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
194 /*gc_finalize_all();*/
195 /*gc_invoke_finalizers();*/
201 * Class: java/lang/VMRuntime
202 * Method: traceInstructions
205 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
212 * Class: java/lang/VMRuntime
213 * Method: traceMethodCalls
216 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
223 * Class: java/lang/VMRuntime
224 * Method: availableProcessors
227 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
229 return system_processors_online();
234 * Class: java/lang/VMRuntime
236 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
238 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
243 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
245 /* REMOVEME When we use Java-strings internally. */
247 if (libname == NULL) {
248 exceptions_throw_nullpointerexception();
252 name = javastring_toutf((java_handle_t *) libname, false);
254 return native_library_load(env, name, cl);
259 * Class: java/lang/VMRuntime
260 * Method: mapLibraryName
261 * Signature: (Ljava/lang/String;)Ljava/lang/String;
263 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
271 if (libname == NULL) {
272 exceptions_throw_nullpointerexception();
276 u = javastring_toutf((java_handle_t *) libname, false);
278 /* calculate length of library name */
280 buffer_len = strlen("lib");
282 buffer_len += utf_bytes(u);
284 #if defined(__DARWIN__)
285 buffer_len += strlen(".dylib");
287 buffer_len += strlen(".so");
290 buffer_len += strlen("0");
294 buffer = DMNEW(char, buffer_len);
296 /* generate library name */
298 strcpy(buffer, "lib");
301 #if defined(__DARWIN__)
302 strcat(buffer, ".dylib");
304 strcat(buffer, ".so");
307 o = javastring_new_from_utf_string(buffer);
313 return (java_lang_String *) o;
318 * These are local overrides for various environment variables in Emacs.
319 * Please do not remove this and leave it at the end of the file, where
320 * Emacs will automagically detect them.
321 * ---------------------------------------------------------------------
324 * indent-tabs-mode: t
328 * vim:noexpandtab:sw=4:ts=4: