Merged.
[cacao.git] / src / native / vm / openjdk / jvm.cpp
index f58bc42ea299718c039dec67ec36babb9956d2ee..1bbdd3ea40c39472427f9ac537ecbfba18d06d51 100644 (file)
 #include <sys/stat.h>
 #include <sys/types.h>
 
-#include "vm/types.h"
+// Include our JNI header before the JVM headers, because the JVM
+// headers include jni.h and we want to override the typedefs in
+// jni.h.
+#include "native/jni.hpp"
+
+// We include jvm_md.h before jvm.h as the latter includes the former.
+#include INCLUDE_JVM_MD_H
+#include INCLUDE_JVM_H
 
 #include "mm/memory.h"
 
-#include "native/jni.hpp"
 #include "native/llni.h"
-#include "native/native.h"
+#include "native/native.hpp"
 
 #include "native/vm/reflection.hpp"
 
-#include "native/vm/openjdk/hpi.h"
+#include "native/vm/openjdk/hpi.hpp"
+#include "native/vm/openjdk/management.hpp"
 
-#include "threads/lock-common.h"
+#include "threads/lock.hpp"
 #include "threads/thread.hpp"
 
 #include "toolbox/logging.h"
 #include "toolbox/list.hpp"
 
-#include "vm/array.h"
+#include "vm/array.hpp"
 
 #if defined(ENABLE_ASSERTION)
 #include "vm/assertion.hpp"
 #endif
 
 #include "vm/jit/builtin.hpp"
-#include "vm/classcache.h"
+#include "vm/classcache.hpp"
 #include "vm/exceptions.hpp"
 #include "vm/global.h"
 #include "vm/globals.hpp"
-#include "vm/initialize.h"
+#include "vm/initialize.hpp"
 #include "vm/javaobjects.hpp"
 #include "vm/options.h"
 #include "vm/os.hpp"
 #include "vm/package.hpp"
 #include "vm/primitive.hpp"
 #include "vm/properties.hpp"
-#include "vm/resolve.h"
+#include "vm/resolve.hpp"
 #include "vm/signallocal.h"
 #include "vm/string.hpp"
 #include "vm/vm.hpp"
 #endif
 
 
-typedef struct {
-    /* Naming convention of RE build version string: n.n.n[_uu[c]][-<identifier>]-bxx */
-    unsigned int jvm_version;   /* Consists of major, minor, micro (n.n.n) */
-                                /* and build number (xx) */
-    unsigned int update_version : 8;         /* Update release version (uu) */
-    unsigned int special_update_version : 8; /* Special update release version (c) */
-    unsigned int reserved1 : 16; 
-    unsigned int reserved2; 
-
-    /* The following bits represents JVM supports that JDK has dependency on.
-     * JDK can use these bits to determine which JVM version
-     * and support it has to maintain runtime compatibility.
-     *
-     * When a new bit is added in a minor or update release, make sure
-     * the new bit is also added in the main/baseline.
-     */
-    unsigned int is_attachable : 1;
-    unsigned int : 31;
-    unsigned int : 32;
-    unsigned int : 32;
-} jvm_version_info;
-
-
-/*
- * A structure used to a capture exception table entry in a Java method.
- */
-typedef struct {
-    jint start_pc;
-    jint end_pc;
-    jint handler_pc;
-    jint catchType;
-} JVM_ExceptionTableEntryType;
-
-
 // Interface functions are exported as C functions.
 extern "C" {
 
@@ -672,21 +645,23 @@ void JVM_DisableCompiler(JNIEnv *env, jclass compCls)
 
 /* JVM_GetLastErrorString */
 
-jint JVM_GetLastErrorString(char *buf, int len)
+jint JVM_GetLastErrorString(charbuf, int len)
 {
        TRACEJVMCALLS(("JVM_GetLastErrorString(buf=%p, len=%d", buf, len));
 
-       return hpi_system->GetLastErrorString(buf, len);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_system().GetLastErrorString(buf, len);
 }
 
 
 /* JVM_NativePath */
 
-char *JVM_NativePath(char *path)
+char *JVM_NativePath(charpath)
 {
        TRACEJVMCALLS(("JVM_NativePath(path=%s)", path));
 
-       return hpi_file->NativePath(path);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().NativePath(path);
 }
 
 
@@ -2077,13 +2052,14 @@ jboolean JVM_IsSameClassPackage(JNIEnv *env, jclass class1, jclass class2)
  */
 #define JVM_EEXIST       -100
 
-jint JVM_Open(const char *fname, jint flags, jint mode)
+jint JVM_Open(const charfname, jint flags, jint mode)
 {
        int result;
 
        TRACEJVMCALLS(("JVM_Open(fname=%s, flags=%d, mode=%d)", fname, flags, mode));
 
-       result = hpi_file->Open(fname, flags, mode);
+       HPI& hpi = VM::get_current()->get_hpi();
+       result = hpi.get_file().Open(fname, flags, mode);
 
        if (result >= 0) {
                return result;
@@ -2105,37 +2081,41 @@ jint JVM_Close(jint fd)
 {
        TRACEJVMCALLS(("JVM_Close(fd=%d)", fd));
 
-       return hpi_file->Close(fd);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().Close(fd);
 }
 
 
 /* JVM_Read */
 
-jint JVM_Read(jint fd, char *buf, jint nbytes)
+jint JVM_Read(jint fd, charbuf, jint nbytes)
 {
        TRACEJVMCALLS(("JVM_Read(fd=%d, buf=%p, nbytes=%d)", fd, buf, nbytes));
 
-       return (jint) hpi_file->Read(fd, buf, nbytes);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return (jint) hpi.get_file().Read(fd, buf, nbytes);
 }
 
 
 /* JVM_Write */
 
-jint JVM_Write(jint fd, char *buf, jint nbytes)
+jint JVM_Write(jint fd, charbuf, jint nbytes)
 {
        TRACEJVMCALLS(("JVM_Write(fd=%d, buf=%s, nbytes=%d)", fd, buf, nbytes));
 
-       return (jint) hpi_file->Write(fd, buf, nbytes);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return (jint) hpi.get_file().Write(fd, buf, nbytes);
 }
 
 
 /* JVM_Available */
 
-jint JVM_Available(jint fd, jlong *pbytes)
+jint JVM_Available(jint fd, jlongpbytes)
 {
        TRACEJVMCALLS(("JVM_Available(fd=%d, pbytes=%p)", fd, pbytes));
 
-       return hpi_file->Available(fd, pbytes);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().Available(fd, pbytes);
 }
 
 
@@ -2145,7 +2125,8 @@ jlong JVM_Lseek(jint fd, jlong offset, jint whence)
 {
        TRACEJVMCALLS(("JVM_Lseek(fd=%d, offset=%ld, whence=%d)", fd, offset, whence));
 
-       return hpi_file->Seek(fd, (off_t) offset, whence);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().Seek(fd, (off_t) offset, whence);
 }
 
 
@@ -2155,7 +2136,8 @@ jint JVM_SetLength(jint fd, jlong length)
 {
        TRACEJVMCALLS(("JVM_SetLength(fd=%d, length=%ld)", length));
 
-       return hpi_file->SetLength(fd, length);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().SetLength(fd, length);
 }
 
 
@@ -2165,7 +2147,8 @@ jint JVM_Sync(jint fd)
 {
        TRACEJVMCALLS(("JVM_Sync(fd=%d)", fd));
 
-       return hpi_file->Sync(fd);
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.get_file().Sync(fd);
 }
 
 
@@ -2685,7 +2668,8 @@ jint JVM_InitializeSocketLibrary()
 {
        TRACEJVMCALLS(("JVM_InitializeSocketLibrary()"));
 
-       return hpi_initialize_socket_library();
+       HPI& hpi = VM::get_current()->get_hpi();
+       return hpi.initialize_socket_library();
 }
 
 
@@ -2913,17 +2897,15 @@ struct protoent *JVM_GetProtoByName(char* name)
 
 /* JVM_LoadLibrary */
 
-void *JVM_LoadLibrary(const char *name)
+void* JVM_LoadLibrary(const char* name)
 {
-       utf*  u;
-       void* handle;
-
        TRACEJVMCALLSENTER(("JVM_LoadLibrary(name=%s)", name));
 
-       u = utf_new_char(name);
-
-       handle = native_library_open(u);
+       utf* u = utf_new_char(name);
 
+       NativeLibrary nl(u);
+       void* handle = nl.open();
+       
        TRACEJVMCALLSEXIT(("->%p", handle));
 
        return handle;
@@ -2936,19 +2918,21 @@ void JVM_UnloadLibrary(void* handle)
 {
        TRACEJVMCALLS(("JVM_UnloadLibrary(handle=%p)", handle));
 
-       native_library_close(handle);
+       NativeLibrary nl(handle);
+       nl.close();
 }
 
 
 /* JVM_FindLibraryEntry */
 
-void *JVM_FindLibraryEntry(void *handle, const char *name)
+void *JVM_FindLibraryEntry(void* handle, const char* name)
 {
        void* symbol;
 
        TRACEJVMCALLSENTER(("JVM_FindLibraryEntry(handle=%p, name=%s)", handle, name));
 
-       symbol = hpi_library->FindLibraryEntry(handle, name);
+       HPI& hpi = VM::get_current()->get_hpi();
+       symbol = hpi.get_library().FindLibraryEntry(handle, name);
 
        TRACEJVMCALLSEXIT(("->%p", symbol));
 
@@ -2990,35 +2974,31 @@ jstring JVM_InternString(JNIEnv *env, jstring str)
 
 JNIEXPORT void* JNICALL JVM_RawMonitorCreate(void)
 {
-       java_object_t *o;
-
        TRACEJVMCALLS(("JVM_RawMonitorCreate()"));
 
-       o = NEW(java_object_t);
-
-       lock_init_object_lock(o);
+       Mutex* m = new Mutex();
 
-       return o;
+       return m;
 }
 
 
 /* JVM_RawMonitorDestroy */
 
-JNIEXPORT void JNICALL JVM_RawMonitorDestroy(void *mon)
+JNIEXPORT void JNICALL JVM_RawMonitorDestroy(voidmon)
 {
        TRACEJVMCALLS(("JVM_RawMonitorDestroy(mon=%p)", mon));
 
-       FREE(mon, java_object_t);
+       delete ((Mutex*) mon);
 }
 
 
 /* JVM_RawMonitorEnter */
 
-JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon)
+JNIEXPORT jint JNICALL JVM_RawMonitorEnter(voidmon)
 {
        TRACEJVMCALLS(("JVM_RawMonitorEnter(mon=%p)", mon));
 
-       (void) lock_monitor_enter((java_object_t *) mon);
+       ((Mutex*) mon)->lock();
 
        return 0;
 }
@@ -3026,11 +3006,11 @@ JNIEXPORT jint JNICALL JVM_RawMonitorEnter(void *mon)
 
 /* JVM_RawMonitorExit */
 
-JNIEXPORT void JNICALL JVM_RawMonitorExit(void *mon)
+JNIEXPORT void JNICALL JVM_RawMonitorExit(voidmon)
 {
        TRACEJVMCALLS(("JVM_RawMonitorExit(mon=%p)", mon));
 
-       (void) lock_monitor_exit((java_object_t *) mon);
+       ((Mutex*) mon)->unlock();
 }
 
 
@@ -3263,9 +3243,7 @@ void *JVM_GetManagement(jint version)
 {
        TRACEJVMCALLS(("JVM_GetManagement(version=%d)", version));
 
-       /* TODO We current don't support the management interface. */
-
-       return NULL;
+       return Management::get_jmm_interface(version);
 }
 
 
@@ -3380,6 +3358,24 @@ jintArray JVM_GetThreadStateValues(JNIEnv* env, jint javaThreadState)
 /*             array_intarray_element_set(ia, 2, PARKED); */
                break; 
 
+    case THREAD_STATE_PARKED:
+               ia = builtin_newarray_int(2);
+
+               if (ia == NULL)
+                       return NULL;
+
+               array_intarray_element_set(ia, 0, THREAD_STATE_PARKED);
+               break; 
+
+    case THREAD_STATE_TIMED_PARKED:
+               ia = builtin_newarray_int(2);
+
+               if (ia == NULL)
+                       return NULL;
+
+               array_intarray_element_set(ia, 0, THREAD_STATE_TIMED_PARKED);
+               break; 
+
     case THREAD_STATE_TERMINATED:
                ia = builtin_newarray_int(1);
 
@@ -3475,7 +3471,6 @@ jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArra
                        return NULL;
 
                s = javastring_new(utf_new_char("WAITING.OBJECT_WAIT"));
-/*             s = javastring_new(utf_new_char("WAITING.PARKED")); */
 
                if (s == NULL)
                        return NULL;
@@ -3492,7 +3487,6 @@ jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArra
 
 /*             s = javastring_new(utf_new_char("TIMED_WAITING.SLEEPING")); */
                s = javastring_new(utf_new_char("TIMED_WAITING.OBJECT_WAIT"));
-/*             s = javastring_new(utf_new_char("TIMED_WAITING.PARKED")); */
 
                if (s == NULL)
                        return NULL;
@@ -3502,6 +3496,36 @@ jobjectArray JVM_GetThreadStateNames(JNIEnv* env, jint javaThreadState, jintArra
 /*             array_objectarray_element_set(oa, 2, s); */
                break; 
 
+    case THREAD_STATE_PARKED:
+               oa = builtin_anewarray(2, class_java_lang_String);
+
+               if (oa == NULL)
+                       return NULL;
+
+               s = javastring_new(utf_new_char("WAITING.PARKED"));
+
+               if (s == NULL)
+                       return NULL;
+
+               array_objectarray_element_set(oa, 0, s);
+/*             array_objectarray_element_set(oa, 1, s); */
+               break; 
+
+    case THREAD_STATE_TIMED_PARKED:
+               oa = builtin_anewarray(3, class_java_lang_String);
+
+               if (oa == NULL)
+                       return NULL;
+
+               s = javastring_new(utf_new_char("TIMED_WAITING.PARKED"));
+
+               if (s == NULL)
+                       return NULL;
+
+               array_objectarray_element_set(oa, 0, s);
+/*             array_objectarray_element_set(oa, 1, s); */
+               break; 
+
     case THREAD_STATE_TERMINATED:
                oa = builtin_anewarray(1, class_java_lang_String);