* configure.ac: Default to AC_ENABLE_SHARED and AC_DISABLE_STATIC.
[cacao.git] / src / native / vm / VMRuntime.c
index cad08aa4440678f56588bf4c5ccf6e2c45472262..70f910acc35f0bce72e8566fecf0a736bfd6b32c 100644 (file)
@@ -1,9 +1,9 @@
-/* native/vm/VMRuntime.c - java/lang/VMRuntime
+/* src/native/vm/VMRuntime.c - java/lang/VMRuntime
 
-   Copyright (C) 1996-2005 R. Grafl, A. Krall, C. Kruegel, C. Oates,
-   R. Obermaisser, M. Platter, M. Probst, S. Ring, E. Steiner,
-   C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich, J. Wenninger,
-   Institut f. Computersprachen - TU Wien
+   Copyright (C) 1996-2005, 2006 R. Grafl, A. Krall, C. Kruegel,
+   C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
+   E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
+   J. Wenninger, Institut f. Computersprachen - TU Wien
 
    This file is part of CACAO.
 
 
    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
-   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
-   02111-1307, USA.
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+   02110-1301, USA.
 
-   Contact: cacao@complang.tuwien.ac.at
+   Contact: cacao@cacaojvm.org
 
    Authors: Roman Obermaiser
 
    Changes: Joseph Wenninger
             Christian Thalinger
 
-   $Id: VMRuntime.c 1903 2005-02-08 14:59:35Z twisti $
+   $Id: VMRuntime.c 4530 2006-02-21 09:11:53Z twisti $
 
 */
 
 
 #include "config.h"
 
+#include <assert.h>
 #include <string.h>
 #include <stdlib.h>
-#include <unistd.h>
 #include <sys/utsname.h>
 
+#if !defined(WITH_STATIC_CLASSPATH)
+# include <ltdl.h>
+#endif
+
 #if defined(__DARWIN__)
+# define OS_INLINE    /* required for <libkern/ppc/OSByteOrder.h> */
 # include <mach/mach.h>
 #endif
 
-#if !defined(STATIC_CLASSPATH)
-# include <dlfcn.h>
-#endif
+#include "vm/types.h"
 
-#include "cacao/cacao.h"
 #include "mm/boehm.h"
 #include "mm/memory.h"
 #include "native/jni.h"
 #include "native/include/java_io_File.h"
 #include "native/include/java_lang_String.h"
 #include "native/include/java_lang_Process.h"
-#include "native/include/java_util_Properties.h"     /* java_lang_VMRuntime.h */
-#include "native/include/java_lang_VMRuntime.h"
 #include "toolbox/logging.h"
 #include "vm/builtin.h"
 #include "vm/exceptions.h"
 #include "vm/loader.h"
-#include "vm/tables.h"
+#include "vm/stringlocal.h"
+#include "vm/vm.h"
 
 
 /* this should work on BSD */
 #endif
 */
 
-#undef JOWENN_DEBUG
 
 /* should we run all finalizers on exit? */
 static bool finalizeOnExit = false;
 
 
-/*
- * Class:     java/lang/VMRuntime
- * Method:    execInternal
- * Signature: ([Ljava/lang/String;[Ljava/lang/String;Ljava/io/File;)Ljava/lang/Process;
- */
-JNIEXPORT java_lang_Process* JNICALL Java_java_lang_VMRuntime_execInternal(JNIEnv *env, jclass clazz, java_objectarray *cmd, java_objectarray *shellenv, java_io_File *workingdir)
-{
-       log_text("Java_java_lang_Runtime_execInternal called");
-
-       return NULL;
-}
-
-
 /*
  * Class:     java/lang/VMRuntime
  * Method:    exitInternal
@@ -102,12 +89,12 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_exit(JNIEnv *env, jclass clazz,
        if (finalizeOnExit)
                gc_finalize_all();
 
-       cacao_shutdown(par1);
+       vm_shutdown(par1);
 }
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    freeMemory
  * Signature: ()J
  */
@@ -118,7 +105,29 @@ JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_freeMemory(JNIEnv *env, jclass cla
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
+ * Method:    totalMemory
+ * Signature: ()J
+ */
+JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
+{
+       return gc_get_heap_size();
+}
+
+
+/*
+ * Class:     java_lang_VMRuntime
+ * Method:    maxMemory
+ * Signature: ()J
+ */
+JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
+{
+       return gc_get_max_heap_size();
+}
+
+
+/*
+ * Class:     java/lang/VMRuntime
  * Method:    gc
  * Signature: ()V
  */
@@ -129,7 +138,7 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_gc(JNIEnv *env, jclass clazz)
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    runFinalization
  * Signature: ()V
  */
@@ -140,21 +149,20 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalization(JNIEnv *env, jcl
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    runFinalizersOnExit
  * Signature: (Z)V
  */
 JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizersOnExit(JNIEnv *env, jclass clazz, s4 value)
 {
-#ifdef __GNUC__
-#warning threading
-#endif
+       /* XXX threading */
+
        finalizeOnExit = value;
 }
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    runFinalizationsForExit
  * Signature: ()V
  */
@@ -172,18 +180,7 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_runFinalizationForExit(JNIEnv *e
 
 
 /*
- * Class:     java/lang/Runtime
- * Method:    totalMemory
- * Signature: ()J
- */
-JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_totalMemory(JNIEnv *env, jclass clazz)
-{
-       return gc_get_heap_size();
-}
-
-
-/*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    traceInstructions
  * Signature: (Z)V
  */
@@ -194,7 +191,7 @@ JNIEXPORT void JNICALL Java_java_lang_VMRuntime_traceInstructions(JNIEnv *env, j
 
 
 /*
- * Class:     java/lang/Runtime
+ * Class:     java/lang/VMRuntime
  * Method:    traceMethodCalls
  * Signature: (Z)V
  */
@@ -257,48 +254,60 @@ JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_availableProcessors(JNIEnv *env, j
  */
 JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass clazz, java_lang_String *filename, java_lang_ClassLoader *loader)
 {
-       int retVal=0;
-
-#ifdef JOWENN_DEBUG
-       char *buffer;
-       int buffer_len;
+#if !defined(WITH_STATIC_CLASSPATH)
+       utf         *name;
+       lt_dlhandle  handle;
+       lt_ptr       onload;
+       s4           version;
 #endif
-       utf *data;
 
-#ifdef JOWENN_DEBUG
-       log_text("Java_java_lang_VMRuntime_nativeLoad");
-#endif
+       if (filename == NULL) {
+               exceptions_throw_nullpointerexception();
+               return 0;
+       }
+
+#if defined(WITH_STATIC_CLASSPATH)
+       return 1;
+#else /* defined(WITH_STATIC_CLASSPATH) */
+       name = javastring_toutf(filename, 0);
 
-       data = javastring_toutf(filename, 0);
-       
-       if (!data) {
-               log_text("nativeLoad: Error: empty string");
+       /* is the library already loaded? */
+
+       if (native_hashtable_library_find(name, (java_objectheader *) loader))
                return 1;
+
+       /* try to open the library */
+
+       if (!(handle = lt_dlopen(name->text)))
+               return 0;
+
+       /* resolve JNI_OnLoad function */
+
+       if ((onload = lt_dlsym(handle, "JNI_OnLoad"))) {
+               JNIEXPORT s4 (JNICALL *JNI_OnLoad) (JavaVM *, void *);
+               JavaVM *vm;
+
+               JNI_OnLoad = (JNIEXPORT s4 (JNICALL *)(JavaVM *, void *)) (ptrint) onload;
+
+               (*env)->GetJavaVM(env, &vm);
+
+               version = JNI_OnLoad(vm, NULL);
+
+               /* if the version is not 1.2 and not 1.4 the library cannot be loaded */
+
+               if ((version != JNI_VERSION_1_2) && (version != JNI_VERSION_1_4)) {
+                       lt_dlclose(handle);
+
+                       return 0;
+               }
        }
-       
-#if JOWENN_DEBUG       
-       buffer_len = utf_strlen(data) + 40;
-
-       buffer = MNEW(char, buffer_len);
-       strcpy(buffer, "Java_java_lang_VMRuntime_nativeLoad:");
-       utf_sprint(buffer + strlen((char *) data), data);
-       log_text(buffer);       
-        
-  
-       MFREE(buffer, char, buffer_len);
-#endif
 
-#ifndef STATIC_CLASSPATH
-       /*here it could be interesting to store the references in a list eg for nicely cleaning up or for certain platforms*/
-        if (dlopen(data->text,RTLD_NOW | RTLD_GLOBAL)) {
-               /*log_text("LIBLOADED");*/
-                retVal=1;
-        }
-#else
-       retVal=1;
-#endif
+       /* insert the library name into the library hash */
+
+       native_hashtable_library_add(name, (java_objectheader *) loader, handle);
 
-       return retVal;
+       return 1;
+#endif /* defined(WITH_STATIC_CLASSPATH) */
 }
 
 
@@ -309,51 +318,54 @@ JNIEXPORT s4 JNICALL Java_java_lang_VMRuntime_nativeLoad(JNIEnv *env, jclass cla
  */
 JNIEXPORT java_lang_String* JNICALL Java_java_lang_VMRuntime_mapLibraryName(JNIEnv *env, jclass clazz, java_lang_String *libname)
 {
-       char *buffer;
-       int buffer_len;
-       utf *u;
+       utf              *u;
+       char             *buffer;
+       s4                buffer_len;
+       s4                dumpsize;
        java_lang_String *s;
 
        if (!libname) {
-               *exceptionptr = new_nullpointerexception();
+               exceptions_throw_nullpointerexception();
                return NULL;
        }
 
        u = javastring_toutf(libname, 0);
-       
-       if (!u) {
-               log_text("nativeGetLibName: Error: empty string");
-               return 0;;
-       }
-       
-       buffer_len = utf_strlen(u) + 6 /*lib .so */ +1 /*0*/;
-       buffer = MNEW(char, buffer_len);
 
-       sprintf(buffer, "lib");
-       utf_sprint(buffer + 3, u);
-       strcat(buffer, ".so");
+       /* calculate length of library name */
 
-#ifdef JOWENN_DEBUG
-       log_text("nativeGetLibName:");
-       log_text(buffer);
+       buffer_len = strlen("lib");
+
+       buffer_len += utf_strlen(u);
+
+#if defined(__DARWIN__)
+       buffer_len += strlen(".dylib");
+#else
+       buffer_len += strlen(".so");
 #endif
-       
-       s = javastring_new_char(buffer);        
 
-       MFREE(buffer, char, buffer_len);
+       buffer_len += strlen("0");
 
-       return s;
-}
+       dumpsize = dump_size();
+       buffer = DMNEW(char, buffer_len);
 
+       /* generate library name */
 
-/*
- * Class:     java_lang_VMRuntime
- * Method:    maxMemory
- * Signature: ()J
- */
-JNIEXPORT s8 JNICALL Java_java_lang_VMRuntime_maxMemory(JNIEnv *env, jclass clazz)
-{
-       return gc_get_max_heap_size();
+       strcpy(buffer, "lib");
+       utf_strcat(buffer, u);
+
+#if defined(__DARWIN__)
+       strcat(buffer, ".dylib");
+#else
+       strcat(buffer, ".so");
+#endif
+
+       s = javastring_new_char(buffer);
+
+       /* release memory */
+
+       dump_release(dumpsize);
+
+       return s;
 }