commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r1091672 - in /commons/sandbox/runtime/trunk/src/main: java/org/apache/commons/runtime/util/ native/ native/include/acr/ native/shared/ test/org/apache/commons/runtime/
Date Wed, 13 Apr 2011 06:49:57 GMT
Author: mturk
Date: Wed Apr 13 06:49:57 2011
New Revision: 1091672

URL: http://svn.apache.org/viewvc?rev=1091672&view=rev
Log:
Add array copy/compare

Added:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
  (with props)
    commons/sandbox/runtime/trunk/src/main/native/shared/array.c   (with props)
    commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
  (with props)
Modified:
    commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
    commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
    commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h
    commons/sandbox/runtime/trunk/src/main/native/shared/error.c

Added: 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=1091672&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
Wed Apr 13 06:49:57 2011
@@ -0,0 +1,197 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime.util;
+
+/**
+ * Primitive array helper routines.
+ *
+ * @author Mladen Turk
+ * @since Runtime 1.0
+ */
+public final class Array
+{
+    private Array()
+    {
+        // No instance
+    }
+
+    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)
+    {
+        if (componentType == Integer.TYPE)
+            return 4;
+        else if (componentType == Byte.TYPE)
+            return 1;
+        else if (componentType == Long.TYPE)
+            return 8;
+        else if (componentType == Short.TYPE)
+            return 2;
+        else if (componentType == Character.TYPE)
+            return 2;
+        else if (componentType == Boolean.TYPE)
+            return 1;
+        else if (componentType == Double.TYPE)
+            return 8;
+        else if (componentType == Float.TYPE)
+            return 4;
+        else
+            return 0;
+    }
+    /**
+     * System.arraycopy replacement that can copy between arrays of
+     * different primitive types.
+     * <p>
+     * Copies the number of {@code length} elements of the Array {@code src}
+     * starting at the offset {@code srcPos} into the Array {@code dst} at
+     * the position {@code dstPos}.
+     * </p>
+
+     * @param src
+     *            the source array to copy the content.
+     * @param srcPos
+     *            the starting index of the content in {@code src}.
+     * @param dst
+     *            the destination array to copy the data into.
+     * @param dstPos
+     *            the starting index for the copied content in {@code dst}.
+     * @param length
+     *            the number of elements of the {@code src} content they have
+     *            to be copied.
+     */
+    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();
+        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);
+        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);
+
+        return memcmp0(src, srcPos, elemSize1, dst, dstPos, elemSize2, length);
+    }
+
+}
+

Propchange: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/util/Array.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in?rev=1091672&r1=1091671&r2=1091672&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in (original)
+++ commons/sandbox/runtime/trunk/src/main/native/Makefile.unx.in Wed Apr 13 06:49:57 2011
@@ -87,6 +87,7 @@ LIBSOURCES=\
 	$(TOPDIR)/port/bsdrand.c \
 	$(TOPDIR)/port/bsdstring.c \
 	$(TOPDIR)/port/bsdsys.c \
+	$(TOPDIR)/shared/array.c \
 	$(TOPDIR)/shared/bzip2.c \
 	$(TOPDIR)/shared/callback.c \
 	$(TOPDIR)/shared/clazz.c \

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h?rev=1091672&r1=1091671&r2=1091672&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/error.h Wed Apr 13 06:49:57
2011
@@ -43,6 +43,7 @@ enum {
     ACR_EX_EINVAL,          /* java/lang/IllegalArgumentException */
     ACR_EX_EISTATE,         /* java/lang/IllegalStateException */
     ACR_EX_EINDEX,          /* java/lang/IndexOutOfBoundsException */
+    ACR_EX_EAINDEX,         /* java/lang/ArrayIndexOutOfBoundsException */
     ACR_EX_ECCAST,          /* java/lang/ClassCastException */
     ACR_EX_EINSTANCE,       /* java/lang/InstantiationException */
     ACR_EX_EINTERNAL,       /* java/lang/InternalError */

Modified: commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h?rev=1091672&r1=1091671&r2=1091672&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h (original)
+++ commons/sandbox/runtime/trunk/src/main/native/include/acr/jnidefs.h Wed Apr 13 06:49:57
2011
@@ -43,7 +43,7 @@
 #define ACR_NET_EXPORT(RT, CL, MN)  \
     ACR_JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_net_##CL##_##MN
 #define ACR_UTIL_EXPORT(RT, CL, MN)  \
-    ACR_JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_net_##CL##_##MN
+    ACR_JNIEXPORT RT JNICALL Java_org_apache_commons_runtime_util_##CL##_##MN
 
 #define JNI_STDARGS             JNIEnv *env, jobject obj
 #define JNI_STDENV              JNIEnv *env

Added: 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=1091672&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/array.c (added)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/array.c Wed Apr 13 06:49:57 2011
@@ -0,0 +1,157 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "acr/memory.h"
+#include "acr/clazz.h"
+
+/**
+ * Array utilities
+ */
+
+ACR_UTIL_EXPORT(jboolean, Array, memcpy0)(JNI_STDARGS,
+                                          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)(*env)->GetArrayLength(env, src);
+    dstLen = (jint)(*env)->GetArrayLength(env, dst);
+
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((dstLen * dstSiz) - dstOff)))
+        return JNI_FALSE;
+
+    scp = (*env)->GetPrimitiveArrayCritical(env, src, 0);
+    if (scp == 0)
+        return JNI_FALSE;
+    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
+    if (dcp == 0) {
+        (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+        return JNI_FALSE;
+    }        
+    memmove(dcp + (size_t)dstOff,
+            scp + (size_t)srcOff, (size_t)nbytes);
+    (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+    (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+    return JNI_TRUE;
+}
+
+ACR_UTIL_EXPORT(jboolean, Array, memcpy1)(JNI_STDARGS,
+                                          jarray src,
+                                          jint srcPos,
+                                          jint srcSiz,
+                                          jint dstPos,
+                                          jint length)
+{
+    jbyte *scp;
+    jint   srcLen;
+    jint   srcOff = srcPos * srcSiz;
+    jint   dstOff = dstPos * srcSiz;
+    jint   nbytes = length * srcSiz;
+
+    srcLen = (jint)(*env)->GetArrayLength(env, src);
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((srcLen * srcSiz) - dstOff)))
+        return JNI_FALSE;
+
+    scp = (*env)->GetPrimitiveArrayCritical(env, src, 0);
+    if (scp != 0) {
+        memmove(scp + (size_t)dstOff,
+                scp + (size_t)srcOff, (size_t)nbytes);
+        (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
+}
+
+ACR_UTIL_EXPORT(jboolean, Array, memset0)(JNI_STDARGS,
+                                          jarray dst,
+                                          jint dstPos,
+                                          jint dstSiz,
+                                          jint number,
+                                          jint length)
+{
+    jbyte *dcp;
+    jint   dstLen;
+    jint   dstOff = dstPos * dstSiz;
+    jint   nbytes = length * dstSiz;
+
+    dstLen = (jint)(*env)->GetArrayLength(env, dst);
+    if (nbytes > ((dstLen * dstSiz) - dstOff))
+        return JNI_FALSE;
+
+    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
+    if (dcp != 0) {
+        memset(dcp + (size_t)dstOff, number, (size_t)nbytes);
+        (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+        return JNI_TRUE;
+    }
+    else
+        return JNI_FALSE;
+}
+
+ACR_UTIL_EXPORT(jint, Array, memcmp0)(JNI_STDARGS,
+                                      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;
+    jint   rv;
+
+    srcLen = (jint)(*env)->GetArrayLength(env, src);
+    dstLen = (jint)(*env)->GetArrayLength(env, dst);
+    if ((length > (srcLen - srcPos)) ||
+        (nbytes > ((dstLen * dstSiz) - dstOff))) {
+        ACR_THROW(ACR_EX_EAINDEX, 0);
+        return -1;
+    }
+
+    scp = (*env)->GetPrimitiveArrayCritical(env, src, 0);
+    if (scp == 0)
+        return -1;
+    dcp = (*env)->GetPrimitiveArrayCritical(env, dst, 0);
+    if (dcp == 0) {
+        (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+        return -1;        
+    }
+    rv = memcmp(dcp + (size_t)dstOff,
+                scp + (size_t)srcOff, (size_t)nbytes);
+    (*env)->ReleasePrimitiveArrayCritical(env, src, scp, 0);
+    (*env)->ReleasePrimitiveArrayCritical(env, dst, dcp, 0);
+    return rv;
+}

Propchange: commons/sandbox/runtime/trunk/src/main/native/shared/array.c
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: commons/sandbox/runtime/trunk/src/main/native/shared/error.c
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/native/shared/error.c?rev=1091672&r1=1091671&r2=1091672&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/error.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/error.c Wed Apr 13 06:49:57 2011
@@ -37,6 +37,7 @@ static struct {
     { 0, "java/lang/IllegalArgumentException"                   },
     { 0, "java/lang/IllegalStateException"                      },
     { 0, "java/lang/IndexOutOfBoundsException"                  },
+    { 0, "java/lang/ArrayIndexOutOfBoundsException"             },
     { 0, "java/lang/ClassCastException"                         },
     { 0, "java/lang/InstantiationException"                     },
     { 0, "java/lang/InternalError"                              },

Added: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java?rev=1091672&view=auto
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
(added)
+++ commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
Wed Apr 13 06:49:57 2011
@@ -0,0 +1,42 @@
+/* Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.runtime;
+
+import java.io.IOException;
+import java.io.File;
+import org.testng.annotations.*;
+import org.testng.Assert;
+import org.apache.commons.runtime.util.Array;
+
+public class TestArray
+{
+
+    @Test(groups = { "utils" })
+    public void arrayCopy()
+        throws Exception
+    {
+        int[]  ia = { 1, 2, 3, 4 };
+        byte[] ba = new byte[64];
+
+        Array.copy(ia, 1, ba, 0, 3);
+        // bytes 0 .. 3 contains integer[1] (2)
+        // ### Assertion works on LSB machines only
+        Assert.assertEquals(2, (int)ba[0] & 0xff);
+        Assert.assertEquals(3, (int)ba[4] & 0xff);
+        Assert.assertEquals(4, (int)ba[8] & 0xff);
+    }
+}

Propchange: commons/sandbox/runtime/trunk/src/main/test/org/apache/commons/runtime/TestArray.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message