1 /* src/native/vm/gnu/java_lang_VMRuntime.c
3 Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4 C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5 E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6 J. Wenninger, Institut f. Computersprachen - TU Wien
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
35 #include <sys/utsname.h>
37 #if defined(__DARWIN__)
38 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
39 # include <mach/mach.h>
42 #include "mm/gc-common.h"
43 #include "mm/memory.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 "native/vm/java_lang_Runtime.h"
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/stringlocal.h"
62 #include "vmcore/system.h"
63 #include "vmcore/utf8.h"
66 /* native methods implemented by this file ************************************/
68 static JNINativeMethod methods[] = {
69 { "exit", "(I)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_exit },
70 { "freeMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_freeMemory },
71 { "totalMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_totalMemory },
72 { "maxMemory", "()J", (void *) (intptr_t) &Java_java_lang_VMRuntime_maxMemory },
73 { "gc", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_gc },
74 { "runFinalization", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalization },
75 { "runFinalizersOnExit", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
76 { "runFinalizationForExit", "()V", (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
77 { "traceInstructions", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceInstructions },
78 { "traceMethodCalls", "(Z)V", (void *) (intptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
79 { "availableProcessors", "()I", (void *) (intptr_t) &Java_java_lang_VMRuntime_availableProcessors },
80 { "nativeLoad", "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (intptr_t) &Java_java_lang_VMRuntime_nativeLoad },
81 { "mapLibraryName", "(Ljava/lang/String;)Ljava/lang/String;", (void *) (intptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
85 /* _Jv_java_lang_VMRuntime_init ************************************************
87 Register native functions.
89 *******************************************************************************/
91 void _Jv_java_lang_VMRuntime_init(void)
95 u = utf_new_char("java/lang/VMRuntime");
97 native_method_register(u, methods, NATIVE_METHODS_COUNT);
102 * Class: java/lang/VMRuntime
106 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
108 _Jv_java_lang_Runtime_exit(status);
113 * Class: java/lang/VMRuntime
117 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
119 return _Jv_java_lang_Runtime_freeMemory();
124 * Class: java/lang/VMRuntime
125 * Method: totalMemory
128 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
130 return _Jv_java_lang_Runtime_totalMemory();
135 * Class: java/lang/VMRuntime
139 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
141 return gc_get_max_heap_size();
146 * Class: java/lang/VMRuntime
150 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
152 _Jv_java_lang_Runtime_gc();
157 * Class: java/lang/VMRuntime
158 * Method: runFinalization
161 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
163 gc_invoke_finalizers();
168 * Class: java/lang/VMRuntime
169 * Method: runFinalizersOnExit
172 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
174 _Jv_java_lang_Runtime_runFinalizersOnExit(value);
179 * Class: java/lang/VMRuntime
180 * Method: runFinalizationsForExit
183 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
185 /* if (finalizeOnExit) { */
187 /* gc_finalize_all(); */
189 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
190 /*gc_finalize_all();*/
191 /*gc_invoke_finalizers();*/
197 * Class: java/lang/VMRuntime
198 * Method: traceInstructions
201 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
208 * Class: java/lang/VMRuntime
209 * Method: traceMethodCalls
212 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
219 * Class: java/lang/VMRuntime
220 * Method: availableProcessors
223 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
225 return system_processors_online();
230 * Class: java/lang/VMRuntime
232 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
234 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
238 cl = loader_hashtable_classloader_add((java_handle_t *) loader);
240 #if defined(ENABLE_JNI)
241 return _Jv_java_lang_Runtime_loadLibrary(env, libname, cl);
243 return _Jv_java_lang_Runtime_loadLibrary(libname, cl);
249 * Class: java/lang/VMRuntime
250 * Method: mapLibraryName
251 * Signature: (Ljava/lang/String;)Ljava/lang/String;
253 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
261 if (libname == NULL) {
262 exceptions_throw_nullpointerexception();
266 u = javastring_toutf((java_handle_t *) libname, false);
268 /* calculate length of library name */
270 buffer_len = strlen("lib");
272 buffer_len += utf_bytes(u);
274 #if defined(__DARWIN__)
275 buffer_len += strlen(".dylib");
277 buffer_len += strlen(".so");
280 buffer_len += strlen("0");
282 dumpsize = dump_size();
283 buffer = DMNEW(char, buffer_len);
285 /* generate library name */
287 strcpy(buffer, "lib");
290 #if defined(__DARWIN__)
291 strcat(buffer, ".dylib");
293 strcat(buffer, ".so");
296 o = javastring_new_from_utf_string(buffer);
300 dump_release(dumpsize);
302 return (java_lang_String *) o;
307 * These are local overrides for various environment variables in Emacs.
308 * Please do not remove this and leave it at the end of the file, where
309 * Emacs will automagically detect them.
310 * ---------------------------------------------------------------------
313 * indent-tabs-mode: t
317 * vim:noexpandtab:sw=4:ts=4: