* m4/classpath.m4: Renamed --with-classpath* options to
[cacao.git] / src / native / native.c
index dacc6dfcb4f80cd3b4d01e05e447a2e047237b7f..9a4cd343f55d984a39cd2b519d419967c23e6e34 100644 (file)
@@ -1,9 +1,7 @@
 /* src/native/native.c - table of native functions
 
-   Copyright (C) 1996-2005, 2006, 2007 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, 2007, 2008
+   CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
 
    This file is part of CACAO.
 
@@ -91,6 +89,8 @@ static s4 native_tree_native_methods_comparator(const void *treenode, const void
 
 bool native_init(void)
 {
+       TRACESUBSYSTEMINITIALIZATION("native_init");
+
 #if defined(ENABLE_LTDL)
        /* initialize libltdl */
 
@@ -171,12 +171,12 @@ static utf *native_make_overloaded_function(utf *name, utf *descriptor)
        char *utf_ptr;
        u2    c;
        s4    i;
-       s4    dumpsize;
        utf  *u;
+       int32_t dumpmarker;
 
        /* mark memory */
 
-       dumpsize = dump_size();
+       DMARKER;
 
        utf_ptr = descriptor->text;
        namelen = strlen(name->text) + strlen("__") + strlen("0");
@@ -268,7 +268,7 @@ static utf *native_make_overloaded_function(utf *name, utf *descriptor)
 
        /* release memory */
 
-       dump_release(dumpsize);
+       DRELEASE;
 
        return u;
 }
@@ -351,12 +351,12 @@ static utf *native_method_symbol(utf *classname, utf *methodname)
        char *utf_endptr;
        u2    c;
        u4    pos;
-       s4    dumpsize;
        utf  *u;
+       int32_t dumpmarker;
 
        /* mark memory */
 
-       dumpsize = dump_size();
+       DMARKER;
 
        /* Calculate length of native function name.  We multiply the
           class and method name length by 6 as this is the maxium
@@ -412,7 +412,7 @@ static utf *native_method_symbol(utf *classname, utf *methodname)
 
        /* release memory */
 
-       dump_release(dumpsize);
+       DRELEASE;
 
        return u;
 }
@@ -475,7 +475,7 @@ static functionptr native_method_find(methodinfo *m)
 
        /* fill the temporary structure used for searching the tree */
 
-       tmpnmn.classname  = m->class->name;
+       tmpnmn.classname  = m->clazz->name;
        tmpnmn.name       = m->name;
        tmpnmn.descriptor = m->descriptor;
 
@@ -508,13 +508,13 @@ functionptr native_method_resolve(methodinfo *m)
        utf                            *newname;
        functionptr                     f;
 #if defined(ENABLE_LTDL)
-       classloader                    *cl;
+       classloader_t                  *cl;
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne;
        u4                              key;    /* hashkey                        */
        u4                              slot;   /* slot in hashtable              */
 #endif
-#if defined(WITH_CLASSPATH_SUN)
+#if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
        methodinfo                     *method_findNative;
        java_handle_t                  *s;
 #endif
@@ -523,7 +523,7 @@ functionptr native_method_resolve(methodinfo *m)
 
        if (opt_verbosejni) {
                printf("[Dynamic-linking native method ");
-               utf_display_printable_ascii_classname(m->class->name);
+               utf_display_printable_ascii_classname(m->clazz->name);
                printf(".");
                utf_display_printable_ascii(m->name);
                printf(" ... ");
@@ -531,7 +531,7 @@ functionptr native_method_resolve(methodinfo *m)
 
        /* generate method symbol string */
 
-       name = native_method_symbol(m->class->name, m->name);
+       name = native_method_symbol(m->clazz->name, m->name);
 
        /* generate overloaded function (having the types in it's name)           */
 
@@ -545,7 +545,7 @@ functionptr native_method_resolve(methodinfo *m)
 #if defined(ENABLE_LTDL)
        /* Get the classloader. */
 
-       cl = class_get_classloader(m->class);
+       cl = class_get_classloader(m->clazz);
 
        /* normally addresses are aligned to 4, 8 or 16 bytes */
 
@@ -572,7 +572,7 @@ functionptr native_method_resolve(methodinfo *m)
                le = le->hashlink;
        }
 
-# if defined(WITH_CLASSPATH_SUN)
+# if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
        if (f == NULL) {
                /* We can resolve the function directly from
                   java.lang.ClassLoader as it's a static function. */
@@ -732,7 +732,7 @@ void native_library_close(lt_dlhandle handle)
 *******************************************************************************/
 
 #if defined(ENABLE_LTDL)
-void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
+void native_library_add(utf *filename, classloader_t *loader, lt_dlhandle handle)
 {
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne; /* library name                       */
@@ -815,7 +815,7 @@ void native_library_add(utf *filename, classloader *loader, lt_dlhandle handle)
 
 #if defined(ENABLE_LTDL)
 hashtable_library_name_entry *native_library_find(utf *filename,
-                                                                                                 classloader *loader)
+                                                                                                 classloader_t *loader)
 {
        hashtable_library_loader_entry *le;
        hashtable_library_name_entry   *ne; /* library name                       */
@@ -860,6 +860,86 @@ hashtable_library_name_entry *native_library_find(utf *filename,
 #endif
 
 
+/* native_library_load *********************************************************
+
+   Load a native library and initialize it, if possible.
+
+   IN:
+       name ... name of the library
+          cl ..... classloader which loads this library
+
+   RETURN:
+       1 ... library loaded successfully
+       0 ... error
+
+*******************************************************************************/
+
+int native_library_load(JNIEnv *env, utf *name, classloader_t *cl)
+{
+#if defined(ENABLE_LTDL)
+       lt_dlhandle        handle;
+# if defined(ENABLE_JNI)
+       lt_ptr             onload;
+       int32_t            version;
+# endif
+
+       if (name == NULL) {
+               exceptions_throw_nullpointerexception();
+               return 0;
+       }
+
+       /* Is the library already loaded? */
+
+       if (native_library_find(name, cl) != NULL)
+               return 1;
+
+       /* Open the library. */
+
+       handle = native_library_open(name);
+
+       if (handle == NULL)
+               return 0;
+
+# if defined(ENABLE_JNI)
+       /* Resolve JNI_OnLoad function. */
+
+       onload = lt_dlsym(handle, "JNI_OnLoad");
+
+       if (onload != NULL) {
+               JNIEXPORT int32_t (JNICALL *JNI_OnLoad) (JavaVM *, void *);
+               JavaVM *vm;
+
+               JNI_OnLoad = (JNIEXPORT int32_t (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;
+               }
+       }
+# endif
+
+       /* Insert the library name into the library hash. */
+
+       native_library_add(name, cl, handle);
+
+       return 1;
+#else
+       vm_abort("native_library_load: not available");
+
+       /* Keep compiler happy. */
+
+       return 0;
+#endif
+}
+
+
 /* native_new_and_init *********************************************************
 
    Creates a new object on the heap and calls the initializer.