1 // natRuntime.cc - Implementation of native side of Runtime class.
3 /* Copyright (C) 1998, 1999, 2000, 2001 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)
85 java::lang::Runtime::_exit (jint status)
87 // Make status right for Unix. This is perhaps strange.
88 if (status < 0 || status > 255)
92 _Jv_RunAllFinalizers ();
94 // Delete all files registered with File.deleteOnExit()
95 gnu::gcj::runtime::FileDeleter::deleteOnExitNow ();
101 java::lang::Runtime::freeMemory (void)
103 return _Jv_GCFreeMemory ();
107 java::lang::Runtime::gc (void)
113 java::lang::Runtime::_load (jstring path, jboolean do_search)
115 JvSynchronize sync (this);
117 using namespace java::lang;
119 jint len = _Jv_GetStringUTFLength (path);
120 char buf[len + 1 + 3];
123 // On Unix boxes, prefix library name with `lib', for loadLibrary.
130 jsize total = JvGetStringUTFRegion (path, 0, path->length(), &buf[offset]);
131 buf[offset + total] = '\0';
132 // FIXME: make sure path is absolute.
133 lt_dlhandle h = do_search ? lt_dlopenext (buf) : lt_dlopen (buf);
136 const char *msg = lt_dlerror ();
137 jstring str = path->concat (JvNewStringLatin1 (": "));
138 str = str->concat (JvNewStringLatin1 (msg));
139 throw new UnsatisfiedLinkError (str);
144 void *onload = lt_dlsym (h, "JNI_OnLoad");
147 JavaVM *vm = _Jv_GetJavaVM ();
153 jint vers = ((jint (*) (JavaVM *, void *)) onload) (vm, NULL);
154 if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
156 // FIXME: unload the library.
157 throw new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad"));
161 throw new UnknownError
162 (JvNewStringLatin1 (do_search
163 ? "Runtime.loadLibrary not implemented"
164 : "Runtime.load not implemented"));
165 #endif /* USE_LTDL */
169 java::lang::Runtime::loadLibraryInternal (jstring lib)
171 JvSynchronize sync (this);
172 using namespace java::lang;
174 jint len = _Jv_GetStringUTFLength (lib);
176 jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
178 // FIXME: make sure path is absolute.
179 lt_dlhandle h = lt_dlopenext (buf);
185 #endif /* USE_LTDL */
189 java::lang::Runtime::init (void)
191 finalize_on_exit = false;
198 java::lang::Runtime::runFinalization (void)
200 _Jv_RunFinalizers ();
204 java::lang::Runtime::totalMemory (void)
206 return _Jv_GCTotalMemory ();
210 java::lang::Runtime::traceInstructions (jboolean)
216 java::lang::Runtime::traceMethodCalls (jboolean)