3af4ec0a1ea1c181790eb22a75b2a057209310df
[cacao.git] / src / native / vm / VMRuntime.c
1 /* src/native/vm/VMRuntime.c - java/lang/VMRuntime
2
3    Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
4    R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
5    C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
6    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., 59 Temple Place - Suite 330, Boston, MA
23    02111-1307, USA.
24
25    Contact: cacao@complang.tuwien.ac.at
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: VMRuntime.c 2647 2005-06-13 13:56:42Z twisti $
33
34 */
35
36
37 #include "config.h"
38
39 #include <string.h>
40 #include <stdlib.h>
41 #include <unistd.h>
42 #include <sys/utsname.h>
43
44 #if defined(__DARWIN__)
45 # include <mach/mach.h>
46 #endif
47
48 #if !defined(STATIC_CLASSPATH)
49 # include "src/libltdl/ltdl.h"
50 #endif
51
52 #include "cacao/cacao.h"
53 #include "mm/boehm.h"
54 #include "mm/memory.h"
55 #include "native/jni.h"
56 #include "native/native.h"
57 #include "native/include/java_io_File.h"
58 #include "native/include/java_lang_String.h"
59 #include "native/include/java_lang_Process.h"
60 #include "toolbox/logging.h"
61 #include "vm/builtin.h"
62 #include "vm/exceptions.h"
63 #include "vm/loader.h"
64 #include "vm/stringlocal.h"
65 #include "vm/tables.h"
66
67
68 /* this should work on BSD */
69 /*
70 #if defined(__DARWIN__)
71 #include <sys/sysctl.h>
72 #endif
73 */
74
75 #undef JOWENN_DEBUG
76
77 /* should we run all finalizers on exit? */
78 static bool finalizeOnExit = false;
79
80
81 /*
82  * Class:     java/lang/VMRuntime
83  * Method:    execInternal
84  * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
85  */
86 JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
87 {
88         log_text("Java_java_lang_Runtime_execInternal called");
89
90         return NULL;
91 }
92
93
94 /*
95  * Class:     java/lang/VMRuntime
96  * Method:    exitInternal
97  * Signature: (I)V
98  */
99 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
100 {
101         if (finalizeOnExit)
102                 gc_finalize_all();
103
104         cacao_shutdown(par1);
105 }
106
107
108 /*
109  * Class:     java/lang/VMRuntime
110  * Method:    freeMemory
111  * Signature: ()J
112  */
113 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
114 {
115         return gc_get_free_bytes();
116 }
117
118
119 /*
120  * Class:     java/lang/VMRuntime
121  * Method:    gc
122  * Signature: ()V
123  */
124 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
125 {
126         gc_call();
127 }
128
129
130 /*
131  * Class:     java/lang/VMRuntime
132  * Method:    runFinalization
133  * Signature: ()V
134  */
135 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
136 {
137         gc_invoke_finalizers();
138 }
139
140
141 /*
142  * Class:     java/lang/VMRuntime
143  * Method:    runFinalizersOnExit
144  * Signature: (Z)V
145  */
146 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
147 {
148         /* XXX threading */
149
150         finalizeOnExit = value;
151 }
152
153
154 /*
155  * Class:     java/lang/VMRuntime
156  * Method:    runFinalizationsForExit
157  * Signature: ()V
158  */
159 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
160 {
161 /*      if (finalizeOnExit) { */
162 /*              gc_call(); */
163         /* gc_finalize_all(); */
164 /*      } */
165 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
166         /*gc_finalize_all();*/
167         /*gc_invoke_finalizers();*/
168         /*gc_call();*/
169 }
170
171
172 /*
173  * Class:     java/lang/VMRuntime
174  * Method:    totalMemory
175  * Signature: ()J
176  */
177 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
178 {
179         return gc_get_heap_size();
180 }
181
182
183 /*
184  * Class:     java/lang/VMRuntime
185  * Method:    traceInstructions
186  * Signature: (Z)V
187  */
188 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
189 {
190         /* not supported */
191 }
192
193
194 /*
195  * Class:     java/lang/VMRuntime
196  * Method:    traceMethodCalls
197  * Signature: (Z)V
198  */
199 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
200 {
201         /* not supported */
202 }
203
204
205 /*
206  * Class:     java_lang_Runtime
207  * Method:    availableProcessors
208  * Signature: ()I
209  */
210 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
211 {
212 #if defined(_SC_NPROC_ONLN)
213         return (s4) sysconf(_SC_NPROC_ONLN);
214
215 #elif defined(_SC_NPROCESSORS_ONLN)
216         return (s4) sysconf(_SC_NPROCESSORS_ONLN);
217
218 #elif defined(__DARWIN__)
219         /* this should work in BSD */
220         /*
221         int ncpu, mib[2], rc;
222         size_t len;
223
224         mib[0] = CTL_HW;
225         mib[1] = HW_NCPU;
226         len = sizeof(ncpu);
227         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
228
229         return (s4) ncpu;
230         */
231
232         host_basic_info_data_t hinfo;
233         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
234         kern_return_t rc;
235
236         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
237                                    (host_info_t) &hinfo, &hinfo_count);
238  
239         if (rc != KERN_SUCCESS) {
240                 return -1;
241         }
242
243     return (s4) hinfo.avail_cpus;
244
245 #else
246         return 1;
247 #endif
248 }
249
250
251 /*
252  * Class:     java/lang/VMRuntime
253  * Method:    nativeLoad
254  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
255  */
256 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
257 {
258         utf *name;
259
260         if (!filename) {
261                 *exceptionptr = new_nullpointerexception();
262                 return 0;
263         }
264
265         name = javastring_toutf(filename, 0);
266         
267 #if defined(STATIC_CLASSPATH)
268         return 1;
269 #else
270         /* here it could be interesting to store the references in a list eg for  */
271         /* nicely cleaning up or for certain platforms */
272
273         if (lt_dlopenext(name->text))
274                 return 1;
275
276         return 0;
277 #endif
278 }
279
280
281 /*
282  * Class:     java/lang/VMRuntime
283  * Method:    mapLibraryName
284  * Signature: (Ljava/lang/String;)Ljava/lang/String;
285  */
286 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
287 {
288         utf              *u;
289         char             *buffer;
290         s4                buffer_len;
291         s4                dumpsize;
292         java_lang_String *s;
293
294         if (!libname) {
295                 *exceptionptr = new_nullpointerexception();
296                 return NULL;
297         }
298
299         u = javastring_toutf(libname, 0);
300
301         /* calculate length of library name */
302
303         buffer_len = strlen("lib") + utf_strlen(u) + strlen(".so") + strlen("0");
304
305         dumpsize = dump_size();
306         buffer = DMNEW(char, buffer_len);
307
308         /* generate library name, we use lt_dlopenext so we don't need an */
309         /* extension */
310
311         strcpy(buffer, "lib");
312         utf_strcat(buffer, u);
313         strcat(buffer, ".so");
314
315         s = javastring_new_char(buffer);
316
317         /* release memory */
318
319         dump_release(dumpsize);
320
321         return s;
322 }
323
324
325 /*
326  * Class:     java_lang_VMRuntime
327  * Method:    maxMemory
328  * Signature: ()J
329  */
330 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
331 {
332         return gc_get_max_heap_size();
333 }
334
335
336 /*
337  * These are local overrides for various environment variables in Emacs.
338  * Please do not remove this and leave it at the end of the file, where
339  * Emacs will automagically detect them.
340  * ---------------------------------------------------------------------
341  * Local variables:
342  * mode: c
343  * indent-tabs-mode: t
344  * c-basic-offset: 4
345  * tab-width: 4
346  * End:
347  */