Author: mturk
Date: Thu Apr 23 15:33:30 2009
New Revision: 767945

URL: http://svn.apache.org/viewvc?rev=767945&view=rev
Log:
Add peek and poke to the Pointer

Modified:
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
    
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
    commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
    
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java?rev=767945&r1=767944&r2=767945&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java
 Thu Apr 23 15:33:30 2009
@@ -95,6 +95,28 @@
     }
 
     /**
+     * Get a {...@code byte} value this {...@code pointer} contains at the
+     * {...@code index}.
+     *
+     * @return a {...@code byte} at {...@code index}.
+     * @throws IndexOutOfBoundsException if {...@code index} would cause access
+     *          outside the pointer address space.
+     */
+    public abstract int peek(int index)
+        throws IndexOutOfBoundsException;
+
+    /**
+     * Set a {...@code byte} value to this {...@code pointer} at the
+     * {...@code index} location.
+     *
+     * @param value Value to set at {...@code index}.
+     * @throws IndexOutOfBoundsException if {...@code index} would cause access
+     *          outside the pointer address space.
+     */
+    public abstract void poke(int index, int value)
+        throws IndexOutOfBoundsException;
+
+    /**
      * Returns a string representation of the Pointer.
      * The returned string is hexadecimal representation of the underlying
      * pointer.

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java?rev=767945&r1=767944&r2=767945&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer32.java
 Thu Apr 23 15:33:30 2009
@@ -59,6 +59,26 @@
         return new Integer(POINTER);
     }
 
+    private static native int  peek0(int addr, int index);
+
+    public int peek(int index)
+        throws IndexOutOfBoundsException
+    {
+        if (index < 0 || index >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        return peek0(POINTER, index);
+    }
+
+    private static native void  poke0(int addr, int index, int b);
+
+    public void poke(int index, int value)
+        throws IndexOutOfBoundsException
+    {
+        if (index < 0 || index >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        poke0(POINTER, index, value);
+    }
+
     public String toString()
     {
         String h = Integer.toHexString(POINTER);

Modified: 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java?rev=767945&r1=767944&r2=767945&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer64.java
 Thu Apr 23 15:33:30 2009
@@ -59,6 +59,26 @@
         return new Long(POINTER);
     }
 
+    private static native int  peek0(long addr, int index);
+
+    public int peek(int index)
+        throws IndexOutOfBoundsException
+    {
+        if (index < 0 || index >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        return peek0(POINTER, index);
+    }
+
+    private static native void  poke0(long addr, int index, int b);
+
+    public void poke(int index, int value)
+        throws IndexOutOfBoundsException
+    {
+        if (index < 0 || index >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        poke0(POINTER, index, value);
+    }
+
     public String toString()
     {
         String h = Long.toHexString(POINTER);

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c?rev=767945&r1=767944&r2=767945&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Thu Apr 23 
15:33:30 2009
@@ -29,6 +29,8 @@
  * Pointer class utilities
  */
 #if CC_SIZEOF_VOIDP == 8
+#define ACR_PTR_EXPORT_DECLARE(RT, FN)  \
+    JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_Pointer64_##FN
 
 J_DECLARE_CLAZZ = {
     NULL,
@@ -43,6 +45,8 @@
 };
 
 #else
+#define ACR_PTR_EXPORT_DECLARE(RT, FN)  \
+    JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_Pointer32_##FN
 
 J_DECLARE_CLAZZ = {
     NULL,
@@ -145,6 +149,22 @@
     }
 }
 
+ACR_PTR_EXPORT_DECLARE(jint, peek0)(ACR_JNISTDARGS, jniptr a, jint i)
+{
+    char *b = (char *)((acr_ptr_t)a);
+    UNREFERENCED_STDARGS;
+
+    return b[i];
+}
+
+ACR_PTR_EXPORT_DECLARE(void, poke0)(ACR_JNISTDARGS, jniptr a, jint i, jint v)
+{
+    char *b = (char *)((acr_ptr_t)a);
+    UNREFERENCED_STDARGS;
+
+    b[i] = v;
+}
+
 ACR_DECLARE(jobject) ACR_PointerCreate(JNIEnv *_E, void *p, size_t len,
                                        acr_pointer_cleanup_fn_t *cb)
 {

Modified: 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
URL: 
http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java?rev=767945&r1=767944&r2=767945&view=diff
==============================================================================
--- 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
 (original)
+++ 
commons/sandbox/runtime/trunk/src/test/org/apache/commons/runtime/TestMemory.java
 Thu Apr 23 15:33:30 2009
@@ -152,5 +152,27 @@
         p.free();
     }
 
+    public void testPoke()
+        throws Throwable
+    {
+        Pointer p = Memory.calloc(1000);
+        assertNotNull("Pointer", p);
+        Memory.set(p, 0, 1000, 0xFF);
+        assertEquals("Value", (byte)0xFF, p.peek(0));
+
+        p.free();
+    }
+
+    public void testPeek()
+        throws Throwable
+    {
+        Pointer p = Memory.calloc(1000);
+        assertNotNull("Pointer", p);
+        p.poke(999, 23);
+        assertEquals("Value", 23, p.peek(999));
+
+        p.free();
+    }
+
 }
 


Reply via email to