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