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