commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1085321 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Unsafe.java native/shared/unsafe.c
Date Fri, 25 Mar 2011 11:16:39 GMT
Author: mturk
Date: Fri Mar 25 11:16:39 2011
New Revision: 1085321

URL: http://svn.apache.org/viewvc?rev=1085321&view=rev
Log:
Add more Unsafe methods

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java
    commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java?rev=1085321&r1=1085320&r2=1085321&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java Fri
Mar 25 11:16:39 2011
@@ -15,6 +15,7 @@
  */
 
 package org.apache.commons.runtime;
+import java.lang.reflect.Field;
 
 /**
  * Wrapper over sun.misc.Unsafe.
@@ -30,8 +31,11 @@ public final class Unsafe
         // No instance.
     }
 
-    private static final Object unsafe;
+    private static final  Object unsafe;
     private static native Object get0();
+    private static native int    get1(Field f);
+    private static native int    get2(Field f);
+    private static native int    get3(Field f);
     static {
         unsafe = get0();
     }
@@ -45,7 +49,40 @@ public final class Unsafe
     }
 
     public static Object getInstance()
+        throws UnsupportedOperationException
     {
+        if (unsafe == null)
+            throw new UnsupportedOperationException();
         return unsafe;
     }
+
+    public static int objectFieldOffset(Field field)
+        throws UnsupportedOperationException, IllegalArgumentException
+    {
+        if (unsafe == null)
+            throw new UnsupportedOperationException();
+        if (field == null)
+            throw new IllegalArgumentException();
+        return get1(field);
+    }
+
+    public static int staticFieldBase(Field field)
+        throws UnsupportedOperationException, IllegalArgumentException
+    {
+        if (unsafe == null)
+            throw new UnsupportedOperationException();
+        if (field == null)
+            throw new IllegalArgumentException();
+        return get2(field);
+    }
+
+    public static int staticFieldOffset(Field field)
+        throws UnsupportedOperationException, IllegalArgumentException
+    {
+        if (unsafe == null)
+            throw new UnsupportedOperationException();
+        if (field == null)
+            throw new IllegalArgumentException();
+        return get3(field);
+    }
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c?rev=1085321&r1=1085320&r2=1085321&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c Fri Mar 25 11:16:39 2011
@@ -50,6 +50,12 @@ J_DECLARE_M_ID(0002) = {
 
 J_DECLARE_M_ID(0003) = {
     0,
+    "staticFieldBase",
+    "(Ljava/lang/reflect/Field;)J"
+};
+
+J_DECLARE_M_ID(0004) = {
+    0,
     "staticFieldOffset",
     "(Ljava/lang/reflect/Field;)J"
 };
@@ -67,6 +73,7 @@ ACR_CLASS_CTOR(Unsafe)
     J_LOAD_METHOD(0001);
     J_LOAD_METHOD(0002);
     J_LOAD_METHOD(0003);
+    J_LOAD_METHOD(0004);
 
     return 0;
 }
@@ -78,8 +85,8 @@ ACR_CLASS_DTOR(Unsafe)
 
 static jobject _unsafe_instance = 0;
 
-jobject
-AcrUnsafeGetInstance(JNI_STDENV)
+ACR_INLINE(jobject)
+_get_unsafe(JNI_STDENV)
 {
     if (_unsafe_instance == 0 && _clazzn.i != 0) {
         _unsafe_instance = GET_SFIELD_O(0000, _clazzn.i);
@@ -87,41 +94,82 @@ AcrUnsafeGetInstance(JNI_STDENV)
     return _unsafe_instance;
 }
 
-jlong
+jint
 AcrUnsafeObjectFieldOffset(JNI_STDARGS)
 {
-    if (AcrUnsafeGetInstance(_E) == 0) {
+    jlong fo;
+    if (_get_unsafe(_E) == 0) {
+        return 0;
+    }
+    fo = CALL_METHOD1(Long, 0002, _unsafe_instance, _O);
+    if (fo > 0 && fo < INT_MAX)
+        return (jint)fo;
+    else
+        return 0;
+}
+
+jint
+AcrUnsafeStaticFieldBase(JNI_STDARGS)
+{
+    jlong fo;
+    if (_get_unsafe(_E) == 0) {
         return 0;
     }
-    return CALL_METHOD1(Long, 0002, _unsafe_instance, _O);
+    fo = CALL_METHOD1(Long, 0003, _unsafe_instance, _O);
+    if (fo > 0 && fo < INT_MAX)
+        return (jint)fo;
+    else
+        return 0;
 }
 
-jlong
+jint
 AcrUnsafeStaticFieldOffset(JNI_STDARGS)
 {
-    if (AcrUnsafeGetInstance(_E) == 0) {
+    jlong fo;
+    if (_get_unsafe(_E) == 0) {
         return 0;
     }
-    return CALL_METHOD1(Long, 0003, _unsafe_instance, _O);
+    fo = CALL_METHOD1(Long, 0004, _unsafe_instance, _O);
+    if (fo > 0 && fo < INT_MAX)
+        return (jint)fo;
+    else
+        return 0;
 }
 
 ACR_JNI_EXPORT(jobject, Unsafe, get0)(JNI_STDARGS)
 {
-    return AcrUnsafeGetInstance(_E);
+    return _get_unsafe(_E);
+}
+
+ACR_JNI_EXPORT(jint, Unsafe, get1)(JNI_STDARGS, jobject field)
+{
+    return AcrUnsafeObjectFieldOffset(_E, field);
+}
+
+ACR_JNI_EXPORT(jint, Unsafe, get2)(JNI_STDARGS, jobject field)
+{
+    return AcrUnsafeStaticFieldBase(_E, field);
+}
+
+ACR_JNI_EXPORT(jint, Unsafe, get3)(JNI_STDARGS, jobject field)
+{
+    return AcrUnsafeStaticFieldOffset(_E, field);
 }
 
 #if defined(ENABLE_TEST_PRIVATE)
-ACR_JNI_EXPORT(jint, TestUnsafe, get0)(JNI_STDARGS, jobject o, jlong off)
+ACR_JNI_EXPORT(jint, TestUnsafe, get0)(JNI_STDARGS, jobject o, jint off)
 {
-    if (o != 0) {
-        char *ob = (*(char **)o + (ptrdiff_t)off);
-        return *((jint *)ob);
+    if (o != 0 && off > 0) {
+        char *oa = *(char **)o;
+        if (oa != 0) {
+            char *fa = (oa + (ptrdiff_t)off);
+            return *((jint *)fa);
+        }
     }
-    else
-        return 0;
+    return 0;
 }
 
-ACR_JNI_EXPORT(jlong, TestUnsafe, get1)(JNI_STDARGS, jobject o)
+ACR_JNI_EXPORT(jint, TestUnsafe, get1)(JNI_STDARGS, jobject o)
 {
     return AcrUnsafeObjectFieldOffset(_E, o);
 }



Mime
View raw message