* src/vm/system.c: Removed.
[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 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2, or (at
13    your option) any later version.
14
15    This program is distributed in the hope that it will be useful, but
16    WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #include "config.h"
29
30 #include <assert.h>
31 #include <string.h>
32 #include <stdint.h>
33 #include <stdlib.h>
34 #include <unistd.h>
35 #include <sys/utsname.h>
36
37 #if defined(__DARWIN__)
38 # define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
39 # include <mach/mach.h>
40 #endif
41
42 #include "mm/gc-common.h"
43 #include "mm/memory.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 "native/vm/java_lang_Runtime.h"
56
57 #include "vm/builtin.h"
58 #include "vm/exceptions.h"
59 #include "vm/stringlocal.h"
60 #include "vm/vm.h"
61
62 #include "vmcore/system.h"
63 #include "vmcore/utf8.h"
64
65
66 /* native methods implemented by this file ************************************/
67
68 static JNINativeMethod methods[] = {
69         { "exit",                   "(I)V",                                         (void *) (intptr_t) &Java_java_lang_VMRuntime_exit                   },
70         { "freeMemory",             "()J",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_freeMemory             },
71         { "totalMemory",            "()J",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_totalMemory            },
72         { "maxMemory",              "()J",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_maxMemory              },
73         { "gc",                     "()V",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_gc                     },
74         { "runFinalization",        "()V",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalization        },
75         { "runFinalizersOnExit",    "(Z)V",                                         (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizersOnExit    },
76         { "runFinalizationForExit", "()V",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_runFinalizationForExit },
77         { "traceInstructions",      "(Z)V",                                         (void *) (intptr_t) &Java_java_lang_VMRuntime_traceInstructions      },
78         { "traceMethodCalls",       "(Z)V",                                         (void *) (intptr_t) &Java_java_lang_VMRuntime_traceMethodCalls       },
79         { "availableProcessors",    "()I",                                          (void *) (intptr_t) &Java_java_lang_VMRuntime_availableProcessors    },
80         { "nativeLoad",             "(Ljava/lang/String;Ljava/lang/ClassLoader;)I", (void *) (intptr_t) &Java_java_lang_VMRuntime_nativeLoad             },
81         { "mapLibraryName",         "(Ljava/lang/String;)Ljava/lang/String;",       (void *) (intptr_t) &Java_java_lang_VMRuntime_mapLibraryName         },
82 };
83
84
85 /* _Jv_java_lang_VMRuntime_init ************************************************
86
87    Register native functions.
88
89 *******************************************************************************/
90
91 void _Jv_java_lang_VMRuntime_init(void)
92 {
93         utf *u;
94
95         u = utf_new_char("java/lang/VMRuntime");
96
97         native_method_register(u, methods, NATIVE_METHODS_COUNT);
98 }
99
100
101 /*
102  * Class:     java/lang/VMRuntime
103  * Method:    exit
104  * Signature: (I)V
105  */
106 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, int32_t status)
107 {
108         _Jv_java_lang_Runtime_exit(status);
109 }
110
111
112 /*
113  * Class:     java/lang/VMRuntime
114  * Method:    freeMemory
115  * Signature: ()J
116  */
117 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
118 {
119         return _Jv_java_lang_Runtime_freeMemory();
120 }
121
122
123 /*
124  * Class:     java/lang/VMRuntime
125  * Method:    totalMemory
126  * Signature: ()J
127  */
128 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
129 {
130         return _Jv_java_lang_Runtime_totalMemory();
131 }
132
133
134 /*
135  * Class:     java/lang/VMRuntime
136  * Method:    maxMemory
137  * Signature: ()J
138  */
139 JNIEXPORT int64_t JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
140 {
141         return gc_get_max_heap_size();
142 }
143
144
145 /*
146  * Class:     java/lang/VMRuntime
147  * Method:    gc
148  * Signature: ()V
149  */
150 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
151 {
152         _Jv_java_lang_Runtime_gc();
153 }
154
155
156 /*
157  * Class:     java/lang/VMRuntime
158  * Method:    runFinalization
159  * Signature: ()V
160  */
161 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
162 {
163         gc_invoke_finalizers();
164 }
165
166
167 /*
168  * Class:     java/lang/VMRuntime
169  * Method:    runFinalizersOnExit
170  * Signature: (Z)V
171  */
172 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, int32_t value)
173 {
174         _Jv_java_lang_Runtime_runFinalizersOnExit(value);
175 }
176
177
178 /*
179  * Class:     java/lang/VMRuntime
180  * Method:    runFinalizationsForExit
181  * Signature: ()V
182  */
183 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
184 {
185 /*      if (finalizeOnExit) { */
186 /*              gc_call(); */
187         /* gc_finalize_all(); */
188 /*      } */
189 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
190         /*gc_finalize_all();*/
191         /*gc_invoke_finalizers();*/
192         /*gc_call();*/
193 }
194
195
196 /*
197  * Class:     java/lang/VMRuntime
198  * Method:    traceInstructions
199  * Signature: (Z)V
200  */
201 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, int32_t par1)
202 {
203         /* not supported */
204 }
205
206
207 /*
208  * Class:     java/lang/VMRuntime
209  * Method:    traceMethodCalls
210  * Signature: (Z)V
211  */
212 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, int32_t par1)
213 {
214         /* not supported */
215 }
216
217
218 /*
219  * Class:     java/lang/VMRuntime
220  * Method:    availableProcessors
221  * Signature: ()I
222  */
223 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
224 {
225         return system_processors_online();
226 }
227
228
229 /*
230  * Class:     java/lang/VMRuntime
231  * Method:    nativeLoad
232  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
233  */
234 JNIEXPORT int32_t JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *libname, java_lang_ClassLoader *loader)
235 {
236         classloader *cl;
237
238         cl = loader_hashtable_classloader_add((java_handle_t *) loader);
239
240 #if defined(ENABLE_JNI)
241         return _Jv_java_lang_Runtime_loadLibrary(env, libname, cl);
242 #else
243         return _Jv_java_lang_Runtime_loadLibrary(libname, cl);
244 #endif
245 }
246
247
248 /*
249  * Class:     java/lang/VMRuntime
250  * Method:    mapLibraryName
251  * Signature: (Ljava/lang/String;)Ljava/lang/String;
252  */
253 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
254 {
255         utf           *u;
256         char          *buffer;
257         int32_t        buffer_len;
258         int32_t        dumpsize;
259         java_handle_t *o;
260
261         if (libname == NULL) {
262                 exceptions_throw_nullpointerexception();
263                 return NULL;
264         }
265
266         u = javastring_toutf((java_handle_t *) libname, false);
267
268         /* calculate length of library name */
269
270         buffer_len = strlen("lib");
271
272         buffer_len += utf_bytes(u);
273
274 #if defined(__DARWIN__)
275         buffer_len += strlen(".dylib");
276 #else
277         buffer_len += strlen(".so");
278 #endif
279
280         buffer_len += strlen("0");
281
282         dumpsize = dump_size();
283         buffer = DMNEW(char, buffer_len);
284
285         /* generate library name */
286
287         strcpy(buffer, "lib");
288         utf_cat(buffer, u);
289
290 #if defined(__DARWIN__)
291         strcat(buffer, ".dylib");
292 #else
293         strcat(buffer, ".so");
294 #endif
295
296         o = javastring_new_from_utf_string(buffer);
297
298         /* release memory */
299
300         dump_release(dumpsize);
301
302         return (java_lang_String *) o;
303 }
304
305
306 /*
307  * These are local overrides for various environment variables in Emacs.
308  * Please do not remove this and leave it at the end of the file, where
309  * Emacs will automagically detect them.
310  * ---------------------------------------------------------------------
311  * Local variables:
312  * mode: c
313  * indent-tabs-mode: t
314  * c-basic-offset: 4
315  * tab-width: 4
316  * End:
317  * vim:noexpandtab:sw=4:ts=4:
318  */