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 # if defined(__POWERPC__)
37 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
39 # include <mach/mach.h>
42 #include "mm/dumpmemory.h"
43 #include "mm/gc-common.h"
45 #include "native/jni.h"
46 #include "native/native.h"
48 #include "native/include/java_io_File.h"
49 #include "native/include/java_lang_ClassLoader.h"
50 #include "native/include/java_lang_String.h"
51 #include "native/include/java_lang_Process.h"
53 #include "native/include/java_lang_VMRuntime.h"
55 #include "vm/builtin.h"
56 #include "vm/exceptions.h"
57 #include "vm/stringlocal.h"
60 #include "vmcore/system.h"
61 #include "vmcore/utf8.h"
64 /* native methods implemented by this file ************************************/
66 static JNINativeMethod methods[] = {
67 { "exit", "(I)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_exit },
68 { "freeMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_freeMemory },
69 { "totalMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_totalMemory },
70 { "maxMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_maxMemory },
71 { "gc", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_gc },
72 { "runFinalization", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalization },
73 { "runFinalizersOnExit", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
74 { "runFinalizationForExit", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
75 { "traceInstructions", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceInstructions },
76 { "traceMethodCalls", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
77 { "availableProcessors", "()I", (void *) (intptr_t) &Java_java_lang_VMRuntime_availableProcessors },
78 { "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (intptr_t) &Java_java_lang_VMRuntime_nativeLoad },
79 { "mapLibraryName", "(Ljava/lang/String;)Ljava/lang/String;", (void *) (intptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
83 /* _Jv_java_lang_VMRuntime_init ************************************************
85 Register native functions.
87 *******************************************************************************/
89 void _Jv_java_lang_VMRuntime_init(void)
93 u = utf_new_char("java/lang/VMRuntime");
95 native_method_register(u, methods, NATIVE_METHODS_COUNT);
99 static bool finalizeOnExit = false;
103 * Class: java/lang/VMRuntime
107 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
117 * Class: java/lang/VMRuntime
121 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
123 return gc_get_free_bytes();
128 * Class: java/lang/VMRuntime
129 * Method: totalMemory
132 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
134 return gc_get_heap_size();
139 * Class: java/lang/VMRuntime
143 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
145 return gc_get_max_heap_size();
150 * Class: java/lang/VMRuntime
154 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
161 * Class: java/lang/VMRuntime
162 * Method: runFinalization
165 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
167 gc_invoke_finalizers();
172 * Class: java/lang/VMRuntime
173 * Method: runFinalizersOnExit
176 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
180 finalizeOnExit = value;
185 * Class: java/lang/VMRuntime
186 * Method: runFinalizationsForExit
189 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
191 /* if (finalizeOnExit) { */
193 /* gc_finalize_all(); */
195 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
196 /*gc_finalize_all();*/
197 /*gc_invoke_finalizers();*/
203 * Class: java/lang/VMRuntime
204 * Method: traceInstructions
207 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
214 * Class: java/lang/VMRuntime
215 * Method: traceMethodCalls
218 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
225 * Class: java/lang/VMRuntime
226 * Method: availableProcessors
229 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
231 return system_processors_online();
236 * Class: java/lang/VMRuntime
238 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
240 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
245 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
247 /* REMOVEME When we use Java-strings internally. */
249 if (libname == NULL) {
250 exceptions_throw_nullpointerexception();
254 name = javastring_toutf((java_handle_t *) libname, false);
256 return native_library_load(env, name, cl);
261 * Class: java/lang/VMRuntime
262 * Method: mapLibraryName
263 * Signature: (Ljava/lang/String;)Ljava/lang/String;
265 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
273 if (libname == NULL) {
274 exceptions_throw_nullpointerexception();
278 u = javastring_toutf((java_handle_t *) libname, false);
280 /* calculate length of library name */
282 buffer_len = strlen("lib");
284 buffer_len += utf_bytes(u);
286 #if defined(__DARWIN__)
287 buffer_len += strlen(".dylib");
289 buffer_len += strlen(".so");
292 buffer_len += strlen("0");
296 buffer = DMNEW(char, buffer_len);
298 /* generate library name */
300 strcpy(buffer, "lib");
303 #if defined(__DARWIN__)
304 strcat(buffer, ".dylib");
306 strcat(buffer, ".so");
309 o = javastring_new_from_utf_string(buffer);
315 return (java_lang_String *) o;
320 * These are local overrides for various environment variables in Emacs.
321 * Please do not remove this and leave it at the end of the file, where
322 * Emacs will automagically detect them.
323 * ---------------------------------------------------------------------
326 * indent-tabs-mode: t
330 * vim:noexpandtab:sw=4:ts=4: