OSDN Git Service

* include/jvm.h (_Jv_GetJavaVM): Declare.
[pf3gnuchains/gcc-fork.git] / libjava / java / lang / natRuntime.cc
1 // natRuntime.cc - Implementation of native side of Runtime class.
2
3 /* Copyright (C) 1998, 1999, 2000  Red Hat, Inc.
4
5    This file is part of libgcj.
6
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License.  Please consult the file "LIBGCJ_LICENSE" for
9 details.  */
10
11 #include <config.h>
12
13 #include <stdlib.h>
14
15 #include <gcj/cni.h>
16 #include <jvm.h>
17 #include <java/lang/Runtime.h>
18 #include <java/lang/UnknownError.h>
19 #include <java/lang/UnsatisfiedLinkError.h>
20
21 #include <jni.h>
22
23 #ifdef USE_LTDL
24 #include <ltdl.h>
25
26 /* FIXME: we don't always need this.  The next libtool will let us use
27    AC_LTDL_PREOPEN to see if we do.  */
28 const lt_dlsymlist lt_preloaded_symbols[1] = { { 0, 0 } };
29
30 // We keep track of all the libraries loaded by this application.  For
31 // now we use them to look up symbols for JNI.  `libraries_size' holds
32 // the total size of the buffer.  `libraries_count' is the number of
33 // items which are in use.
34 static int libraries_size;
35 static int libraries_count;
36 static lt_dlhandle *libraries;
37
38 static void
39 add_library (lt_dlhandle lib)
40 {
41   if (libraries_count == libraries_size)
42     {
43       int ns = libraries_size * 2;
44       if (ns == 0)
45         ns = 10;
46       lt_dlhandle *n = (lt_dlhandle *) _Jv_Malloc (ns * sizeof (lt_dlhandle));
47       if (libraries)
48         {
49           memcpy (n, libraries, libraries_size * sizeof (lt_dlhandle));
50           _Jv_Free (libraries);
51         }
52       libraries = n;
53       libraries_size = ns;
54       for (int i = libraries_count; i < libraries_size; ++i)
55         libraries[i] = NULL;
56     }
57
58   libraries[libraries_count++] = lib;
59 }
60
61 void *
62 _Jv_FindSymbolInExecutable (const char *symname)
63 {
64   for (int i = 0; i < libraries_count; ++i)
65     {
66       void *r = lt_dlsym (libraries[i], symname);
67       if (r)
68         return r;
69     }
70
71   return lt_dlsym (NULL, symname);
72 }
73
74 #endif /* USE_LTDL */
75
76 void
77 java::lang::Runtime::exit (jint status)
78 {
79   checkExit (status);
80
81   // Make status right for Unix.  This is perhaps strange.
82   if (status < 0 || status > 255)
83     status = 255;
84
85   if (finalize_on_exit)
86     _Jv_RunAllFinalizers ();
87
88   ::exit (status);
89 }
90
91 jlong
92 java::lang::Runtime::freeMemory (void)
93 {
94   return _Jv_GCFreeMemory ();
95 }
96
97 void
98 java::lang::Runtime::gc (void)
99 {
100   _Jv_RunGC ();
101 }
102
103 void
104 java::lang::Runtime::_load (jstring path, jboolean do_search)
105 {
106   JvSynchronize sync (this);
107   checkLink (path);
108   using namespace java::lang;
109 #ifdef USE_LTDL
110   jint len = _Jv_GetStringUTFLength (path);
111   char buf[len + 1 + 3];
112   int offset = 0;
113 #ifndef WIN32
114   // On Unix boxes, prefix library name with `lib', for loadLibrary.
115   if (do_search)
116     {
117       strcpy (buf, "lib");
118       offset = 3;
119     }
120 #endif
121   jsize total = JvGetStringUTFRegion (path, 0, path->length(), &buf[offset]);
122   buf[offset + total] = '\0';
123   // FIXME: make sure path is absolute.
124   lt_dlhandle h = do_search ? lt_dlopenext (buf) : lt_dlopen (buf);
125   if (h == NULL)
126     {
127       const char *msg = lt_dlerror ();
128       jstring str = path->concat (JvNewStringLatin1 (": "));
129       str = str->concat (JvNewStringLatin1 (msg));
130       _Jv_Throw (new UnsatisfiedLinkError (str));
131     }
132
133   add_library (h);
134
135   void *onload = lt_dlsym (h, "JNI_OnLoad");
136   if (onload != NULL)
137     {
138       JavaVM *vm = _Jv_GetJavaVM ();
139       if (vm == NULL)
140         {
141           // FIXME: what?
142           return;
143         }
144       jint vers = ((jint (*) (JavaVM *, void *)) onload) (vm, NULL);
145       if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
146         {
147           // FIXME: unload the library.
148           _Jv_Throw (new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad")));
149         }
150     }
151 #else
152   _Jv_Throw (new UnknownError
153              (JvNewStringLatin1 (do_search
154                                  ? "Runtime.loadLibrary not implemented"
155                                  : "Runtime.load not implemented")));
156 #endif /* USE_LTDL */
157 }
158
159 jboolean
160 java::lang::Runtime::loadLibraryInternal (jstring lib)
161 {
162   JvSynchronize sync (this);
163   using namespace java::lang;
164 #ifdef USE_LTDL
165   jint len = _Jv_GetStringUTFLength (lib);
166   char buf[len + 1];
167   jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
168   buf[total] = '\0';
169   // FIXME: make sure path is absolute.
170   lt_dlhandle h = lt_dlopenext (buf);
171   if (h != NULL)
172     add_library (h);
173   return h != NULL;
174 #else
175   return false;
176 #endif /* USE_LTDL */
177 }
178
179 void
180 java::lang::Runtime::init (void)
181 {
182   finalize_on_exit = false;
183 #ifdef USE_LTDL
184   lt_dlinit ();
185 #endif
186 }
187
188 void
189 java::lang::Runtime::runFinalization (void)
190 {
191   _Jv_RunFinalizers ();
192 }
193
194 jlong
195 java::lang::Runtime::totalMemory (void)
196 {
197   return _Jv_GCTotalMemory ();
198 }
199
200 void
201 java::lang::Runtime::traceInstructions (jboolean)
202 {
203   // Do nothing.
204 }
205
206 void
207 java::lang::Runtime::traceMethodCalls (jboolean)
208 {
209   // Do nothing.
210 }