CACAO
java_lang_VMRuntime.cpp
Go to the documentation of this file.
1 /* src/native/vm/gnuclasspath/java_lang_VMRuntime.cpp
2 
3  Copyright (C) 1996-2013
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.hpp"
49 #include "vm/vm.hpp"
50 
51 #include "toolbox/buffer.hpp"
52 
53 static bool finalizeOnExit = false;
54 
55 
56 // Native functions are exported as C functions.
57 extern "C" {
58 
59 /*
60  * Class: java/lang/VMRuntime
61  * Method: exit
62  * Signature: (I)V
63  */
64 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
65 {
66  if (finalizeOnExit)
68 
69  vm_shutdown(status);
70 }
71 
72 
73 /*
74  * Class: java/lang/VMRuntime
75  * Method: freeMemory
76  * Signature: ()J
77  */
78 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
79 {
80  return gc_get_free_bytes();
81 }
82 
83 
84 /*
85  * Class: java/lang/VMRuntime
86  * Method: totalMemory
87  * Signature: ()J
88  */
89 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
90 {
91  return gc_get_heap_size();
92 }
93 
94 
95 /*
96  * Class: java/lang/VMRuntime
97  * Method: maxMemory
98  * Signature: ()J
99  */
100 JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
101 {
102  return gc_get_max_heap_size();
103 }
104 
105 
106 /*
107  * Class: java/lang/VMRuntime
108  * Method: gc
109  * Signature: ()V
110  */
111 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
112 {
113  gc_call();
114 }
115 
116 
117 /*
118  * Class: java/lang/VMRuntime
119  * Method: runFinalization
120  * Signature: ()V
121  */
122 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
123 {
125 }
126 
127 
128 /*
129  * Class: java/lang/VMRuntime
130  * Method: runFinalizersOnExit
131  * Signature: (Z)V
132  */
133 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
134 {
135  /* XXX threading */
136 
137  finalizeOnExit = value;
138 }
139 
140 
141 /*
142  * Class: java/lang/VMRuntime
143  * Method: runFinalizationsForExit
144  * Signature: ()V
145  */
147 {
148 /* if (finalizeOnExit) { */
149 /* gc_call(); */
150  /* gc_finalize_all(); */
151 /* } */
152 /* log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
153  /*gc_finalize_all();*/
154  /*gc_invoke_finalizers();*/
155  /*gc_call();*/
156 }
157 
158 
159 /*
160  * Class: java/lang/VMRuntime
161  * Method: traceInstructions
162  * Signature: (Z)V
163  */
164 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
165 {
166  /* not supported */
167 }
168 
169 
170 /*
171  * Class: java/lang/VMRuntime
172  * Method: traceMethodCalls
173  * Signature: (Z)V
174  */
175 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
176 {
177  /* not supported */
178 }
179 
180 
181 /*
182  * Class: java/lang/VMRuntime
183  * Method: availableProcessors
184  * Signature: ()I
185  */
186 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
187 {
188  return os::processors_online();
189 }
190 
191 
192 /*
193  * Class: java/lang/VMRuntime
194  * Method: nativeLoad
195  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
196  */
197 JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
198 {
200 
201  /* REMOVEME When we use Java-strings internally. */
202 
203  if (libname == NULL) {
205  return 0;
206  }
207 
209 
210  NativeLibrary library(name, cl);
211  return library.load(env);
212 }
213 
214 
215 /*
216  * Class: java/lang/VMRuntime
217  * Method: mapLibraryName
218  * Signature: (Ljava/lang/String;)Ljava/lang/String;
219  */
220 JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
221 {
222  Utf8String u;
223  java_handle_t *o;
224 
225  if (libname == NULL) {
227  return NULL;
228  }
229 
230  u = JavaString((java_handle_t*) libname).to_utf8();
231 
232  /* generate library name */
233 
234  Buffer<> buf;
235 
237  .write(u)
239 
240  o = JavaString::from_utf8(buf.c_str());
241 
242  return (jstring) o;
243 }
244 
245 } // extern "C"
246 
247 
248 /* native methods implemented by this file ************************************/
249 
250 static JNINativeMethod methods[] = {
251  { (char*) "exit", (char*) "(I)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_exit },
252  { (char*) "freeMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_freeMemory },
253  { (char*) "totalMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_totalMemory },
254  { (char*) "maxMemory", (char*) "()J", (void*) (uintptr_t) &Java_java_lang_VMRuntime_maxMemory },
255  { (char*) "gc", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_gc },
256  { (char*) "runFinalization", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalization },
257  { (char*) "runFinalizersOnExit", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit },
258  { (char*) "runFinalizationForExit", (char*) "()V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
259  { (char*) "traceInstructions", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceInstructions },
260  { (char*) "traceMethodCalls", (char*) "(Z)V", (void*) (uintptr_t) &Java_java_lang_VMRuntime_traceMethodCalls },
261  { (char*) "availableProcessors", (char*) "()I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_availableProcessors },
262  { (char*) "nativeLoad", (char*) "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void*) (uintptr_t) &Java_java_lang_VMRuntime_nativeLoad },
263  { (char*) "mapLibraryName", (char*) "(Ljava/lang/String;)Ljava/lang/String;", (void*) (uintptr_t) &Java_java_lang_VMRuntime_mapLibraryName },
264 };
265 
266 
267 /* _Jv_java_lang_VMRuntime_init ************************************************
268 
269  Register native functions.
270 
271 *******************************************************************************/
272 
274 {
275  Utf8String u = Utf8String::from_utf8("java/lang/VMRuntime");
276 
279 }
280 
281 
282 /*
283  * These are local overrides for various environment variables in Emacs.
284  * Please do not remove this and leave it at the end of the file, where
285  * Emacs will automagically detect them.
286  * ---------------------------------------------------------------------
287  * Local variables:
288  * mode: c++
289  * indent-tabs-mode: t
290  * c-basic-offset: 4
291  * tab-width: 4
292  * End:
293  * vim:noexpandtab:sw=4:ts=4:
294  */
void gc_call(void)
Definition: gc.c:492
static int processors_online()
Returns the number of online processors in the system.
Definition: os.cpp:278
Table containing all native methods registered with the VM.
Definition: native.hpp:132
NativeMethods & get_nativemethods()
Definition: vm.hpp:128
void register_methods(Utf8String classname, const JNINativeMethod *methods, size_t count)
Register native methods with the VM.
Definition: native.cpp:242
JNIEXPORT jstring JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, jstring libname)
JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
Utf8String to_utf8() const
Definition: string.cpp:437
void _Jv_java_lang_VMRuntime_init(void)
_Jv_JNIEnv JNIEnv
Definition: jni.hpp:112
#define NATIVE_METHODS_COUNT
Definition: native.hpp:45
static JavaString from_utf8(Utf8String)
Definition: string.cpp:184
const char * c_str()
get contents of buffer as zero-terminated c-style-string This strings lifetime is tied to it&#39;s buffer...
Definition: buffer.hpp:489
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, jboolean value)
JNIEnv jclass jobject const char * name
Definition: jvmti.h:312
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, jboolean par1)
void vm_shutdown()
Hook point before the VM is actually destroyed.
Definition: hook.hpp:168
s8 gc_get_free_bytes(void)
Definition: gc.c:553
#define NATIVE_LIBRARY_PREFIX
Definition: native.hpp:48
s8 gc_get_max_heap_size(void)
Definition: gc.c:555
classloader_t * loader_hashtable_classloader_add(java_handle_t *cl)
Definition: loader.cpp:305
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
void gc_invoke_finalizers(void)
Definition: gc.c:513
bool load(JNIEnv *env)
Load this native library and initialize it, if possible.
Definition: native.cpp:469
void gc_finalize_all(void)
Definition: gc.c:535
JNIEXPORT jint JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, jstring libname, jobject loader)
void exceptions_throw_nullpointerexception(void)
JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
static bool finalizeOnExit
static Utf8String from_utf8(const char *, size_t)
Definition: utf8.cpp:335
JNIEnv jclass jobject loader
Definition: jvmti.h:312
Represents a native library.
Definition: native.hpp:60
static JNINativeMethod methods[]
JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
#define NATIVE_LIBRARY_SUFFIX
Definition: native.hpp:53
s8 gc_get_heap_size(void)
Definition: gc.c:552
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
Buffer & write(char)
Definition: buffer.hpp:280
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, jint status)
JNIEXPORT jlong JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, jboolean par1)
static VM * get_current()
Definition: vm.hpp:99