* Removed all Id tags.
[cacao.git] / src / native / vm / java_lang_Runtime.c
1 /* src/native/vm/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 #if defined(ENABLE_LTDL) && defined(HAVE_LTDL_H)
31 # include <ltdl.h>
32 #endif
33
34 #include "vm/types.h"
35
36 #include "mm/gc-common.h"
37
38 #include "native/jni.h"
39 #include "native/native.h"
40
41 #include "native/include/java_lang_String.h"
42
43 #include "toolbox/logging.h"
44
45 #include "vm/exceptions.h"
46 #include "vm/stringlocal.h"
47 #include "vm/vm.h"
48
49 #include "vmcore/options.h"
50
51
52 /* should we run all finalizers on exit? */
53 static bool finalizeOnExit = false;
54
55
56 /*
57  * Class:     java/lang/Runtime
58  * Method:    exitInternal
59  * Signature: (I)V
60  */
61 void _Jv_java_lang_Runtime_exit(s4 status)
62 {
63         if (finalizeOnExit)
64                 gc_finalize_all();
65
66         vm_shutdown(status);
67 }
68
69
70 /*
71  * Class:     java/lang/Runtime
72  * Method:    freeMemory
73  * Signature: ()J
74  */
75 s8 _Jv_java_lang_Runtime_freeMemory(void)
76 {
77         return gc_get_free_bytes();
78 }
79
80
81 /*
82  * Class:     java/lang/Runtime
83  * Method:    totalMemory
84  * Signature: ()J
85  */
86 s8 _Jv_java_lang_Runtime_totalMemory(void)
87 {
88         return gc_get_heap_size();
89 }
90
91
92 /*
93  * Class:     java/lang/Runtime
94  * Method:    gc
95  * Signature: ()V
96  */
97 void _Jv_java_lang_Runtime_gc(void)
98 {
99         gc_call();
100 }
101
102
103 /*
104  * Class:     java/lang/Runtime
105  * Method:    loadLibrary
106  * Signature: (Ljava/lang/String;Ljava/lang/ClassLoader;)I
107  */
108 #if defined(ENABLE_JNI)
109 s4 _Jv_java_lang_Runtime_loadLibrary(JNIEnv *env, java_lang_String *libname, classloader *cl)
110 #else
111 s4 _Jv_java_lang_Runtime_loadLibrary(java_lang_String *libname, classloader *cl)
112 #endif
113 {
114 #if defined(ENABLE_LTDL)
115         utf               *name;
116         lt_dlhandle        handle;
117 # if defined(ENABLE_JNI)
118         lt_ptr             onload;
119         s4                 version;
120 # endif
121
122         if (libname == NULL) {
123                 exceptions_throw_nullpointerexception();
124                 return 0;
125         }
126
127         name = javastring_toutf((java_handle_t *) libname, false);
128
129         /* is the library already loaded? */
130
131         if (native_library_find(name, cl) != NULL)
132                 return 1;
133
134         /* open the library */
135
136         handle = native_library_open(name);
137
138         if (handle == NULL)
139                 return 0;
140
141 # if defined(ENABLE_JNI)
142         /* resolve JNI_OnLoad function */
143
144         onload = lt_dlsym(handle, "JNI_OnLoad");
145
146         if (onload != NULL) {
147                 JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
148                 JavaVM *vm;
149
150                 JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
151
152                 (*env)->GetJavaVM(env, &vm);
153
154                 version = JNI_OnLoad(vm, NULL);
155
156                 /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
157
158                 if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
159                         lt_dlclose(handle);
160
161                         return 0;
162                 }
163         }
164 # endif
165
166         /* insert the library name into the library hash */
167
168         native_library_add(name, cl, handle);
169
170         return 1;
171 #else
172         vm_abort("_Jv_java_lang_Runtime_loadLibrary: not available");
173
174         /* keep compiler happy */
175
176         return 0;
177 #endif
178 }
179
180
181 #if defined(ENABLE_JAVASE)
182
183 /*
184  * Class:     java/lang/Runtime
185  * Method:    runFinalizersOnExit
186  * Signature: (Z)V
187  */
188 void _Jv_java_lang_Runtime_runFinalizersOnExit(s4 value)
189 {
190         /* XXX threading */
191
192         finalizeOnExit = value;
193 }
194
195 #endif
196
197
198 /*
199  * These are local overrides for various environment variables in Emacs.
200  * Please do not remove this and leave it at the end of the file, where
201  * Emacs will automagically detect them.
202  * ---------------------------------------------------------------------
203  * Local variables:
204  * mode: c
205  * indent-tabs-mode: t
206  * c-basic-offset: 4
207  * tab-width: 4
208  * End:
209  * vim:noexpandtab:sw=4:ts=4:
210  */