* src/vm/string.c, src/vm/stringlocal.h (java_string_new_char):
[cacao.git] / src / native / vm / VMRuntime.c
1 /* src/native/vm/VMRuntime.c - java/lang/VMRuntime
2
3    Copyright (C) 1996-2005, 2006 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    Contact: cacao@cacaojvm.org
26
27    Authors: Roman Obermaiser
28
29    Changes: Joseph Wenninger
30             Christian Thalinger
31
32    $Id: VMRuntime.c 4874 2006-05-05 14:36:18Z edwin $
33
34 */
35
36
37 #include "config.h"
38
39 #include <assert.h>
40 #include <string.h>
41 #include <stdlib.h>
42 #include <sys/utsname.h>
43
44 #if !defined(WITH_STATIC_CLASSPATH)
45 # include <ltdl.h>
46 #endif
47
48 #if defined(__DARWIN__)
49 # define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
50 # include <mach/mach.h>
51 #endif
52
53 #include "vm/types.h"
54
55 #include "mm/boehm.h"
56 #include "mm/memory.h"
57 #include "native/jni.h"
58 #include "native/native.h"
59 #include "native/include/java_io_File.h"
60 #include "native/include/java_lang_String.h"
61 #include "native/include/java_lang_Process.h"
62 #include "toolbox/logging.h"
63 #include "vm/builtin.h"
64 #include "vm/exceptions.h"
65 #include "vm/loader.h"
66 #include "vm/options.h"
67 #include "vm/stringlocal.h"
68 #include "vm/vm.h"
69
70
71 /* this should work on BSD */
72 /*
73 #if defined(__DARWIN__)
74 #include <sys/sysctl.h>
75 #endif
76 */
77
78
79 /* should we run all finalizers on exit? */
80 static bool finalizeOnExit = false;
81
82
83 /*
84  * Class:     java/lang/VMRuntime
85  * Method:    exitInternal
86  * Signature: (I)V
87  */
88 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
89 {
90         if (finalizeOnExit)
91                 gc_finalize_all();
92
93         vm_shutdown(par1);
94 }
95
96
97 /*
98  * Class:     java/lang/VMRuntime
99  * Method:    freeMemory
100  * Signature: ()J
101  */
102 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
103 {
104         return gc_get_free_bytes();
105 }
106
107
108 /*
109  * Class:     java/lang/VMRuntime
110  * Method:    totalMemory
111  * Signature: ()J
112  */
113 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
114 {
115         return gc_get_heap_size();
116 }
117
118
119 /*
120  * Class:     java_lang_VMRuntime
121  * Method:    maxMemory
122  * Signature: ()J
123  */
124 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
125 {
126         return gc_get_max_heap_size();
127 }
128
129
130 /*
131  * Class:     java/lang/VMRuntime
132  * Method:    gc
133  * Signature: ()V
134  */
135 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
136 {
137         gc_call();
138 }
139
140
141 /*
142  * Class:     java/lang/VMRuntime
143  * Method:    runFinalization
144  * Signature: ()V
145  */
146 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
147 {
148         gc_invoke_finalizers();
149 }
150
151
152 /*
153  * Class:     java/lang/VMRuntime
154  * Method:    runFinalizersOnExit
155  * Signature: (Z)V
156  */
157 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
158 {
159         /* XXX threading */
160
161         finalizeOnExit = value;
162 }
163
164
165 /*
166  * Class:     java/lang/VMRuntime
167  * Method:    runFinalizationsForExit
168  * Signature: ()V
169  */
170 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
171 {
172 /*      if (finalizeOnExit) { */
173 /*              gc_call(); */
174         /* gc_finalize_all(); */
175 /*      } */
176 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
177         /*gc_finalize_all();*/
178         /*gc_invoke_finalizers();*/
179         /*gc_call();*/
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 #if !defined(WITH_STATIC_CLASSPATH)
259         utf         *name;
260         lt_dlhandle  handle;
261         lt_ptr       onload;
262         s4           version;
263 #endif
264
265         if (filename == NULL) {
266                 exceptions_throw_nullpointerexception();
267                 return 0;
268         }
269
270 #if defined(WITH_STATIC_CLASSPATH)
271         return 1;
272 #else /* defined(WITH_STATIC_CLASSPATH) */
273         name = javastring_toutf(filename, 0);
274
275         /* is the library already loaded? */
276
277         if (native_hashtable_library_find(name, (java_objectheader *) loader))
278                 return 1;
279
280         /* try to open the library */
281
282         if (!(handle = lt_dlopen(name->text))) {
283                 if (opt_verbose) {
284                         log_start();
285                         log_print("Java_java_lang_VMRuntime_nativeLoad: ");
286                         log_print(lt_dlerror());
287                         log_finish();
288                 }
289
290                 return 0;
291         }
292
293         /* resolve JNI_OnLoad function */
294
295         if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
296                 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
297                 JavaVM *vm;
298
299                 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
300
301                 (*env)->GetJavaVM(env, &vm);
302
303                 version = JNI_OnLoad(vm, NULL);
304
305                 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
306
307                 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
308                         lt_dlclose(handle);
309
310                         return 0;
311                 }
312         }
313
314         /* insert the library name into the library hash */
315
316         native_hashtable_library_add(name, (java_objectheader *) loader, handle);
317
318         return 1;
319 #endif /* defined(WITH_STATIC_CLASSPATH) */
320 }
321
322
323 /*
324  * Class:     java/lang/VMRuntime
325  * Method:    mapLibraryName
326  * Signature: (Ljava/lang/String;)Ljava/lang/String;
327  */
328 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
329 {
330         utf              *u;
331         char             *buffer;
332         s4                buffer_len;
333         s4                dumpsize;
334         java_lang_String *s;
335
336         if (!libname) {
337                 exceptions_throw_nullpointerexception();
338                 return NULL;
339         }
340
341         u = javastring_toutf(libname, 0);
342
343         /* calculate length of library name */
344
345         buffer_len = strlen("lib");
346
347         buffer_len += utf_get_number_of_u2s(u);
348
349 #if defined(__DARWIN__)
350         buffer_len += strlen(".dylib");
351 #else
352         buffer_len += strlen(".so");
353 #endif
354
355         buffer_len += strlen("0");
356
357         dumpsize = dump_size();
358         buffer = DMNEW(char, buffer_len);
359
360         /* generate library name */
361
362         strcpy(buffer, "lib");
363         utf_strcat(buffer, u);
364
365 #if defined(__DARWIN__)
366         strcat(buffer, ".dylib");
367 #else
368         strcat(buffer, ".so");
369 #endif
370
371         s = javastring_new_from_ascii(buffer);
372
373         /* release memory */
374
375         dump_release(dumpsize);
376
377         return s;
378 }
379
380
381 /*
382  * These are local overrides for various environment variables in Emacs.
383  * Please do not remove this and leave it at the end of the file, where
384  * Emacs will automagically detect them.
385  * ---------------------------------------------------------------------
386  * Local variables:
387  * mode: c
388  * indent-tabs-mode: t
389  * c-basic-offset: 4
390  * tab-width: 4
391  * End:
392  */