1 // natRuntime.cc - Implementation of native side of Runtime class.
3 /* Copyright (C) 1998, 1999, 2000 Free Software Foundation
5 This file is part of libgcj.
7 This software is copyrighted work licensed under the terms of the
8 Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
17 #include <java/lang/Runtime.h>
18 #include <java/lang/UnknownError.h>
19 #include <java/lang/UnsatisfiedLinkError.h>
20 #include <gnu/gcj/runtime/FileDeleter.h>
27 /* FIXME: we don't always need this. The next libtool will let us use
28 AC_LTDL_PREOPEN to see if we do. */
29 extern const lt_dlsymlist lt_preloaded_symbols[1] = { { 0, 0 } };
31 // We keep track of all the libraries loaded by this application. For
32 // now we use them to look up symbols for JNI. `libraries_size' holds
33 // the total size of the buffer. `libraries_count' is the number of
34 // items which are in use.
35 static int libraries_size;
36 static int libraries_count;
37 static lt_dlhandle *libraries;
40 add_library (lt_dlhandle lib)
42 if (libraries_count == libraries_size)
44 int ns = libraries_size * 2;
47 lt_dlhandle *n = (lt_dlhandle *) _Jv_Malloc (ns * sizeof (lt_dlhandle));
50 memcpy (n, libraries, libraries_size * sizeof (lt_dlhandle));
55 for (int i = libraries_count; i < libraries_size; ++i)
59 libraries[libraries_count++] = lib;
63 _Jv_FindSymbolInExecutable (const char *symname)
65 for (int i = 0; i < libraries_count; ++i)
67 void *r = lt_dlsym (libraries[i], symname);
72 return lt_dlsym (NULL, symname);
78 java::lang::Runtime::exit (jint status)
82 // Make status right for Unix. This is perhaps strange.
83 if (status < 0 || status > 255)
87 _Jv_RunAllFinalizers ();
89 // Delete all files registered with File.deleteOnExit()
90 gnu::gcj::runtime::FileDeleter::deleteOnExitNow ();
96 java::lang::Runtime::freeMemory (void)
98 return _Jv_GCFreeMemory ();
102 java::lang::Runtime::gc (void)
108 java::lang::Runtime::_load (jstring path, jboolean do_search)
110 JvSynchronize sync (this);
112 using namespace java::lang;
114 jint len = _Jv_GetStringUTFLength (path);
115 char buf[len + 1 + 3];
118 // On Unix boxes, prefix library name with `lib', for loadLibrary.
125 jsize total = JvGetStringUTFRegion (path, 0, path->length(), &buf[offset]);
126 buf[offset + total] = '\0';
127 // FIXME: make sure path is absolute.
128 lt_dlhandle h = do_search ? lt_dlopenext (buf) : lt_dlopen (buf);
131 const char *msg = lt_dlerror ();
132 jstring str = path->concat (JvNewStringLatin1 (": "));
133 str = str->concat (JvNewStringLatin1 (msg));
134 _Jv_Throw (new UnsatisfiedLinkError (str));
139 void *onload = lt_dlsym (h, "JNI_OnLoad");
142 JavaVM *vm = _Jv_GetJavaVM ();
148 jint vers = ((jint (*) (JavaVM *, void *)) onload) (vm, NULL);
149 if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
151 // FIXME: unload the library.
152 _Jv_Throw (new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad")));
156 _Jv_Throw (new UnknownError
157 (JvNewStringLatin1 (do_search
158 ? "Runtime.loadLibrary not implemented"
159 : "Runtime.load not implemented")));
160 #endif /* USE_LTDL */
164 java::lang::Runtime::loadLibraryInternal (jstring lib)
166 JvSynchronize sync (this);
167 using namespace java::lang;
169 jint len = _Jv_GetStringUTFLength (lib);
171 jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
173 // FIXME: make sure path is absolute.
174 lt_dlhandle h = lt_dlopenext (buf);
180 #endif /* USE_LTDL */
184 java::lang::Runtime::init (void)
186 finalize_on_exit = false;
193 java::lang::Runtime::runFinalization (void)
195 _Jv_RunFinalizers ();
199 java::lang::Runtime::totalMemory (void)
201 return _Jv_GCTotalMemory ();
205 java::lang::Runtime::traceInstructions (jboolean)
211 java::lang::Runtime::traceMethodCalls (jboolean)