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/12/03 08:19:44 UTC

svn commit: r886687 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/util/Array.java native/include/acr_private.h native/shared/array.c

Author: mturk
Date: Thu Dec  3 07:19:25 2009
New Revision: 886687

URL: http://svn.apache.org/viewvc?rev=886687&view=rev
Log:
Add few more array methods

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
    commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
    commons/sandbox/runtime/trunk/src/main/native/shared/array.c

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java?rev=886687&r1=886686&r2=886687&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java Thu Dec  3 07:19:25 2009
@@ -17,7 +17,7 @@
 package org.apache.commons.runtime.util;
 
 /**
- * Array routines.
+ * Primitive array helper routines.
  *
  * @author Mladen Turk
  * @since Runtime 1.0
@@ -29,9 +29,17 @@
         // No instance
     }
 
-    private static native boolean copy0(Object src, int srcPos, int srcSiz,
-                                        Object dst, int dstPos, int dstSiz,
-                                        int length);
+    private static native boolean memcpy0(Object src, int srcPos, int srcSiz,
+                                          Object dst, int dstPos, int dstSiz,
+                                          int length);
+    private static native boolean memcpy1(Object src, int srcPos, int srcSiz,
+                                          int dstPos, int length);
+    private static native int     memcmp0(Object src, int srcPos, int srcSiz,
+                                          Object dst, int dstPos, int dstSiz,
+                                          int length)
+        throws ArrayIndexOutOfBoundsException;
+    private static native boolean memset0(Object dst, int dstPos, int dstSiz,
+                                          int number, int length);
 
     private static int getComponentSize(Class<?> componentType)
     {
@@ -77,7 +85,13 @@
      */
     public static void copy(Object src, int srcPos, Object dst,
                             int dstPos, int length)
+        throws ArrayIndexOutOfBoundsException, ArrayStoreException,
+               NullPointerException
     {
+        if (srcPos < 0 || dstPos < 0 || length < 0) {
+            // Sanity check
+            throw new ArrayIndexOutOfBoundsException();
+        }
         // Sending getClass() to both arguments will check for null
         Class<?> type1 = src.getClass();
         Class<?> type2 = dst.getClass();
@@ -91,15 +105,92 @@
             !componentType2.isPrimitive()) {
             throw new ArrayStoreException();
         }
+        int elemSize1 = getComponentSize(componentType1);
+        if (src == dst) {
+            if (!memcpy1(src, srcPos, elemSize1, dstPos, length))
+                throw new ArrayIndexOutOfBoundsException();
+        }
+        else {
+            int elemSize2 = getComponentSize(componentType2);
+            if (!memcpy0(src, srcPos, elemSize1, dst, dstPos,
+                         elemSize2, length))
+                throw new ArrayIndexOutOfBoundsException();
+        }
+    }
+
+    /**
+     */
+    public static void set(Object dst, int dstPos, int value, int length)
+        throws ArrayIndexOutOfBoundsException, ArrayStoreException,
+               NullPointerException
+    {
+        if (dstPos < 0 || length < 0) {
+            // Sanity check
+            throw new ArrayIndexOutOfBoundsException();
+        }
+        // Sending getClass() to both arguments will check for null
+        Class<?> type1 = dst.getClass();
+        if (!type1.isArray()) {
+            throw new ArrayStoreException();
+        }
+        Class<?> componentType1 = type1.getComponentType();
+
+        if (!componentType1.isPrimitive()) {
+            throw new ArrayStoreException();
+        }
+        int elemSize1 = getComponentSize(componentType1);
+
+        if (!memset0(dst, dstPos, elemSize1, value, length))
+            throw new ArrayIndexOutOfBoundsException();
+    }
+
+    public static void zero(Object dst, int dstPos, int length)
+        throws ArrayIndexOutOfBoundsException, ArrayStoreException,
+               NullPointerException
+    {
+        set(dst, dstPos, 0, length);
+    }
+
+    /**
+
+     * @param src
+     *            the source array to compare.
+     * @param srcPos
+     *            the starting index of the content in {@code src}.
+     * @param dst
+     *            the array to compare {@code src} with.
+     * @param dstPos
+     *            the starting index for the comare content in {@code dst}.
+     * @param length
+     *            the number of elements of the {@code src} content they have
+     *            to be compared.
+     */
+    public static int compare(Object src, int srcPos, Object dst,
+                              int dstPos, int length)
+        throws ArrayIndexOutOfBoundsException, ArrayStoreException,
+               NullPointerException
+    {
         if (srcPos < 0 || dstPos < 0 || length < 0) {
             // Sanity check
             throw new ArrayIndexOutOfBoundsException();
         }
+        // Sending getClass() to both arguments will check for null
+        Class<?> type1 = src.getClass();
+        Class<?> type2 = dst.getClass();
+        if (!type1.isArray() || !type2.isArray()) {
+            throw new ArrayStoreException();
+        }
+        Class<?> componentType1 = type1.getComponentType();
+        Class<?> componentType2 = type2.getComponentType();
+
+        if (!componentType1.isPrimitive() ||
+            !componentType2.isPrimitive()) {
+            throw new ArrayStoreException();
+        }
         int elemSize1 = getComponentSize(componentType1);
         int elemSize2 = getComponentSize(componentType2);
 
-        if (!copy0(src, srcPos, elemSize1, dst, dstPos, elemSize2, length))
-            throw new ArrayIndexOutOfBoundsException();
+        return memcmp0(src, srcPos, elemSize1, dst, dstPos, elemSize2, length);
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h?rev=886687&r1=886686&r2=886687&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr_private.h Thu Dec  3 07:19:25 2009
@@ -320,6 +320,11 @@
 #define J_DECLARE_F_ID(I) static JAVA_F_ID _f##I##n
 #define J_DECLARE_M_ID(I) static JAVA_M_ID _m##I##n
 
+#define J_DECLARE_FIELD(N, S) static JAVA_F_ID _f##N##n = { NULL, #N, #S }
+#define J_DECLARE_M_SET(N, S) static JAVA_M_ID _m##N##n = { NULL, "set" #N, "(" #S ")V" }
+#define J_DECLARE_M_GET(N, S) static JAVA_M_ID _m##N##n = { NULL, "get" #N, "()" #S     }
+#define J_DECLARE_M_INI(N, S) static JAVA_M_ID _m##N##n = { NULL, "<init>", "(" #S ")V" }
+
 #define J4MID(I)          _m##I##n.i
 
 #define J_LOAD_METHOD(I)    \

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/array.c?rev=886687&r1=886686&r2=886687&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Thu Dec  3 07:19:25 2009
@@ -31,14 +31,14 @@
  * Array utilities
  */
 
-ACR_UTIL_EXPORT_DECLARE(jboolean, Array, copy0)(ACR_JNISTDARGS,
-                                                jarray src,
-                                                jint srcPos,
-                                                jint srcSiz,
-                                                jarray dst,
-                                                jint dstPos,
-                                                jint dstSiz,
-                                                jint length)
+ACR_UTIL_EXPORT_DECLARE(jboolean, Array, memcpy0)(ACR_JNISTDARGS,
+                                                  jarray src,
+                                                  jint srcPos,
+                                                  jint srcSiz,
+                                                  jarray dst,
+                                                  jint dstPos,
+                                                  jint dstSiz,
+                                                  jint length)
 {
     jbyte *scp;
     jbyte *dcp;
@@ -61,9 +61,107 @@
         memmove(dcp + (size_t)dstOff,
                 scp + (size_t)srcOff, (size_t)nbytes);
         (*_E)->ReleasePrimitiveArrayCritical(_E, src, scp, 0);
-        (*_E)->ReleasePrimitiveArrayCritical(_E, src, dcp, 0);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, dst, dcp, 0);
         return JNI_TRUE;
     }
     else
         return JNI_FALSE;
 }
+
+ACR_UTIL_EXPORT_DECLARE(jboolean, Array, memcpy1)(ACR_JNISTDARGS,
+                                                  jarray src,
+                                                  jint srcPos,
+                                                  jint srcSiz,
+                                                  jint dstPos,
+                                                  jint length)
+{
+    jbyte *scp;
+    jint   srcLen;
+    jint   dstLen;
+    jint   srcOff = srcPos * srcSiz;
+    jint   dstOff = dstPos * srcSiz;
+    jint   nbytes = length * srcSiz;
+
+    srcLen = (jint)(*_E)->GetArrayLength(_E, src);
+
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((srcLen * srcSiz) - dstOff)))
+        return JNI_FALSE;
+
+    scp = (*_E)->GetPrimitiveArrayCritical(_E, src, NULL);
+    if (scp) {
+        memmove(scp + (size_t)dstOff,
+                scp + (size_t)srcOff, (size_t)nbytes);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, src, scp, 0);
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
+}
+
+ACR_UTIL_EXPORT_DECLARE(jboolean, Array, memset0)(ACR_JNISTDARGS,
+                                                  jarray dst,
+                                                  jint dstPos,
+                                                  jint dstSiz,
+                                                  jint number,
+                                                  jint length)
+{
+    jbyte *dcp;
+    jint   dstLen;
+    jint   dstOff = dstPos * dstSiz;
+    jint   nbytes = length * dstSiz;
+
+    dstLen = (jint)(*_E)->GetArrayLength(_E, dst);
+
+    if (nbytes > ((dstLen * dstSiz) - dstOff))
+        return JNI_FALSE;
+
+    dcp = (*_E)->GetPrimitiveArrayCritical(_E, dst, NULL);
+    if (dcp) {
+        memset(dcp + (size_t)dstOff, number, (size_t)nbytes);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, dst, dcp, 0);
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
+}
+
+ACR_UTIL_EXPORT_DECLARE(jint, Array, memcmp0)(ACR_JNISTDARGS,
+                                              jarray src,
+                                              jint srcPos,
+                                              jint srcSiz,
+                                              jarray dst,
+                                              jint dstPos,
+                                              jint dstSiz,
+                                              jint length)
+{
+    jbyte *scp;
+    jbyte *dcp;
+    jint   srcLen;
+    jint   dstLen;
+    jint   srcOff = srcPos * srcSiz;
+    jint   dstOff = dstPos * dstSiz;
+    jint   nbytes = length * srcSiz;
+
+    srcLen = (jint)(*_E)->GetArrayLength(_E, src);
+    dstLen = (jint)(*_E)->GetArrayLength(_E, dst);
+
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((dstLen * dstSiz) - dstOff))) {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+    scp = (*_E)->GetPrimitiveArrayCritical(_E, src, NULL);
+    dcp = (*_E)->GetPrimitiveArrayCritical(_E, dst, NULL);
+    if (scp && dcp) {
+        int rv = memcmp(dcp + (size_t)dstOff,
+                        scp + (size_t)srcOff, (size_t)nbytes);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, src, scp, 0);
+        (*_E)->ReleasePrimitiveArrayCritical(_E, dst, dcp, 0);
+        return rv;
+    }
+    else {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_EINDEX, 0);
+        return -1;
+    }
+}