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 2009/04/22 11:05:19 UTC
svn commit: r767415 - in /commons/sandbox/runtime/trunk/src:
main/java/org/apache/commons/runtime/Memory.java
main/native/shared/memory.c test/org/apache/commons/runtime/TestMemory.java
Author: mturk
Date: Wed Apr 22 09:05:18 2009
New Revision: 767415
URL: http://svn.apache.org/viewvc?rev=767415&view=rev
Log:
Add Memory.dup
Modified:
commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java
commons/sandbox/runtime/trunk/src/main/native/shared/memory.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/Memory.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Memory.java?rev=767415&r1=767414&r2=767415&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 Wed Apr 22 09:05:18 2009
@@ -78,6 +78,25 @@
NullPointerException;
/**
+ * Creates a new {@link Pointer} object whose content is a duplicated
+ * subsequence of a {@code src} memory address.
+ *
+ * @param src {@code Pointer} whose content to use.
+ * @param offset offset from the {@code src} memory address.
+ * @param size size of the destination memory.
+ * @return new {@link Pointer}.
+ *
+ * @throws IllegalArgumentException if the {@code offset} is
+ * {@code negative} or {@code size} is {@code zero}.
+ * @throws IndexOutOfBoundsException if the operation would cause
+ * access of data outside allocated memory bounds.
+ * @throws NullPointerException if {@code src} is {@code null}.
+ */
+ public static native Pointer dup(Pointer src, long offset, long size)
+ throws IndexOutOfBoundsException, IllegalArgumentException,
+ NullPointerException;
+
+ /**
* Copy the memory area from {@code src} to {@code dst}.
* <p>
* Method uses the {@code memcpy} function to do a copying, meaning
Modified: commons/sandbox/runtime/trunk/src/main/native/shared/memory.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/memory.c?rev=767415&r1=767414&r2=767415&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/memory.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/memory.c Wed Apr 22 09:05:18 2009
@@ -276,7 +276,6 @@
jobject src, jlong off,
jlong siz)
{
- jobject ptr = NULL;
size_t so = (size_t)off;
size_t ss = (size_t)siz;
size_t sl;
@@ -300,8 +299,42 @@
/* Create the Pointer class without the cleanup.
* Original object will clean the entire allocated memory.
*/
- ptr = ACR_PointerCreate(_E, sp + so, ss, NULL);
- return ptr;
+ return ACR_PointerCreate(_E, sp + so, ss, NULL);
+}
+
+ACR_JNI_EXPORT_DECLARE(jobject, Memory, dup)(ACR_JNISTDARGS,
+ jobject src, jlong off,
+ jlong siz)
+{
+ size_t so = (size_t)off;
+ size_t ss = (size_t)siz;
+ size_t sl;
+ void *dp;
+ char *sp = (char *)ACR_PointerGet(_E, src, &sl);
+
+ UNREFERENCED_O;
+
+ if (!sp) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ENULL, 0);
+ return NULL;
+ }
+ if (off < 0L || siz < 1L) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINVAL, 0);
+ return NULL;
+ }
+ if ((so + ss) > sl) {
+ ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+ return NULL;
+ }
+ dp = ACR_Malloc(_E, THROW_NMARK, ACR_ALIGN_DEFAULT(ss));
+ if (dp == NULL) {
+ /* Allocation failed.
+ */
+ return NULL;
+ }
+ /* Copy the original content */
+ memcpy(dp, sp + so, ss);
+ return ACR_PointerCreate(_E, dp, ss, ptr_cleanup);
}
ACR_JNI_EXPORT_DECLARE(void, Memory, copy)(ACR_JNISTDARGS, jobject src,
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=767415&r1=767414&r2=767415&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 Wed Apr 22 09:05:18 2009
@@ -95,6 +95,17 @@
dst.free();
}
+ public void testDup()
+ throws Throwable
+ {
+ Pointer src = Memory.calloc(1000);
+ assertNotNull("Pointer", src);
+ Pointer dup = Memory.dup(src, 0, 1000);
+ assertNotNull("Pointer", dup);
+ src.free();
+ dup.free();
+ }
+
public void testSet()
throws Throwable
{