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