* src/vm/vm.c, src/vm/vm.h: Moved to .cpp.
[cacao.git] / src / native / jni.c
index 392126e17f902b09a60473c47487cf156d47c358..592a16350d690640a5ce3ffacf2b6db34c2b22d5 100644 (file)
@@ -31,7 +31,7 @@
 
 #include "vm/types.h"
 
-#include "mm/gc-common.h"
+#include "mm/gc.hpp"
 #include "mm/memory.h"
 
 #include "native/jni.h"
@@ -105,10 +105,10 @@ struct java_lang_ClassLoader;
 #include "vm/exceptions.h"
 #include "vm/global.h"
 #include "vm/initialize.h"
-#include "vm/primitive.h"
+#include "vm/primitive.hpp"
 #include "vm/resolve.h"
 #include "vm/stringlocal.h"
-#include "vm/vm.h"
+#include "vm/vm.hpp"
 
 #include "vm/jit/argument.h"
 #include "vm/jit/asmpart.h"
@@ -123,14 +123,36 @@ struct java_lang_ClassLoader;
 /* debug **********************************************************************/
 
 #if !defined(NDEBUG)
-# define TRACEJNICALLS(text)                                   \
+
+# define TRACEJNICALLS(x)                                              \
     do {                                                                               \
         if (opt_TraceJNICalls) {                               \
-            log_println text;                                  \
+            log_println x;                                             \
         }                                                                              \
     } while (0)
+
+# define TRACEJNICALLSENTER(x)                                                                 \
+    do {                                                                                                               \
+        if (opt_TraceJNICalls) {                                                               \
+                       log_start();                                                                            \
+            log_print x;                                                                               \
+        }                                                                                                              \
+    } while (0)
+
+# define TRACEJNICALLSEXIT(x)                                                                  \
+    do {                                                                                                               \
+        if (opt_TraceJNICalls) {                                                               \
+                       log_print x;                                                                            \
+                       log_finish();                                                                           \
+        }                                                                                                              \
+    } while (0)
+
 #else
-# define TRACEJNICALLS(text)
+
+# define TRACEJNICALLS(x)
+# define TRACEJNICALLSENTER(x)
+# define TRACEJNICALLSEXIT(x)
+
 #endif
 
 
@@ -1310,14 +1332,14 @@ jobject _Jv_JNI_AllocObject(JNIEnv *env, jclass clazz)
 
 *******************************************************************************/
 
-jobject _Jv_JNI_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
+jobject jni_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
 {
        java_handle_t *o;
        classinfo     *c;
        methodinfo    *m;
        va_list        ap;
 
-       STATISTICS(jniinvokation());
+       TRACEJNICALLSENTER(("jni_NewObject(env=%p, clazz=%p, methodID=%p, ...)", env, clazz, methodID));
 
        c = LLNI_classinfo_unwrap(clazz);
        m = (methodinfo *) methodID;
@@ -1335,6 +1357,8 @@ jobject _Jv_JNI_NewObject(JNIEnv *env, jclass clazz, jmethodID methodID, ...)
        _Jv_jni_CallVoidMethod(o, LLNI_vftbl_direct(o), m, ap);
        va_end(ap);
 
+       TRACEJNICALLSEXIT(("->%p", o));
+
        return jni_NewLocalRef(env, (jobject) o);
 }
 
@@ -3205,7 +3229,7 @@ jint _Jv_JNI_GetJavaVM(JNIEnv *env, JavaVM **vm)
 {
        STATISTICS(jniinvokation());
 
-    *vm = (JavaVM *) _Jv_jvm;
+    *vm = VM_get_javavm();
 
        return 0;
 }
@@ -3380,14 +3404,14 @@ void _Jv_JNI_DeleteWeakGlobalRef(JNIEnv* env, jweak ref)
 
 *******************************************************************************/
     
-jobject _Jv_JNI_NewGlobalRef(JNIEnv* env, jobject obj)
+jobject jni_NewGlobalRef(JNIEnv* env, jobject obj)
 {
        hashtable_global_ref_entry *gre;
        u4   key;                           /* hashkey                            */
        u4   slot;                          /* slot in hashtable                  */
        java_handle_t *o;
 
-       STATISTICS(jniinvokation());
+       TRACEJNICALLS(("jni_NewGlobalRef(env=%p, obj=%p)", env, obj));
 
        o = (java_handle_t *) obj;
 
@@ -3420,7 +3444,7 @@ jobject _Jv_JNI_NewGlobalRef(JNIEnv* env, jobject obj)
        /* global ref not found, create a new one */
 
        if (gre == NULL) {
-               gre = NEW(hashtable_global_ref_entry);
+               gre = GCNEW_UNCOLLECTABLE(hashtable_global_ref_entry, 1);
 
 #if defined(ENABLE_GC_CACAO)
                /* register global ref with the GC */
@@ -3462,7 +3486,7 @@ jobject _Jv_JNI_NewGlobalRef(JNIEnv* env, jobject obj)
 
 *******************************************************************************/
 
-void _Jv_JNI_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
+void jni_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
 {
        hashtable_global_ref_entry *gre;
        hashtable_global_ref_entry *prevgre;
@@ -3470,7 +3494,7 @@ void _Jv_JNI_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
        u4   slot;                          /* slot in hashtable                  */
        java_handle_t              *o;
 
-       STATISTICS(jniinvokation());
+       TRACEJNICALLS(("jni_DeleteGlobalRef(env=%p, globalRef=%p)", env, globalRef));
 
        o = (java_handle_t *) globalRef;
 
@@ -3512,7 +3536,7 @@ void _Jv_JNI_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
                                gc_reference_unregister(&(gre->o));
 #endif
 
-                               FREE(gre, hashtable_global_ref_entry);
+                               GCFREE(gre);
                        }
 
                        LLNI_CRITICAL_END;
@@ -3526,7 +3550,7 @@ void _Jv_JNI_DeleteGlobalRef(JNIEnv* env, jobject globalRef)
                gre     = gre->hashlink;            /* next element in external chain */
        }
 
-       log_println("JNI-DeleteGlobalRef: global reference not found");
+       log_println("jni_DeleteGlobalRef: Global reference not found.");
 
        LLNI_CRITICAL_END;
 
@@ -3563,7 +3587,7 @@ jboolean _Jv_JNI_ExceptionCheck(JNIEnv *env)
 
 *******************************************************************************/
 
-jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
+jobject jni_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
 {
 #if defined(ENABLE_JAVASE)
 # if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
@@ -3575,7 +3599,7 @@ jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
        gnu_classpath_Pointer32 *paddress;
 # endif
 
-       TRACEJNICALLS(("_Jv_JNI_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld", env, address, capacity));
+       TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
 
        /* alocate a gnu.classpath.Pointer{32,64} object */
 
@@ -3600,6 +3624,8 @@ jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
 
        /* add local reference and return the value */
 
+       TRACEJNICALLSEXIT(("->%p", nbuf));
+
        return jni_NewLocalRef(env, nbuf);
 
 # elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
@@ -3608,7 +3634,7 @@ jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
        int64_t addr;
        int32_t cap;
 
-       TRACEJNICALLS(("_Jv_JNI_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld", env, address, capacity));
+       TRACEJNICALLSENTER(("jni_NewDirectByteBuffer(env=%p, address=%p, capacity=%ld)", env, address, capacity));
 
        /* Be paranoid about address sign-extension. */
 
@@ -3620,6 +3646,8 @@ jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
 
        /* Add local reference and return the value. */
 
+       TRACEJNICALLSEXIT(("->%p", o));
+
        return jni_NewLocalRef(env, o);
 
 # else
@@ -3627,7 +3655,7 @@ jobject _Jv_JNI_NewDirectByteBuffer(JNIEnv *env, void *address, jlong capacity)
 # endif
 
 #else
-       vm_abort("_Jv_JNI_NewDirectByteBuffer: not implemented in this configuration");
+       vm_abort("jni_NewDirectByteBuffer: Not implemented in this configuration.");
 
        /* keep compiler happy */
 
@@ -3794,7 +3822,7 @@ jint _Jv_JNI_DestroyJavaVM(JavaVM *vm)
 
        TRACEJNICALLS(("_Jv_JNI_DestroyJavaVM(vm=%p)", vm));
 
-       if (vm_created == false)
+       if (VM_is_created() == false)
                return JNI_ERR;
 
     status = vm_destroy(vm);
@@ -3829,7 +3857,7 @@ static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon
        result = thread_current_is_attached();
 
        if (result == true) {
-               *p_env = _Jv_env;
+               *p_env = VM_get_jnienv();
 
                return JNI_OK;
        }
@@ -3842,26 +3870,26 @@ static int jni_attach_current_thread(void **p_env, void *thr_args, bool isdaemon
                        return JNI_EVERSION;
        }
 
-       if (!threads_attach_current_thread(vm_aargs, false))
+       if (!thread_attach_current_external_thread(vm_aargs, false))
                return JNI_ERR;
 
        if (!localref_table_init())
                return JNI_ERR;
 #endif
 
-       *p_env = _Jv_env;
+       *p_env = VM_get_jnienv();
 
        return JNI_OK;
 }
 
 
-jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
+jint jni_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
 {
        int result;
 
-       TRACEJNICALLS(("_Jv_JNI_AttachCurrentThread(vm=%p, p_env=%p, thr_args=%p)", vm, p_env, thr_args));
+       TRACEJNICALLS(("jni_AttachCurrentThread(vm=%p, p_env=%p, thr_args=%p)", vm, p_env, thr_args));
 
-       if (vm_created == false)
+       if (VM_is_created() == false)
                return JNI_ERR;
 
        result = jni_attach_current_thread(p_env, thr_args, false);
@@ -3887,24 +3915,17 @@ jint _Jv_JNI_AttachCurrentThread(JavaVM *vm, void **p_env, void *thr_args)
 
 *******************************************************************************/
 
-jint _Jv_JNI_DetachCurrentThread(JavaVM *vm)
+jint jni_DetachCurrentThread(JavaVM *vm)
 {
 #if defined(ENABLE_THREADS)
-       threadobject *t;
-       bool          result;
-
-       TRACEJNICALLS(("_Jv_JNI_DetachCurrentThread(vm=%p)", vm));
+       bool result;
 
-       t = thread_get_current();
+       TRACEJNICALLS(("jni_DetachCurrentThread(vm=%p)", vm));
 
-       /* Sanity check. */
-
-       assert(t != NULL);
-
-    /* If the given thread has already been detached, this operation
+    /* If the current thread has already been detached, this operation
           is a no-op. */
 
-       result = thread_is_attached(t);
+       result = thread_current_is_attached();
 
        if (result == false)
                return true;
@@ -3916,7 +3937,7 @@ jint _Jv_JNI_DetachCurrentThread(JavaVM *vm)
        if (!localref_table_destroy())
                return JNI_ERR;
 
-       if (!threads_detach_thread(t))
+       if (!thread_detach_current_external_thread())
                return JNI_ERR;
 #endif
 
@@ -3937,7 +3958,7 @@ jint jni_GetEnv(JavaVM *vm, void **env, jint version)
 {
        TRACEJNICALLS(("jni_GetEnv(vm=%p, env=%p, version=%d)", vm, env, version));
 
-       if (vm_created == false) {
+       if (VM_is_created() == false) {
                *env = NULL;
                return JNI_EDETACHED;
        }
@@ -3953,7 +3974,7 @@ jint jni_GetEnv(JavaVM *vm, void **env, jint version)
        /* Check the JNI version. */
 
        if (jni_version_check(version) == true) {
-               *env = _Jv_env;
+               *env = VM_get_jnienv();
                return JNI_OK;
        }
 
@@ -3987,13 +4008,13 @@ jint jni_GetEnv(JavaVM *vm, void **env, jint version)
 
 *******************************************************************************/
 
-jint _Jv_JNI_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *args)
+jint jni_AttachCurrentThreadAsDaemon(JavaVM *vm, void **penv, void *args)
 {
        int result;
 
-       TRACEJNICALLS(("_Jv_JNI_AttachCurrentThreadAsDaemon(vm=%p, penv=%p, args=%p)", vm, penv, args));
+       TRACEJNICALLS(("jni_AttachCurrentThreadAsDaemon(vm=%p, penv=%p, args=%p)", vm, penv, args));
 
-       if (vm_created == false)
+       if (VM_is_created() == false)
                return JNI_ERR;
 
        result = jni_attach_current_thread(penv, args, true);
@@ -4010,10 +4031,10 @@ const struct JNIInvokeInterface_ _Jv_JNIInvokeInterface = {
        NULL,
 
        _Jv_JNI_DestroyJavaVM,
-       _Jv_JNI_AttachCurrentThread,
-       _Jv_JNI_DetachCurrentThread,
+       jni_AttachCurrentThread,
+       jni_DetachCurrentThread,
        jni_GetEnv,
-       _Jv_JNI_AttachCurrentThreadAsDaemon
+       jni_AttachCurrentThreadAsDaemon
 };
 
 
@@ -4044,15 +4065,15 @@ struct JNINativeInterface_ _Jv_JNINativeInterface = {
        jni_PushLocalFrame,
        jni_PopLocalFrame,
 
-       _Jv_JNI_NewGlobalRef,
-       _Jv_JNI_DeleteGlobalRef,
+       jni_NewGlobalRef,
+       jni_DeleteGlobalRef,
        jni_DeleteLocalRef,
        _Jv_JNI_IsSameObject,
        jni_NewLocalRef,
        jni_EnsureLocalCapacity,
 
        _Jv_JNI_AllocObject,
-       _Jv_JNI_NewObject,
+       jni_NewObject,
        _Jv_JNI_NewObjectV,
        _Jv_JNI_NewObjectA,
 
@@ -4284,7 +4305,7 @@ struct JNINativeInterface_ _Jv_JNINativeInterface = {
 
        /* New JNI 1.4 functions. */
 
-       _Jv_JNI_NewDirectByteBuffer,
+       jni_NewDirectByteBuffer,
        _Jv_JNI_GetDirectBufferAddress,
        _Jv_JNI_GetDirectBufferCapacity,
 
@@ -4348,7 +4369,7 @@ jint JNI_GetCreatedJavaVMs(JavaVM **vmBuf, jsize bufLen, jsize *nVMs)
 
        /* We currently only support 1 VM running. */
 
-       vmBuf[0] = (JavaVM *) _Jv_jvm;
+       vmBuf[0] = VM_get_javavm();
        *nVMs    = 1;
 
     return JNI_OK;
@@ -4368,7 +4389,7 @@ jint JNI_CreateJavaVM(JavaVM **p_vm, void **p_env, void *vm_args)
 
        /* actually create the JVM */
 
-       if (!vm_createjvm(p_vm, p_env, vm_args))
+       if (!VM_create(p_vm, p_env, vm_args))
                return JNI_ERR;
 
        return JNI_OK;