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