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