Author: mturk Date: Fri Apr 24 13:03:38 2009 New Revision: 768294 URL: http://svn.apache.org/viewvc?rev=768294&view=rev Log: Extend copy and move methods
Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java 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/DirectByteBuffer.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java?rev=768294&r1=768293&r2=768294&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/DirectByteBuffer.java Fri Apr 24 13:03:38 2009 @@ -191,8 +191,6 @@ * * @param ptr {...@link Pointer} taht holds the native memory area. * @param sizes Array of lengths for each ByteBuffer. - * @param off Start offset of the {...@code sizes} array. - * @param len The length of the {...@code sizes} array to use. * @return The ByteBuffer array with allocated memory * * @throws OutOfMemoryError if memory cannot be allocated from the system. Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=768294&r1=768293&r2=768294&view=diff ============================================================================== --- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java (original) +++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java Fri Apr 24 13:03:38 2009 @@ -125,7 +125,7 @@ /** * Change the size of memory block pointed by {...@code ptr}. * - * @param prt Pointer which size to change.. + * @param ptr Pointer which size to change.. * @param size The new size of the memory block. * * @throws OutOfMemoryError if memory cannot be allocated. 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=768294&r1=768293&r2=768294&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 13:03:38 2009 @@ -174,16 +174,59 @@ NullPointerException; /** - * Copy the memory area from {...@code this} pointer to {...@code dst}. + * Copy the memory area from {...@code this} pointer to array + * pointed by {...@code dst}. + * + * @param srcPos starting position in the source memory. + * @param dst destination {...@code array}. + * @param dstPos starting position in the destination array. + * @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, byte[] dst, + int dstPos, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException; + + /** + * Copy the memory area to {...@code this} pointer from array + * pointed by {...@code dst}. + * + * @param src source {...@code array}. + * @param dstPos starting position in the source memory. + * @param srcPos starting position in the destination array. + * @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(byte[] src, int srcPos, long dstPos, + int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException; + + /** + * Copy the memory area from pointer {...@code src} to {...@code this} pointer. * <p> * Method uses the {...@code memmove} function to do a copying, meaning - * that {...@code source} and {...@code destination} memory areas may - * overlap. + * that {...@code source} and {...@code destination} memory areas can overlap. * </p> * + * @param src source {...@code Pointer}. * @param srcPos starting position in the source memory. - * @param dst destination {...@code Pointer}. - * @param dstPos starting position in the destination memory. + * @param dstPos starting position in our memory area. * @param length the number of bytes to be copied. * * @throws IllegalArgumentException if the {...@code srcPos} or @@ -194,7 +237,7 @@ * @throws NullPointerException if {...@code this} or {...@code dst} is * {...@code null}. */ - public abstract void move(long srcPos, Pointer dst, + public abstract void move(Pointer src, long srcPos, long dstPos, long length) throws IndexOutOfBoundsException, IllegalArgumentException, NullPointerException; 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=768294&r1=768293&r2=768294&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 13:03:38 2009 @@ -89,7 +89,9 @@ } private static native void copy0(int src, int dst, long length); + private static native void copy1(int addr, byte[] d, int dstPos, int len); private static native void move0(int src, int dst, long length); + private static native void move1(int addr, byte[] d, int srcPos, int len); public void copy(long srcPos, Pointer dst, long dstPos, long length) @@ -99,7 +101,7 @@ Pointer32 d32 = (Pointer32)dst; if (POINTER == 0 || d32.POINTER == 0) throw new NullPointerException(); - else if (srcPos < 0L || dstPos < 0L) + else if (srcPos < 0L || dstPos < 0L || length == 0L) throw new IllegalArgumentException(); else if ((int)(srcPos + length) > PLENGTH) throw new IndexOutOfBoundsException(); @@ -108,21 +110,53 @@ copy0(POINTER + (int)srcPos, d32.POINTER + (int)dstPos, length); } - public void move(long srcPos, Pointer dst, - long dstPos, long length) + public void copy(long srcPos, byte[] dst, + int dstPos, int length) throws IndexOutOfBoundsException, IllegalArgumentException, NullPointerException { - Pointer32 d32 = (Pointer32)dst; - if (POINTER == 0 || d32.POINTER == 0) + if (POINTER == 0) throw new NullPointerException(); - else if (srcPos < 0L || dstPos < 0L) + else if (srcPos < 0 || dstPos < 0 || length == 0) throw new IllegalArgumentException(); else if ((int)(srcPos + length) > PLENGTH) throw new IndexOutOfBoundsException(); - else if ((int)(dstPos + length) > d32.PLENGTH) + else if ((dstPos + length) > dst.length) + throw new IndexOutOfBoundsException(); + copy1(POINTER + (int)srcPos, dst, dstPos, length); + } + + public void move(Pointer src, long srcPos, + long dstPos, long length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException + { + Pointer32 s32 = (Pointer32)src; + if (POINTER == 0 || s32.POINTER == 0) + throw new NullPointerException(); + else if (srcPos < 0L || dstPos < 0L || length == 0L) + throw new IllegalArgumentException(); + else if ((int)(dstPos + length) > PLENGTH) + throw new IndexOutOfBoundsException(); + else if ((int)(srcPos + length) > s32.PLENGTH) + throw new IndexOutOfBoundsException(); + move0(s32.POINTER + (int)dstPos, POINTER + (int)srcPos, length); + } + + public void move(byte[] src, int srcPos, + long dstPos, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException + { + if (POINTER == 0) + throw new NullPointerException(); + else if (srcPos < 0 || dstPos < 0 || length == 0) + throw new IllegalArgumentException(); + else if ((int)(dstPos + length) > PLENGTH) + throw new IndexOutOfBoundsException(); + else if ((srcPos + length) > src.length) throw new IndexOutOfBoundsException(); - move0(POINTER + (int)srcPos, d32.POINTER + (int)dstPos, length); + move1(POINTER + (int)dstPos, src, srcPos, length); } public String toString() 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=768294&r1=768293&r2=768294&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 13:03:38 2009 @@ -89,7 +89,9 @@ } private static native void copy0(long src, long dst, long length); + private static native void copy1(long addr, byte[] d, int dstPos, int len); private static native void move0(long src, long dst, long length); + private static native void move1(long addr, byte[] d, int srcPos, int len); public void copy(long srcPos, Pointer dst, long dstPos, long length) @@ -99,7 +101,7 @@ Pointer64 d64 = (Pointer64)dst; if (POINTER == 0L || d64.POINTER == 0L) throw new NullPointerException(); - else if (srcPos < 0L || dstPos < 0L) + else if (srcPos < 0L || dstPos < 0L || length == 0L) throw new IllegalArgumentException(); else if (srcPos + length >= PLENGTH) throw new IndexOutOfBoundsException(); @@ -108,21 +110,53 @@ copy0(POINTER + srcPos, d64.POINTER + dstPos, length); } - public void move(long srcPos, Pointer dst, + public void copy(long srcPos, byte[] dst, + int dstPos, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException + { + if (POINTER == 0L) + throw new NullPointerException(); + else if (srcPos < 0L || dstPos < 0 || length == 0) + throw new IllegalArgumentException(); + else if ((srcPos + length) > PLENGTH) + throw new IndexOutOfBoundsException(); + else if ((dstPos + length) > dst.length) + throw new IndexOutOfBoundsException(); + copy1(POINTER + srcPos, dst, dstPos, length); + } + + public void move(Pointer src, long srcPos, long dstPos, long length) throws IndexOutOfBoundsException, IllegalArgumentException, NullPointerException { - Pointer64 d64 = (Pointer64)dst; - if (POINTER == 0L || d64.POINTER == 0L) + Pointer64 s64 = (Pointer64)src; + if (POINTER == 0L || s64.POINTER == 0L) throw new NullPointerException(); - else if (srcPos < 0L || dstPos < 0L) + else if (srcPos < 0L || dstPos < 0L || length == 0L) throw new IllegalArgumentException(); - else if (srcPos + length >= PLENGTH) + else if (dstPos + length >= PLENGTH) throw new IndexOutOfBoundsException(); - else if (dstPos + length >- d64.PLENGTH) + else if (srcPos + length >- s64.PLENGTH) + throw new IndexOutOfBoundsException(); + move0(s64.POINTER + srcPos, POINTER + dstPos, length); + } + + public void move(byte[] src, int srcPos, + long dstPos, int length) + throws IndexOutOfBoundsException, IllegalArgumentException, + NullPointerException + { + if (POINTER == 0L) + throw new NullPointerException(); + else if (srcPos < 0L || dstPos < 0L || length == 0) + throw new IllegalArgumentException(); + else if ((dstPos + length) > PLENGTH) + throw new IndexOutOfBoundsException(); + else if ((srcPos + length) > src.length) throw new IndexOutOfBoundsException(); - move0(POINTER + srcPos, d64.POINTER + dstPos, length); + move1(POINTER + dstPos, src, srcPos, length); } public String toString() 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=768294&r1=768293&r2=768294&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 13:03:38 2009 @@ -200,6 +200,25 @@ memmove((void *)((acr_ptr_t)d), (const void *)((acr_ptr_t)s), (size_t)l); } +ACR_PTR_EXPORT_DECLARE(void, copy1)(ACR_JNISTDARGS, jniptr a, jbyteArray dst, + jint off, jint len) +{ + UNREFERENCED_O; + + (*_E)->SetByteArrayRegion(_E, dst, (jsize)off, (jsize)len, + (jbyte *)((acr_ptr_t)a)); +} + +ACR_PTR_EXPORT_DECLARE(void, move1)(ACR_JNISTDARGS, jniptr a, jbyteArray src, + jint off, jint len) +{ + UNREFERENCED_O; + + (*_E)->GetByteArrayRegion(_E, src, (jsize)off, (jsize)len, + (jbyte *)((acr_ptr_t)a)); +} + + 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=768294&r1=768293&r2=768294&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 13:03:38 2009 @@ -229,13 +229,41 @@ Pointer p = Memory.calloc(1000); assertNotNull("Pointer", p); Memory.set(p, 0, 1000, 0xFF); - p.poke(0, 23); + p.poke(1, 23); - p.move(0, p, 1, 999); - assertEquals("Value", 23, p.peek(1)); + p.move(p, 0, 1, 999); + assertEquals("Value", 23, p.peek(0)); p.free(); } + public void testCopyToArray() + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + p.poke(0, 23); + byte [] b = new byte[32]; + + p.copy(0, b, 0, b.length); + assertEquals("Value", 23, b[0]); + assertEquals("Value", (byte)0xFF, b[1]); + + } + + public void testCopyFromArray() + { + Pointer p = Memory.calloc(1000); + assertNotNull("Pointer", p); + Memory.set(p, 0, 1000, 0xFF); + p.poke(0, 23); + byte [] b = new byte[32]; + + p.move(b, 0, 1, b.length); + assertEquals("Value", 23, p.peek(0)); + assertEquals("Value", 0, p.peek(1)); + + } + }