Merged with michi branch at rev 1684fe51cf3d.
[cacao.git] / src / native / vm / sun_misc_Unsafe.cpp
index 950df13523d3fd106ff57418e2ce239cf1082fe6..bcbfe0feceb955cc6b29e16bf83e8e49f34a5f0d 100644 (file)
@@ -30,7 +30,7 @@
 
 #include "threads/atomic.hpp"
 
-#include "mm/memory.h"
+#include "mm/memory.hpp"
 
 #include "native/jni.hpp"
 #include "native/llni.h"
@@ -40,6 +40,7 @@
 # include "native/include/sun_misc_Unsafe.h"
 #endif
 
+#include "vm/array.hpp"
 #include "vm/jit/builtin.hpp"
 #include "vm/exceptions.hpp"
 #include "vm/initialize.hpp"
@@ -461,6 +462,39 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putFloat__JF(JNIEnv *env, jobject _t
 }
 
 
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    getDouble
+ * Signature: (J)D
+ */
+JNIEXPORT jdouble JNICALL Java_sun_misc_Unsafe_getDouble__J(JNIEnv *env, jobject _this, jlong address)
+{
+       double *p;
+       double  value;
+
+       p = (double*) (intptr_t) address;
+
+       value = *p;
+
+       return value;
+}
+
+
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    putDouble
+ * Signature: (JD)V
+ */
+JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putDouble__JD(JNIEnv *env, jobject _this, jlong address, jdouble value)
+{
+       double* p;
+
+       p = (double*) (intptr_t) address;
+
+       *p = value;
+}
+
+
 /*
  * Class:     sun/misc/Unsafe
  * Method:    objectFieldOffset
@@ -792,7 +826,9 @@ JNIEXPORT jclass JNICALL Java_sun_misc_Unsafe_defineClass__Ljava_lang_String_2_3
 
        /* check the indexes passed */
 
-       if ((off < 0) || (len < 0) || ((off + len) > LLNI_array_size(b))) {
+       ByteArray ba(b);
+
+       if ((off < 0) || (len < 0) || ((off + len) > ba.get_length())) {
                exceptions_throw_arrayindexoutofboundsexception();
                return NULL;
        }
@@ -808,7 +844,8 @@ JNIEXPORT jclass JNICALL Java_sun_misc_Unsafe_defineClass__Ljava_lang_String_2_3
 
        /* define the class */
 
-       c = class_define(utfname, cl, len, (uint8_t *) &(LLNI_array_direct((java_handle_bytearray_t*) b, off)),
+       uint8_t* ptr = ((uint8_t*) ba.get_raw_data_ptr()) + off;
+       c = class_define(utfname, cl, len, ptr,
                                         (java_handle_t *) protectionDomain);
 
        if (c == NULL)
@@ -866,14 +903,14 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_throwException(JNIEnv *env, jobject
  */
 JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_compareAndSwapObject(JNIEnv *env, jobject _this, jobject o, jlong offset, jobject expected, jobject x)
 {
-       volatile void **p;
+       void **p;
        void           *result;
 
        /* XXX Use LLNI */
 
-       p = (volatile void **) (((uint8_t *) o) + offset);
+       p = (void **) (((uint8_t *) o) + offset);
 
-       result = Atomic::compare_and_swap(p, expected, x);
+       result = Atomic::compare_and_swap(p, (void *) expected, (void *) x);
 
        if (result == expected)
                return true;
@@ -896,7 +933,7 @@ JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_compareAndSwapInt(JNIEnv *env, j
 
        p = (uint32_t *) (((uint8_t *) o) + offset);
 
-       result = Atomic::compare_and_swap(p, expected, x);
+       result = Atomic::compare_and_swap(p, (uint32_t) expected, (uint32_t) x);
 
        if (result == (uint32_t) expected)
                return true;
@@ -919,7 +956,7 @@ JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_compareAndSwapLong(JNIEnv *env,
 
        p = (uint64_t *) (((uint8_t *) o) + offset);
 
-       result = Atomic::compare_and_swap(p, expected, x);
+       result = Atomic::compare_and_swap(p, (uint64_t) expected, (uint64_t) x);
 
        if (result == (uint64_t) expected)
                return true;
@@ -950,6 +987,17 @@ JNIEXPORT void JNICALL Java_sun_misc_Unsafe_putObjectVolatile(JNIEnv *env, jobje
 }
 
 
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    getBooleanVolatile
+ * Signature: (Ljava/lang/Object;J)Z
+ */
+JNIEXPORT jboolean JNICALL Java_sun_misc_Unsafe_getBooleanVolatile(JNIEnv* env, jobject _this, jobject o, jlong offset)
+{
+       return FieldAccess::get_volatile<int32_t>(o, offset);
+}
+
+
 /*
  * Class:     sun/misc/Unsafe
  * Method:    getByteVolatile
@@ -961,6 +1009,28 @@ JNIEXPORT jbyte JNICALL Java_sun_misc_Unsafe_getByteVolatile(JNIEnv* env, jobjec
 }
 
 
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    getShortVolatile
+ * Signature: (Ljava/lang/Object;J)S
+ */
+JNIEXPORT jshort JNICALL Java_sun_misc_Unsafe_getShortVolatile(JNIEnv* env, jobject _this, jobject o, jlong offset)
+{
+       return FieldAccess::get_volatile<int32_t>(o, offset);
+}
+
+
+/*
+ * Class:     sun/misc/Unsafe
+ * Method:    getCharVolatile
+ * Signature: (Ljava/lang/Object;J)C
+ */
+JNIEXPORT jchar JNICALL Java_sun_misc_Unsafe_getCharVolatile(JNIEnv* env, jobject _this, jobject o, jlong offset)
+{
+       return FieldAccess::get_volatile<int32_t>(o, offset);
+}
+
+
 /*
  * Class:     sun/misc/Unsafe
  * Method:    getIntVolatile
@@ -1126,6 +1196,8 @@ static JNINativeMethod methods[] = {
        { (char*) "putLong",                (char*) "(JJ)V",                                                      (void*) (uintptr_t) &Java_sun_misc_Unsafe_putLong__JJ                      },
        { (char*) "getFloat",               (char*) "(J)F",                                                       (void*) (uintptr_t) &Java_sun_misc_Unsafe_getFloat__J                      },
        { (char*) "putFloat",               (char*) "(JF)V",                                                      (void*) (uintptr_t) &Java_sun_misc_Unsafe_putFloat__JF                     },
+       { (char*) "getDouble",              (char*) "(J)D",                                                       (void*) (uintptr_t) &Java_sun_misc_Unsafe_getDouble__J                     },
+       { (char*) "putDouble",              (char*) "(JD)V",                                                      (void*) (uintptr_t) &Java_sun_misc_Unsafe_putDouble__JD                    },
        { (char*) "objectFieldOffset",      (char*) "(Ljava/lang/reflect/Field;)J",                               (void*) (uintptr_t) &Java_sun_misc_Unsafe_objectFieldOffset                },
        { (char*) "allocateMemory",         (char*) "(J)J",                                                       (void*) (uintptr_t) &Java_sun_misc_Unsafe_allocateMemory                   },
 #if 0
@@ -1152,7 +1224,10 @@ static JNINativeMethod methods[] = {
        { (char*) "compareAndSwapLong",     (char*) "(Ljava/lang/Object;JJJ)Z",                                   (void*) (uintptr_t) &Java_sun_misc_Unsafe_compareAndSwapLong               },
        { (char*) "getObjectVolatile",      (char*) "(Ljava/lang/Object;J)Ljava/lang/Object;",                    (void*) (uintptr_t) &Java_sun_misc_Unsafe_getObjectVolatile                },
        { (char*) "putObjectVolatile",      (char*) "(Ljava/lang/Object;JLjava/lang/Object;)V",                   (void*) (uintptr_t) &Java_sun_misc_Unsafe_putObjectVolatile                },
+       { (char*) "getBooleanVolatile",     (char*) "(Ljava/lang/Object;J)Z",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getBooleanVolatile               },
        { (char*) "getByteVolatile",        (char*) "(Ljava/lang/Object;J)B",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getByteVolatile                  },
+       { (char*) "getShortVolatile",       (char*) "(Ljava/lang/Object;J)S",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getShortVolatile                 },
+       { (char*) "getCharVolatile",        (char*) "(Ljava/lang/Object;J)C",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getCharVolatile                  },
        { (char*) "getIntVolatile",         (char*) "(Ljava/lang/Object;J)I",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getIntVolatile                   },
        { (char*) "putIntVolatile",         (char*) "(Ljava/lang/Object;JI)V",                                    (void*) (uintptr_t) &Java_sun_misc_Unsafe_putIntVolatile                   },
        { (char*) "getLongVolatile",        (char*) "(Ljava/lang/Object;J)J",                                     (void*) (uintptr_t) &Java_sun_misc_Unsafe_getLongVolatile                  },