* configure.ac: Added check for sys/utsname.h header.
[cacao.git] / src / native / vm / gnuclasspath / java_lang_VMRuntime.cpp
1 /* src/native/vm/gnuclasspath/java_lang_VMRuntime.cpp
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
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.
12
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.
17
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
21    02110-1301, USA.
22
23 */
24
25
26 #include "config.h"
27
28 #include <assert.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33
34 #if defined(__DARWIN__)
35 # if defined(__POWERPC__)
36 #  define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
37 # endif
38 # include <mach/mach.h>
39 #endif
40
41 #include "mm/memory.hpp"
42 #include "mm/gc.hpp"
43
44 #include "native/jni.hpp"
45 #include "native/native.hpp"
46
47 #if defined(ENABLE_JNI_HEADERS)
48 # include "native/vm/include/java_lang_VMRuntime.h"
49 #endif
50
51 #include "vm/jit/builtin.hpp"
52 #include "vm/exceptions.hpp"
53 #include "vm/os.hpp"
54 #include "vm/string.hpp"
55 #include "vm/utf8.h"
56 #include "vm/vm.hpp"
57
58
59 static bool finalizeOnExit = false;
60
61
62 // Native functions are exported as C functions.
63 extern "C" {
64
65 /*
66  * Class:     java/lang/VMRuntime
67  * Method:    exit
68  * Signature: (I)V
69  */
70 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
71 {
72         if (finalizeOnExit)
73                 gc_finalize_all();
74
75         vm_shutdown(status);
76 }
77
78
79 /*
80  * Class:     java/lang/VMRuntime
81  * Method:    freeMemory
82  * Signature: ()J
83  */
84 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
85 {
86         return gc_get_free_bytes();
87 }
88
89
90 /*
91  * Class:     java/lang/VMRuntime
92  * Method:    totalMemory
93  * Signature: ()J
94  */
95 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
96 {
97         return gc_get_heap_size();
98 }
99
100
101 /*
102  * Class:     java/lang/VMRuntime
103  * Method:    maxMemory
104  * Signature: ()J
105  */
106 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
107 {
108         return gc_get_max_heap_size();
109 }
110
111
112 /*
113  * Class:     java/lang/VMRuntime
114  * Method:    gc
115  * Signature: ()V
116  */
117 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
118 {
119         gc_call();
120 }
121
122
123 /*
124  * Class:     java/lang/VMRuntime
125  * Method:    runFinalization
126  * Signature: ()V
127  */
128 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
129 {
130         gc_invoke_finalizers();
131 }
132
133
134 /*
135  * Class:     java/lang/VMRuntime
136  * Method:    runFinalizersOnExit
137  * Signature: (Z)V
138  */
139 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
140 {
141         /* XXX threading */
142
143         finalizeOnExit = value;
144 }
145
146
147 /*
148  * Class:     java/lang/VMRuntime
149  * Method:    runFinalizationsForExit
150  * Signature: ()V
151  */
152 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
153 {
154 /*      if (finalizeOnExit) { */
155 /*              gc_call(); */
156         /* gc_finalize_all(); */
157 /*      } */
158 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
159         /*gc_finalize_all();*/
160         /*gc_invoke_finalizers();*/
161         /*gc_call();*/
162 }
163
164
165 /*
166  * Class:     java/lang/VMRuntime
167  * Method:    traceInstructions
168  * Signature: (Z)V
169  */
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
171 {
172         /* not supported */
173 }
174
175
176 /*
177  * Class:     java/lang/VMRuntime
178  * Method:    traceMethodCalls
179  * Signature: (Z)V
180  */
181 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
182 {
183         /* not supported */
184 }
185
186
187 /*
188  * Class:     java/lang/VMRuntime
189  * Method:    availableProcessors
190  * Signature: ()I
191  */
192 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
193 {
194         return os::processors_online();
195 }
196
197
198 /*
199  * Class:     java/lang/VMRuntime
200  * Method:    nativeLoad
201  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
202  */
203 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
204 {
205         classloader_t* cl = loader_hashtable_classloader_add((java_handle_t *) loader);
206
207         /* REMOVEME When we use Java-strings internally. */
208
209         if (libname == NULL) {
210                 exceptions_throw_nullpointerexception();
211                 return 0;
212         }
213
214         utf* name = javastring_toutf((java_handle_t *) libname, false);
215
216         NativeLibrary library(name, cl);
217         return library.load(env);
218 }
219
220
221 /*
222  * Class:     java/lang/VMRuntime
223  * Method:    mapLibraryName
224  * Signature: (Ljava/lang/String;)Ljava/lang/String;
225  */
226 JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
227 {
228         utf           *u;
229         char          *buffer;
230         int32_t        buffer_len;
231         java_handle_t *o;
232
233         if (libname == NULL) {
234                 exceptions_throw_nullpointerexception();
235                 return NULL;
236         }
237
238         u = javastring_toutf((java_handle_t *) libname, false);
239
240         /* calculate length of library name */
241
242         buffer_len =
243                 strlen(NATIVE_LIBRARY_PREFIX) +
244                 utf_bytes(u) +
245                 strlen(NATIVE_LIBRARY_SUFFIX) +
246                 strlen("0");
247
248         buffer = MNEW(char, buffer_len);
249
250         /* generate library name */
251
252         strcpy(buffer, NATIVE_LIBRARY_PREFIX);
253         utf_cat(buffer, u);
254         strcat(buffer, NATIVE_LIBRARY_SUFFIX);
255
256         o = javastring_new_from_utf_string(buffer);
257
258         /* release memory */
259
260         MFREE(buffer, char, buffer_len);
261
262         return (jstring) o;
263 }
264
265 } // extern "C"
266
267
268 /* native methods implemented by this file ************************************/
269
270 static JNINativeMethod methods[] = {
271         { (char*) "exit",                   (char*) "(I)V",                                         (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit                   },
272         { (char*) "freeMemory",             (char*) "()J",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory             },
273         { (char*) "totalMemory",            (char*) "()J",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory            },
274         { (char*) "maxMemory",              (char*) "()J",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory              },
275         { (char*) "gc",                     (char*) "()V",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc                     },
276         { (char*) "runFinalization",        (char*) "()V",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization        },
277         { (char*) "runFinalizersOnExit",    (char*) "(Z)V",                                         (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit    },
278         { (char*) "runFinalizationForExit", (char*) "()V",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
279         { (char*) "traceInstructions",      (char*) "(Z)V",                                         (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions      },
280         { (char*) "traceMethodCalls",       (char*) "(Z)V",                                         (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls       },
281         { (char*) "availableProcessors",    (char*) "()I",                                          (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors    },
282         { (char*) "nativeLoad",             (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad             },
283         { (char*) "mapLibraryName",         (char*) "(Ljava/lang/String;)Ljava/lang/String;",       (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName         },
284 };
285
286
287 /* _Jv_java_lang_VMRuntime_init ************************************************
288
289    Register native functions.
290
291 *******************************************************************************/
292
293 void _Jv_java_lang_VMRuntime_init(void)
294 {
295         utf* u = utf_new_char("java/lang/VMRuntime");
296
297         NativeMethods& nm = VM::get_current()->get_nativemethods();
298         nm.register_methods(u, methods, NATIVE_METHODS_COUNT);
299 }
300
301
302 /*
303  * These are local overrides for various environment variables in Emacs.
304  * Please do not remove this and leave it at the end of the file, where
305  * Emacs will automagically detect them.
306  * ---------------------------------------------------------------------
307  * Local variables:
308  * mode: c++
309  * indent-tabs-mode: t
310  * c-basic-offset: 4
311  * tab-width: 4
312  * End:
313  * vim:noexpandtab:sw=4:ts=4:
314  */