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