1 // natRuntime.cc - Implementation of native side of Runtime class.
3 /* Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
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>
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 } };
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;
39 add_library (lt_dlhandle lib)
41 if (libraries_count == libraries_size)
43 int ns = libraries_size * 2;
46 lt_dlhandle *n = (lt_dlhandle *) _Jv_Malloc (ns * sizeof (lt_dlhandle));
49 memcpy (n, libraries, libraries_size * sizeof (lt_dlhandle));
54 for (int i = libraries_count; i < libraries_size; ++i)
58 libraries[libraries_count++] = lib;
62 _Jv_FindSymbolInExecutable (const char *symname)
64 for (int i = 0; i < libraries_count; ++i)
66 void *r = lt_dlsym (libraries[i], symname);
71 return lt_dlsym (NULL, symname);
77 java::lang::Runtime::exit (jint status)
81 // Make status right for Unix. This is perhaps strange.
82 if (status < 0 || status > 255)
86 _Jv_RunAllFinalizers ();
92 java::lang::Runtime::freeMemory (void)
94 return _Jv_GCFreeMemory ();
98 java::lang::Runtime::gc (void)
104 java::lang::Runtime::_load (jstring path, jboolean do_search)
106 JvSynchronize sync (this);
108 using namespace java::lang;
110 jint len = _Jv_GetStringUTFLength (path);
111 char buf[len + 1 + 3];
114 // On Unix boxes, prefix library name with `lib', for loadLibrary.
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);
127 const char *msg = lt_dlerror ();
128 jstring str = path->concat (JvNewStringLatin1 (": "));
129 str = str->concat (JvNewStringLatin1 (msg));
130 _Jv_Throw (new UnsatisfiedLinkError (str));
135 void *onload = lt_dlsym (h, "JNI_OnLoad");
138 JavaVM *vm = _Jv_GetJavaVM ();
144 jint vers = ((jint (*) (JavaVM *, void *)) onload) (vm, NULL);
145 if (vers != JNI_VERSION_1_1 && vers != JNI_VERSION_1_2)
147 // FIXME: unload the library.
148 _Jv_Throw (new UnsatisfiedLinkError (JvNewStringLatin1 ("unrecognized version from JNI_OnLoad")));
152 _Jv_Throw (new UnknownError
153 (JvNewStringLatin1 (do_search
154 ? "Runtime.loadLibrary not implemented"
155 : "Runtime.load not implemented")));
156 #endif /* USE_LTDL */
160 java::lang::Runtime::loadLibraryInternal (jstring lib)
162 JvSynchronize sync (this);
163 using namespace java::lang;
165 jint len = _Jv_GetStringUTFLength (lib);
167 jsize total = JvGetStringUTFRegion (lib, 0, lib->length(), buf);
169 // FIXME: make sure path is absolute.
170 lt_dlhandle h = lt_dlopenext (buf);
176 #endif /* USE_LTDL */
180 java::lang::Runtime::init (void)
182 finalize_on_exit = false;
189 java::lang::Runtime::runFinalization (void)
191 _Jv_RunFinalizers ();
195 java::lang::Runtime::totalMemory (void)
197 return _Jv_GCTotalMemory ();
201 java::lang::Runtime::traceInstructions (jboolean)
207 java::lang::Runtime::traceMethodCalls (jboolean)