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
25 $Id: java_lang_VMRuntime.c 7305 2007-02-09 11:08:14Z twisti $
35 #include <sys/utsname.h>
37 #if !defined(WITH_STATIC_CLASSPATH)
41 #if defined(__DARWIN__)
42 # define OS_INLINE /* required for <libkern/ppc/OSByteOrder.h> */
43 # include <mach/mach.h>
48 #include "mm/gc-common.h"
49 #include "mm/memory.h"
51 #include "native/jni.h"
52 #include "native/native.h"
54 #include "native/include/java_io_File.h"
55 #include "native/include/java_lang_ClassLoader.h"
56 #include "native/include/java_lang_String.h"
57 #include "native/include/java_lang_Process.h"
59 #include "native/vm/java_lang_Runtime.h"
61 #include "toolbox/logging.h"
63 #include "vm/builtin.h"
64 #include "vm/exceptions.h"
65 #include "vm/stringlocal.h"
68 #include "vmcore/options.h"
71 /* this should work on BSD */
73 #if defined(__DARWIN__)
74 #include <sys/sysctl.h>
80 * Class: java/lang/VMRuntime
81 * Method: exitInternal
84 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
86 _Jv_java_lang_Runtime_exit(status);
91 * Class: java/lang/VMRuntime
95 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
97 return _Jv_java_lang_Runtime_freeMemory();
102 * Class: java/lang/VMRuntime
103 * Method: totalMemory
106 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
108 return _Jv_java_lang_Runtime_totalMemory();
113 * Class: java/lang/VMRuntime
117 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
119 return gc_get_max_heap_size();
124 * Class: java/lang/VMRuntime
128 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
130 _Jv_java_lang_Runtime_gc();
135 * Class: java/lang/VMRuntime
136 * Method: runFinalization
139 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
141 gc_invoke_finalizers();
146 * Class: java/lang/VMRuntime
147 * Method: runFinalizersOnExit
150 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
154 finalizeOnExit = value;
159 * Class: java/lang/VMRuntime
160 * Method: runFinalizationsForExit
163 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
165 /* if (finalizeOnExit) { */
167 /* gc_finalize_all(); */
169 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
170 /*gc_finalize_all();*/
171 /*gc_invoke_finalizers();*/
177 * Class: java/lang/VMRuntime
178 * Method: traceInstructions
181 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
188 * Class: java/lang/VMRuntime
189 * Method: traceMethodCalls
192 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
199 * Class: java/lang/VMRuntime
200 * Method: availableProcessors
203 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
205 #if defined(_SC_NPROC_ONLN)
206 return (s4) sysconf(_SC_NPROC_ONLN);
208 #elif defined(_SC_NPROCESSORS_ONLN)
209 return (s4) sysconf(_SC_NPROCESSORS_ONLN);
211 #elif defined(__DARWIN__)
212 /* this should work in BSD */
214 int ncpu, mib[2], rc;
220 rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
225 host_basic_info_data_t hinfo;
226 mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
229 rc = host_info(mach_host_self(), HOST_BASIC_INFO,
230 (host_info_t) &hinfo, &hinfo_count);
232 if (rc != KERN_SUCCESS) {
236 return (s4) hinfo.avail_cpus;
245 * Class: java/lang/VMRuntime
247 * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
249 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
251 #if !defined(WITH_STATIC_CLASSPATH)
258 if (filename == NULL) {
259 exceptions_throw_nullpointerexception();
263 #if defined(WITH_STATIC_CLASSPATH)
265 #else /* defined(WITH_STATIC_CLASSPATH) */
266 name = javastring_toutf(filename, 0);
268 /* is the library already loaded? */
270 if (native_hashtable_library_find(name, (java_objectheader *) loader))
273 /* try to open the library */
275 if (!(handle = lt_dlopen(name->text))) {
278 log_print("Java_java_lang_VMRuntime_nativeLoad: ");
279 log_print(lt_dlerror());
286 /* resolve JNI_OnLoad function */
288 if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
289 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
292 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
294 (*env)->GetJavaVM(env, &vm);
296 version = JNI_OnLoad(vm, NULL);
298 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
300 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
307 /* insert the library name into the library hash */
309 native_hashtable_library_add(name, (java_objectheader *) loader, handle);
312 #endif /* defined(WITH_STATIC_CLASSPATH) */
317 * Class: java/lang/VMRuntime
318 * Method: mapLibraryName
319 * Signature: (Ljava/lang/String;)Ljava/lang/String;
321 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
330 exceptions_throw_nullpointerexception();
334 u = javastring_toutf(libname, 0);
336 /* calculate length of library name */
338 buffer_len = strlen("lib");
340 buffer_len += utf_bytes(u);
342 #if defined(__DARWIN__)
343 buffer_len += strlen(".dylib");
345 buffer_len += strlen(".so");
348 buffer_len += strlen("0");
350 dumpsize = dump_size();
351 buffer = DMNEW(char, buffer_len);
353 /* generate library name */
355 strcpy(buffer, "lib");
358 #if defined(__DARWIN__)
359 strcat(buffer, ".dylib");
361 strcat(buffer, ".so");
364 s = javastring_new_from_utf_string(buffer);
368 dump_release(dumpsize);
375 * These are local overrides for various environment variables in Emacs.
376 * Please do not remove this and leave it at the end of the file, where
377 * Emacs will automagically detect them.
378 * ---------------------------------------------------------------------
381 * indent-tabs-mode: t
385 * vim:noexpandtab:sw=4:ts=4: