commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mt...@apache.org
Subject svn commit: r790485 - in /commons/sandbox/runtime/trunk/src: main/java/org/apache/commons/runtime/ main/native/shared/ test/org/apache/commons/runtime/
Date Thu, 02 Jul 2009 07:49:32 GMT
Author: mturk
Date: Thu Jul  2 07:49:32 2009
New Revision: 790485

URL: http://svn.apache.org/viewvc?rev=790485&view=rev
Log:
Implement Pointer Comparable using memcmp

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=790485&r1=790484&r2=790485&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 Thu
Jul  2 07:49:32 2009
@@ -22,7 +22,7 @@
  * </p>
  * @since Runtime 1.0
  */
-public abstract class Pointer
+public abstract class Pointer implements Comparable<Pointer>
 {
 
     /**
@@ -138,6 +138,17 @@
     public abstract boolean equals(Object other);
 
     /**
+     * Compares this {@code Pointer} to the specified object for order
+     *
+     * @param other the {@code Pointer} to be Compared
+     * @return a negative integer, zero or positive integer as this object
+     *         is less then, equal, or greater then the specified object.
+     */
+    @Override
+    public abstract int compareTo(Pointer other)
+        throws ClassCastException;
+
+    /**
      * Called by the garbage collector when the object is destroyed.
      * The class will free internal resources allocated by the Operating system.
      * @see Object#finalize()

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=790485&r1=790484&r2=790485&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
Thu Jul  2 07:49:32 2009
@@ -143,5 +143,26 @@
         }
     }
 
+    private static native int memcmp0(int a, int n, int length);
+    public int compareTo(Pointer other)
+        throws ClassCastException
+    {
+        if (other == null)
+            throw new ClassCastException();
+        if (other instanceof Pointer32) {
+            Pointer32 d32 = (Pointer32)other;
+            if (POINTER == 0)
+                return -1;
+            if (d32.POINTER == 0)
+                return 1;
+            if (PLENGTH > d32.PLENGTH)
+                return 1;
+            else
+                return memcmp0(POINTER, d32.POINTER, PLENGTH);
+        }
+        else
+            throw new ClassCastException();
+    }
+
 }
 

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=790485&r1=790484&r2=790485&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
Thu Jul  2 07:49:32 2009
@@ -132,6 +132,27 @@
         move0(s64.POINTER + dstPos, POINTER + srcPos, length);
     }
 
+    private static native int memcmp0(long a, long b, long length);
+    public int compareTo(Pointer other)
+        throws ClassCastException
+    {
+        if (other == null)
+            throw new ClassCastException();
+        if (other instanceof Pointer64) {
+            Pointer64 d64 = (Pointer64)other;
+            if (POINTER == 0L)
+                return -1;
+            if (d64.POINTER == 0L)
+                return 1;
+            if (PLENGTH > d64.PLENGTH)
+                return 1;
+            else
+                return memcmp0(POINTER, d64.POINTER, PLENGTH);
+        }
+        else
+            throw new ClassCastException();
+    }
+
     public String toString()
     {
         if (POINTER != 0L) {

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=790485&r1=790484&r2=790485&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c (original)
+++ commons/sandbox/runtime/trunk/src/main/native/shared/pointer.c Thu Jul  2 07:49:32 2009
@@ -211,6 +211,18 @@
     }
 }
 
+ACR_PTR_EXPORT_DECLARE(int, memcmp0)(ACR_JNISTDARGS, jniptr a,
+                                     jniptr b, jniptr n)
+{
+    UNREFERENCED_STDARGS;
+    ACR_TRY {
+        return memcmp(N2P(a, void *), N2P(b, const void *), (size_t)n);
+    } ACR_CATCH() {
+        ACR_ThrowException(_E, THROW_NMARK, ACR_EX_ERUNTIME, EFAULT);
+        return -1;
+    }
+}
+
 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=790485&r1=790484&r2=790485&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 Thu
Jul  2 07:49:32 2009
@@ -374,5 +374,92 @@
         p.free();
     }
 
+    public void testCompare()
+        throws Throwable
+    {
+        Pointer a = Memory.calloc(1000);
+        assertNotNull("Pointer", a);
+        Memory.set(a, 0, 1000, 0xFF);
+        a.poke(1, 23);
+
+        Pointer b = Memory.calloc(1000);
+        assertNotNull("Pointer", b);
+        Memory.set(b, 0, 1000, 0xFF);
+        b.poke(1, 23);
+        assertEquals("Compare", 0, a.compareTo(b));
+        a.free();
+        b.free();
+    }
+
+    public void testCompareLen()
+        throws Throwable
+    {
+        Pointer a = Memory.calloc(500);
+        assertNotNull("Pointer", a);
+        Memory.set(a, 0, 500, 0xFF);
+        a.poke(1, 23);
+
+        Pointer b = Memory.calloc(1000);
+        assertNotNull("Pointer", b);
+        Memory.set(b, 0, 1000, 0xFF);
+        b.poke(1, 23);
+        assertEquals("Compare", 0, a.compareTo(b));
+        a.free();
+        b.free();
+    }
+
+    public void testCompareLess()
+        throws Throwable
+    {
+        Pointer a = Memory.calloc(500);
+        assertNotNull("Pointer", a);
+        Memory.set(a, 0, 500, 0xFF);
+        a.poke(1, 23);
+
+        Pointer b = Memory.calloc(1000);
+        assertNotNull("Pointer", b);
+        Memory.set(b, 0, 1000, 0xFF);
+        b.poke(1, 64);
+        assertEquals("Compare", -1, a.compareTo(b));
+        a.free();
+        b.free();
+    }
+
+    public void testCompareGreater()
+        throws Throwable
+    {
+        Pointer a = Memory.calloc(500);
+        assertNotNull("Pointer", a);
+        Memory.set(a, 0, 500, 0xFF);
+        a.poke(1, 23);
+
+        Pointer b = Memory.calloc(500);
+        assertNotNull("Pointer", b);
+        Memory.set(b, 0, 500, 0xFF);
+        b.poke(1, 3);
+        assertEquals("Compare A",  1, a.compareTo(b));
+        assertEquals("Compare B", -1, b.compareTo(a));
+        a.free();
+        b.free();
+    }
+
+    public void testCompareSmaller()
+        throws Throwable
+    {
+        Pointer a = Memory.calloc(500);
+        assertNotNull("Pointer", a);
+        Memory.set(a, 0, 500, 0xFF);
+        a.poke(1, 23);
+
+        Pointer b = Memory.calloc(400);
+        assertNotNull("Pointer", b);
+        Memory.set(b, 0, 400, 0xFF);
+        b.poke(1, 64);
+        assertEquals("Smaller size", 1, a.compareTo(b));
+        assertEquals("Greater then", 1, b.compareTo(a));
+        a.free();
+        b.free();
+    }
+
 }
 



Mime
View raw message