You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by mt...@apache.org on 2011/05/04 07:17:53 UTC

svn commit: r1099325 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/Unsafe.java java/org/apache/commons/runtime/net/LocalSocketAddress.java native/shared/unsafe.c

Author: mturk
Date: Wed May  4 05:17:53 2011
New Revision: 1099325

URL: http://svn.apache.org/viewvc?rev=1099325&view=rev
Log:
Implement Unsafe memory peek/poke api

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Unsafe.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.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=1099325&r1=1099324&r2=1099325&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 Wed May  4 05:17:53 2011
@@ -31,20 +31,32 @@ public final class Unsafe
         // No instance.
     }
 
-    private static final  Object unsafe;
-    private static native Object init0();
-    private static native int    getOffset0(Field f);
-    private static native int    getOffset1(Field f);
-    private static native int    getBase0(Field f);
+    private static final  Object        unsafe;
+    private static native Object        init0();
+    private static native int           getOffset0(Field f);
+    private static native int           getOffset1(Field f);
+    private static native int           getBase0(Field f);
     
-    private static native int    peek0(long ptr);
-    private static native long   peek1(long ptr);
-    private static native short  peek2(long ptr);
-    private static native double peek3(long ptr);
-    private static native String peek4(long ptr);
-    private static native String peek5(long ptr);
-    private static native char[] peek6(long ptr);
-    private static native long   peek7(long ptr);
+    private static native int           peek0(long ptr);
+    private static native long          peek1(long ptr);
+    private static native short         peek2(long ptr);
+    private static native char          peek3(long ptr);
+    private static native double        peek4(long ptr);
+    private static native String        peek5(long ptr);
+    private static native String        peek6(long ptr);
+    private static native char[]        peek7(long ptr);
+    private static native long          peek8(long ptr);
+    private static native byte          peek9(long ptr);
+    private static native boolean       peek10(long ptr);
+
+    private static native void          poke0(long ptr, byte v);
+    private static native void          poke1(long ptr, short v);
+    private static native void          poke2(long ptr, char v);
+    private static native void          poke3(long ptr, int v);
+    private static native void          poke4(long ptr, long v);
+    private static native void          poke5(long ptr, double v);
+    private static native void          poke6(long ptr, long v);
+    private static native void          poke7(long ptr, boolean v);
 
     private static native long   addr0(byte[] b);
 
@@ -118,5 +130,282 @@ public final class Unsafe
             return getchr0(str);
     }
 
+    /**
+     * Get a {@code int} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code int} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static int getInt(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek0(address);
+    }
+
+    /**
+     * Get a {@code long} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code long} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static long getLong(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek1(address);
+    }
+
+    /**
+     * Get a {@code short} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code short} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static short getShort(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek2(address);
+    }
+
+    /**
+     * Get a {@code char} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code char} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static char getChar(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek3(address);
+    }
+
+    /**
+     * Get a {@code double} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code double} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static double getDouble(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek4(address);
+    }
+
+    /**
+     * Get a {@code C string} pointer value contained at the
+     * {@code address}.
+     *
+     * @return a {@code char*} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static String getAnsiString(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek5(address);
+    }
+
+    /**
+     * Get a {@code wide string} pointer value contained at the
+     * {@code address}.
+     *
+     * @return a {@code wchar_t*} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static String getWideString(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek6(address);
+    }
+
+    /**
+     * Get a {@code msz string} pointer value contained at the
+     * {@code address}.
+     *
+     * @return a {@code wchar_t*} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static char[] getMszString(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek7(address);
+    }
+
+    /**
+     * Get a {@code pointer} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code *(void*)} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static long getPointer(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek8(address);
+    }
+
+    /**
+     * Get a {@code byte} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code byte} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static byte getByte(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek9(address);
+    }
+
+    /**
+     * Get a {@code boolean} value contained at the
+     * {@code address}.
+     *
+     * @return a {@code boolean} at {@code address}.
+     * @throws NullPointerException if address is {@code zero}.
+     */
+    public static boolean getBoolean(long address)
+        throws IndexOutOfBoundsException, NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        return peek10(address);
+    }
+
+    /**
+     * Set a {@code byte} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setByte(long address, byte value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke0(address, value);
+    }
+
+    /**
+     * Set a {@code short} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setShort(long address, short value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke1(address, value);
+    }
+
+    /**
+     * Set a {@code char} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setChar(long address, char value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke2(address, value);
+    }
+
+    /**
+     * Set a {@code int} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setInt(long address, int value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke3(address, value);
+    }
+
+    /**
+     * Set a {@code long} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setLong(long address, long value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke4(address, value);
+    }
+
+    /**
+     * Set a {@code double} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setDouble(long address, double value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke5(address, value);
+    }
+
+    /**
+     * Set a {@code pointer} value at the {@code address} location.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setPointer(long address, long value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke6(address, value);
+    }
+
+    /**
+     * Set a {@code boolean} value at the {@code address} location.
+     * The native storage location is presumed to be 32-bit integer.
+     *
+     * @param value Value to set at {@code address}.
+     * @throws NullPointerException if addres is {@code zero}.
+     */
+    public static void setBoolean(long address, boolean value)
+        throws NullPointerException
+    {
+        if (address == 0L)
+            throw new NullPointerException();
+        poke7(address, value);
+    }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java?rev=1099325&r1=1099324&r2=1099325&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/net/LocalSocketAddress.java Wed May  4 05:17:53 2011
@@ -27,6 +27,7 @@ import org.apache.commons.runtime.Status
 public final class LocalSocketAddress extends SocketAddress
 {
 
+    private static native long   addr0(String name);
     private static native String prefix0();
     private static final  String prefix;
     static {
@@ -54,6 +55,7 @@ public final class LocalSocketAddress ex
             pn.append(c);
         }
         path = pn.toString();
+        addr = addr0(name);
     }
 
     public LocalSocketAddress(File path)
@@ -63,6 +65,7 @@ public final class LocalSocketAddress ex
         // path is presumed to be correctly
         // formatted.
         this.path = path.getPath();
+        addr = addr0(this.path);
     }
 
     /**

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=1099325&r1=1099324&r2=1099325&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/unsafe.c Wed May  4 05:17:53 2011
@@ -215,39 +215,96 @@ ACR_JNI_EXPORT(jshort, Unsafe, peek2)(JN
     return *((jshort *)buf);
 }
 
-ACR_JNI_EXPORT(jdouble, Unsafe, peek3)(JNI_STDARGS, jlong ptr)
+ACR_JNI_EXPORT(jchar, Unsafe, peek3)(JNI_STDARGS, jlong ptr)
+{
+    char *buf = J2P(ptr, char *);
+    return *((jshort *)buf);
+}
+
+ACR_JNI_EXPORT(jdouble, Unsafe, peek4)(JNI_STDARGS, jlong ptr)
 {
     char *buf = J2P(ptr, char *);
     return *((double *)buf);
 }
 
-ACR_JNI_EXPORT(jstring, Unsafe, peek4)(JNI_STDARGS, jlong ptr)
+ACR_JNI_EXPORT(jstring, Unsafe, peek5)(JNI_STDARGS, jlong ptr)
 {
     char *buf = J2P(ptr, char *);
     return AcrNewJavaStringA(env, buf);
 }
 
-ACR_JNI_EXPORT(jstring, Unsafe, peek5)(JNI_STDARGS, jlong ptr)
+ACR_JNI_EXPORT(jstring, Unsafe, peek6)(JNI_STDARGS, jlong ptr)
 {
     wchar_t *buf = J2P(ptr, wchar_t *);
     return AcrNewJavaStringW(env, buf);
 }
 
-ACR_JNI_EXPORT(jcharArray, Unsafe, peek6)(JNI_STDARGS, jlong ptr)
+ACR_JNI_EXPORT(jcharArray, Unsafe, peek7)(JNI_STDARGS, jlong ptr)
 {
     wchar_t *buf = J2P(ptr, wchar_t *);
     return AcrNewJavaMszArrayW(env, buf);
 }
 
-ACR_JNI_EXPORT(jlong, Unsafe, peek7)(JNI_STDARGS, jlong ptr)
+ACR_JNI_EXPORT(jlong, Unsafe, peek8)(JNI_STDARGS, jlong ptr)
 {
-    char **buf = J2P(ptr, char **);
-    if (buf == 0)
-        return 0;
-    else
-        return P2J(*buf);
+    char *buf = J2P(ptr, char *);
+    return P2J((uintptr_t)buf);
+}
+
+ACR_JNI_EXPORT(jbyte,  Unsafe, peek9)(JNI_STDARGS, jlong ptr)
+{
+    char *buf = J2P(ptr, char *);
+    return *((jbyte *)buf);
+}
+
+ACR_JNI_EXPORT(jboolean,  Unsafe, peek10)(JNI_STDARGS, jlong ptr)
+{
+    char *buf = J2P(ptr, char *);
+    return *((jint *)buf) ? JNI_TRUE : JNI_FALSE;
 }
 
+
+ACR_JNI_EXPORT(void, Unsafe, poke0)(JNI_STDARGS, jlong a, jbyte v)
+{
+    *(J2P(a, jbyte *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke1)(JNI_STDARGS, jlong a, jshort v)
+{
+    *(J2P(a, jshort *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke2)(JNI_STDARGS, jlong a, jchar v)
+{
+    *(J2P(a, jchar *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke3)(JNI_STDARGS, jlong a, jint v)
+{
+    *(J2P(a, jint *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke4)(JNI_STDARGS, jlong a, jlong v)
+{
+    *(J2P(a, jlong *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke5)(JNI_STDARGS, jlong a, jdouble v)
+{
+    *(J2P(a, jdouble *)) = v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke6)(JNI_STDARGS, jlong a, jlong v)
+{
+    *(J2P(a, uintptr_t *)) = (uintptr_t)v;
+}
+
+ACR_JNI_EXPORT(void, Unsafe, poke7)(JNI_STDARGS, jlong a, jboolean v)
+{
+    *(J2P(a, jint *)) = (jint)v;
+}
+
+
 ACR_JNI_EXPORT(jlong, Unsafe, addr0)(JNI_STDARGS, jbyteArray ba)
 {
     jlong adr;