Author: mturk
Date: Fri Apr 24 10:47:44 2009
New Revision: 768255

URL: http://svn.apache.org/viewvc?rev=768255&view=rev
Log:
Add optimized copy and move Pointer methods

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=768255&r1=768254&r2=768255&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
 Fri Apr 24 10:47:44 2009
@@ -148,6 +148,59 @@
         throws IndexOutOfBoundsException, NullPointerException;
 
     /**
+     * Copy the memory area from {...@code this} pointer to {...@code dst}.
+     * <p>
+     * Method uses the {...@code memcpy} function to do a copying, meaning
+     * that {...@code source} and {...@code destination} memory areas should
+     * not overlap.
+     * </p>
+     *
+     * @param srcPos starting position in the source memory.
+     * @param dst destination {...@code Pointer}.
+     * @param dstPos starting position in the destination memory.
+     * @param length the number of bytes to be copied.
+     *
+     * @throws IllegalArgumentException if the {...@code srcPos} or
+     *          {...@code dstPos} is {...@code negative} or {...@code length}
+     *          is {...@code zero}.
+     * @throws IndexOutOfBoundsException if the operation would cause
+     *          access of data outside allocated memory bounds.
+     * @throws NullPointerException if {...@code this} or {...@code dst} is
+     *          {...@code null}.
+     */
+    public abstract void copy(long srcPos, Pointer dst,
+                              long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException;
+
+    /**
+     * Copy the memory area from {...@code this} pointer to {...@code dst}.
+     * <p>
+     * Method uses the {...@code memmove} function to do a copying, meaning
+     * that {...@code source} and {...@code destination} memory areas may
+     * overlap.
+     * </p>
+     *
+     * @param srcPos starting position in the source memory.
+     * @param dst destination {...@code Pointer}.
+     * @param dstPos starting position in the destination memory.
+     * @param length the number of bytes to be copied.
+     *
+     * @throws IllegalArgumentException if the {...@code srcPos} or
+     *          {...@code dstPos} is {...@code negative} or {...@code length}
+     *          is {...@code zero}.
+     * @throws IndexOutOfBoundsException if the operation would cause
+     *          access of data outside allocated memory bounds.
+     * @throws NullPointerException if {...@code this} or {...@code dst} is
+     *          {...@code null}.
+     */
+    public abstract void move(long srcPos, Pointer dst,
+                              long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException;
+
+
+    /**
      * 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=768255&r1=768254&r2=768255&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
 Fri Apr 24 10:47:44 2009
@@ -88,6 +88,43 @@
         poke0(POINTER + index, value);
     }
 
+    private static native void copy0(int src, int dst, long length);
+    private static native void move0(int src, int dst, long length);
+
+    public void copy(long srcPos, Pointer dst,
+                     long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException
+    {
+        Pointer32 d32 = (Pointer32)dst;
+        if (POINTER == 0 || d32.POINTER == 0)
+            throw new NullPointerException();
+        else if (srcPos < 0L || dstPos < 0L)
+            throw new IllegalArgumentException();
+        else if ((int)(srcPos + length) > PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((int)(dstPos + length) > d32.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        copy0(POINTER + (int)srcPos, d32.POINTER + (int)dstPos, length);
+    }
+
+    public void move(long srcPos, Pointer dst,
+                     long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException
+    {
+        Pointer32 d32 = (Pointer32)dst;
+        if (POINTER == 0 || d32.POINTER == 0)
+            throw new NullPointerException();
+        else if (srcPos < 0L || dstPos < 0L)
+            throw new IllegalArgumentException();
+        else if ((int)(srcPos + length) > PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if ((int)(dstPos + length) > d32.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        move0(POINTER + (int)srcPos, d32.POINTER + (int)dstPos, length);
+    }
+
     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=768255&r1=768254&r2=768255&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
 Fri Apr 24 10:47:44 2009
@@ -69,9 +69,9 @@
     public int peek(int index)
         throws IndexOutOfBoundsException, NullPointerException
     {
-        if (POINTER == 0)
+        if (POINTER == 0L)
             throw new NullPointerException();
-        else if (index < 0 || index >= PLENGTH)
+        else if (index < 0L || index >= PLENGTH)
             throw new IndexOutOfBoundsException();
         return peek0(POINTER + index);
     }
@@ -81,13 +81,50 @@
     public void poke(int index, int value)
         throws IndexOutOfBoundsException, NullPointerException
     {
-        if (POINTER == 0)
+        if (POINTER == 0L)
             throw new NullPointerException();
-        else if (index < 0 || index >= PLENGTH)
+        else if (index < 0L || index >= PLENGTH)
             throw new IndexOutOfBoundsException();
         poke0(POINTER + index, value);
     }
 
+    private static native void copy0(long src, long dst, long length);
+    private static native void move0(long src, long dst, long length);
+
+    public void copy(long srcPos, Pointer dst,
+                     long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException
+    {
+        Pointer64 d64 = (Pointer64)dst;
+        if (POINTER == 0L || d64.POINTER == 0L)
+            throw new NullPointerException();
+        else if (srcPos < 0L || dstPos < 0L)
+            throw new IllegalArgumentException();
+        else if (srcPos + length >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if (dstPos + length >- d64.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        copy0(POINTER + srcPos, d64.POINTER + dstPos, length);
+    }
+
+    public void move(long srcPos, Pointer dst,
+                     long dstPos, long length)
+        throws IndexOutOfBoundsException, IllegalArgumentException,
+               NullPointerException
+    {
+        Pointer64 d64 = (Pointer64)dst;
+        if (POINTER == 0L || d64.POINTER == 0L)
+            throw new NullPointerException();
+        else if (srcPos < 0L || dstPos < 0L)
+            throw new IllegalArgumentException();
+        else if (srcPos + length >= PLENGTH)
+            throw new IndexOutOfBoundsException();
+        else if (dstPos + length >- d64.PLENGTH)
+            throw new IndexOutOfBoundsException();
+        move0(POINTER + srcPos, d64.POINTER + dstPos, length);
+    }
+
     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=768255&r1=768254&r2=768255&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Fri Apr 24 
10:47:44 2009
@@ -186,6 +186,20 @@
     *((char *)((acr_ptr_t)a)) = v;
 }
 
+ACR_PTR_EXPORT_DECLARE(void, copy0)(ACR_JNISTDARGS, jniptr s,
+                                    jniptr d, jniptr l)
+{
+    UNREFERENCED_STDARGS;
+    memcpy((void *)((acr_ptr_t)d), (const void *)((acr_ptr_t)s), (size_t)l);
+}
+
+ACR_PTR_EXPORT_DECLARE(void, move0)(ACR_JNISTDARGS, jniptr s,
+                                    jniptr d, jniptr l)
+{
+    UNREFERENCED_STDARGS;
+    memmove((void *)((acr_ptr_t)d), (const void *)((acr_ptr_t)s), (size_t)l);
+}
+
 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=768255&r1=768254&r2=768255&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
 Fri Apr 24 10:47:44 2009
@@ -207,5 +207,35 @@
         assertTrue("Not null", Pointer.NULL.IsNull());
 
     }
+
+    public void testPointerCopy()
+        throws Throwable
+    {
+        Pointer src = Memory.calloc(1000);
+        assertNotNull("Source Pointer", src);
+        Memory.set(src, 0, 1000, 0xFF);
+        Pointer dst = Memory.calloc(1000);
+        assertNotNull("Dest Pointer", dst);
+        src.copy(0, dst, 0, 1000);
+        assertEquals("Value", (byte)0xFF, dst.peek(0));
+
+        src.free();
+        dst.free();
+    }
+
+    public void testPointerMove()
+        throws Throwable
+    {
+        Pointer p = Memory.calloc(1000);
+        assertNotNull("Pointer", p);
+        Memory.set(p, 0, 1000, 0xFF);
+        p.poke(0, 23);
+
+        p.move(0, p, 1, 999);
+        assertEquals("Value", 23, p.peek(1));
+
+        p.free();
+    }
+
 }
 


Reply via email to