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