* src/vm/exceptions.c (exceptions_throw_unsatisfiedlinkerror)
[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    $Id: java_lang_VMRuntime.c 7305 2007-02-09 11:08:14Z twisti $
26
27 */
28
29
30 #include "config.h"
31
32 #include <assert.h>
33 #include <string.h>
34 #include <stdlib.h>
35 #include <sys/utsname.h>
36
37 #if !defined(WITH_STATIC_CLASSPATH)
38 # include <ltdl.h>
39 #endif
40
41 #if defined(__DARWIN__)
42 # define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
43 # include <mach/mach.h>
44 #endif
45
46 #include "vm/types.h"
47
48 #include "mm/gc-common.h"
49 #include "mm/memory.h"
50
51 #include "native/jni.h"
52 #include "native/native.h"
53
54 #include "native/include/java_io_File.h"
55 #include "native/include/java_lang_ClassLoader.h"
56 #include "native/include/java_lang_String.h"
57 #include "native/include/java_lang_Process.h"
58
59 #include "native/vm/java_lang_Runtime.h"
60
61 #include "toolbox/logging.h"
62
63 #include "vm/builtin.h"
64 #include "vm/exceptions.h"
65 #include "vm/stringlocal.h"
66 #include "vm/vm.h"
67
68 #include "vmcore/options.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 /*
80  * Class:     java/lang/VMRuntime
81  * Method:    exitInternal
82  * Signature: (I)V
83  */
84 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz, s4 par1)
85 {
86         _Jv_java_lang_Runtime_exit(status);
87 }
88
89
90 /*
91  * Class:     java/lang/VMRuntime
92  * Method:    freeMemory
93  * Signature: ()J
94  */
95 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass clazz)
96 {
97         return _Jv_java_lang_Runtime_freeMemory();
98 }
99
100
101 /*
102  * Class:     java/lang/VMRuntime
103  * Method:    totalMemory
104  * Signature: ()J
105  */
106 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
107 {
108         return _Jv_java_lang_Runtime_totalMemory();
109 }
110
111
112 /*
113  * Class:     java/lang/VMRuntime
114  * Method:    maxMemory
115  * Signature: ()J
116  */
117 JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
118 {
119         return gc_get_max_heap_size();
120 }
121
122
123 /*
124  * Class:     java/lang/VMRuntime
125  * Method:    gc
126  * Signature: ()V
127  */
128 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
129 {
130         _Jv_java_lang_Runtime_gc();
131 }
132
133
134 /*
135  * Class:     java/lang/VMRuntime
136  * Method:    runFinalization
137  * Signature: ()V
138  */
139 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jclass clazz)
140 {
141         gc_invoke_finalizers();
142 }
143
144
145 /*
146  * Class:     java/lang/VMRuntime
147  * Method:    runFinalizersOnExit
148  * Signature: (Z)V
149  */
150 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
151 {
152         /* XXX threading */
153
154         finalizeOnExit = value;
155 }
156
157
158 /*
159  * Class:     java/lang/VMRuntime
160  * Method:    runFinalizationsForExit
161  * Signature: ()V
162  */
163 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *env, jclass clazz)
164 {
165 /*      if (finalizeOnExit) { */
166 /*              gc_call(); */
167         /* gc_finalize_all(); */
168 /*      } */
169 /*      log_text("Java_java_lang_VMRuntime_runFinalizationForExit called"); */
170         /*gc_finalize_all();*/
171         /*gc_invoke_finalizers();*/
172         /*gc_call();*/
173 }
174
175
176 /*
177  * Class:     java/lang/VMRuntime
178  * Method:    traceInstructions
179  * Signature: (Z)V
180  */
181 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, jclass clazz, s4 par1)
182 {
183         /* not supported */
184 }
185
186
187 /*
188  * Class:     java/lang/VMRuntime
189  * Method:    traceMethodCalls
190  * Signature: (Z)V
191  */
192 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceMethodCalls(JNIEnv *env, jclass clazz, s4 par1)
193 {
194         /* not supported */
195 }
196
197
198 /*
199  * Class:     java/lang/VMRuntime
200  * Method:    availableProcessors
201  * Signature: ()I
202  */
203 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, jclass clazz)
204 {
205 #if defined(_SC_NPROC_ONLN)
206         return (s4) sysconf(_SC_NPROC_ONLN);
207
208 #elif defined(_SC_NPROCESSORS_ONLN)
209         return (s4) sysconf(_SC_NPROCESSORS_ONLN);
210
211 #elif defined(__DARWIN__)
212         /* this should work in BSD */
213         /*
214         int ncpu, mib[2], rc;
215         size_t len;
216
217         mib[0] = CTL_HW;
218         mib[1] = HW_NCPU;
219         len = sizeof(ncpu);
220         rc = sysctl(mib, 2, &ncpu, &len, NULL, 0);
221
222         return (s4) ncpu;
223         */
224
225         host_basic_info_data_t hinfo;
226         mach_msg_type_number_t hinfo_count = HOST_BASIC_INFO_COUNT;
227         kern_return_t rc;
228
229         rc = host_info(mach_host_self(), HOST_BASIC_INFO,
230                                    (host_info_t) &hinfo, &hinfo_count);
231  
232         if (rc != KERN_SUCCESS) {
233                 return -1;
234         }
235
236     return (s4) hinfo.avail_cpus;
237
238 #else
239         return 1;
240 #endif
241 }
242
243
244 /*
245  * Class:     java/lang/VMRuntime
246  * Method:    nativeLoad
247  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
248  */
249 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
250 {
251 #if !defined(WITH_STATIC_CLASSPATH)
252         utf         *name;
253         lt_dlhandle  handle;
254         lt_ptr       onload;
255         s4           version;
256 #endif
257
258         if (filename == NULL) {
259                 exceptions_throw_nullpointerexception();
260                 return 0;
261         }
262
263 #if defined(WITH_STATIC_CLASSPATH)
264         return 1;
265 #else /* defined(WITH_STATIC_CLASSPATH) */
266         name = javastring_toutf(filename, 0);
267
268         /* is the library already loaded? */
269
270         if (native_hashtable_library_find(name, (java_objectheader *) loader))
271                 return 1;
272
273         /* try to open the library */
274
275         if (!(handle = lt_dlopen(name->text))) {
276                 if (opt_verbose) {
277                         log_start();
278                         log_print("Java_java_lang_VMRuntime_nativeLoad: ");
279                         log_print(lt_dlerror());
280                         log_finish();
281                 }
282
283                 return 0;
284         }
285
286         /* resolve JNI_OnLoad function */
287
288         if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
289                 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
290                 JavaVM *vm;
291
292                 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
293
294                 (*env)->GetJavaVM(env, &vm);
295
296                 version = JNI_OnLoad(vm, NULL);
297
298                 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
299
300                 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
301                         lt_dlclose(handle);
302
303                         return 0;
304                 }
305         }
306
307         /* insert the library name into the library hash */
308
309         native_hashtable_library_add(name, (java_objectheader *) loader, handle);
310
311         return 1;
312 #endif /* defined(WITH_STATIC_CLASSPATH) */
313 }
314
315
316 /*
317  * Class:     java/lang/VMRuntime
318  * Method:    mapLibraryName
319  * Signature: (Ljava/lang/String;)Ljava/lang/String;
320  */
321 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
322 {
323         utf              *u;
324         char             *buffer;
325         s4                buffer_len;
326         s4                dumpsize;
327         java_lang_String *s;
328
329         if (!libname) {
330                 exceptions_throw_nullpointerexception();
331                 return NULL;
332         }
333
334         u = javastring_toutf(libname, 0);
335
336         /* calculate length of library name */
337
338         buffer_len = strlen("lib");
339
340         buffer_len += utf_bytes(u);
341
342 #if defined(__DARWIN__)
343         buffer_len += strlen(".dylib");
344 #else
345         buffer_len += strlen(".so");
346 #endif
347
348         buffer_len += strlen("0");
349
350         dumpsize = dump_size();
351         buffer = DMNEW(char, buffer_len);
352
353         /* generate library name */
354
355         strcpy(buffer, "lib");
356         utf_cat(buffer, u);
357
358 #if defined(__DARWIN__)
359         strcat(buffer, ".dylib");
360 #else
361         strcat(buffer, ".so");
362 #endif
363
364         s = javastring_new_from_utf_string(buffer);
365
366         /* release memory */
367
368         dump_release(dumpsize);
369
370         return s;
371 }
372
373
374 /*
375  * These are local overrides for various environment variables in Emacs.
376  * Please do not remove this and leave it at the end of the file, where
377  * Emacs will automagically detect them.
378  * ---------------------------------------------------------------------
379  * Local variables:
380  * mode: c
381  * indent-tabs-mode: t
382  * c-basic-offset: 4
383  * tab-width: 4
384  * End:
385  * vim:noexpandtab:sw=4:ts=4:
386  */