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 2011/04/12 10:24:39 UTC

svn commit: r1091322 - in /commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime: ConstPointer.java GenericPointer.java Pointer.java

Author: mturk
Date: Tue Apr 12 08:24:39 2011
New Revision: 1091322

URL: http://svn.apache.org/viewvc?rev=1091322&view=rev
Log:
Make sure each pointer type has its own cleanup

Modified:
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GenericPointer.java
    commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/Pointer.java

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java?rev=1091322&r1=1091321&r2=1091322&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/ConstPointer.java Tue Apr 12 08:24:39 2011
@@ -28,7 +28,6 @@ final class ConstPointer extends Pointer
     private ConstPointer()
     {
         // No instance
-        ISCONST = true;
     }
 
     /*
@@ -38,7 +37,19 @@ final class ConstPointer extends Pointer
     {
         POINTER = ptr;
         PLENGTH = len;
-        ISCONST = true;
+    }
+
+    @Override
+    public final boolean isConst()
+    {
+        return true;
+    }
+    
+    @Override
+    public final void free()
+        throws Throwable
+    {
+        throw new UnsupportedOperationException();
     }
 
 }

Modified: commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GenericPointer.java
URL: http://svn.apache.org/viewvc/commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GenericPointer.java?rev=1091322&r1=1091321&r2=1091322&view=diff
==============================================================================
--- commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GenericPointer.java (original)
+++ commons/sandbox/runtime/trunk/src/main/java/org/apache/commons/runtime/GenericPointer.java Tue Apr 12 08:24:39 2011
@@ -24,6 +24,9 @@ package org.apache.commons.runtime;
 final class GenericPointer extends Pointer
 {
 
+    private static native void free0(long p)
+        throws Throwable;
+
     private GenericPointer()
     {
         // No instance
@@ -36,8 +39,43 @@ final class GenericPointer extends Point
     {
         POINTER = ptr;
         PLENGTH = len;
-        ISCONST = false;
     }
 
+    @Override
+    public final boolean isConst()
+    {
+        return false;
+    }
+
+    @Override
+    public final void free()
+        throws Throwable
+    {
+        if (POINTER == 0L)
+            throw new NullPointerException();
+        try {
+            free0(POINTER);
+        } finally {
+            POINTER = 0L;
+        }
+    }
+
+    /**
+     * Called by the garbage collector when the object is destroyed.
+     * The class will free internal resources allocated by the Operating system.
+     * @see Object#finalize()
+     * @throws Throwable the {@code Exception} raised by this method.
+     */
+    @Override
+    protected final void finalize()
+        throws Throwable
+    {
+        try {
+            free0(POINTER);
+        } finally {
+            POINTER = 0L;
+        }
+    }
+    
 }
 

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=1091322&r1=1091321&r2=1091322&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 Tue Apr 12 08:24:39 2011
@@ -29,7 +29,6 @@ public abstract class Pointer implements
 
     protected long    POINTER;
     protected long    PLENGTH;
-    protected boolean ISCONST;
 
     /**
      * Create new {@code null} Pointer instance.
@@ -56,9 +55,6 @@ public abstract class Pointer implements
         // No Instance
     }
 
-    private native void free0(long p)
-        throws Throwable;
-
     private static native Pointer nullptr0();
 
     /**
@@ -74,7 +70,7 @@ public abstract class Pointer implements
      *
      * @return Internal pointer address casted to the {@code long}.
      */
-    public long address()
+    public final long address()
     {
         return POINTER;
     }
@@ -88,7 +84,7 @@ public abstract class Pointer implements
      *
      * @return Internal pointer size.
      */
-    public long sizeof()
+    public final long sizeof()
     {
         return PLENGTH;    
     }
@@ -98,12 +94,18 @@ public abstract class Pointer implements
      * Check if the pointer is valid
      * @return true if the internal pointer is not {@code NULL}.
      */
-    public boolean isNull()
+    public final boolean isNull()
     {
         return POINTER == 0L;
     }
 
     /**
+     * Check if the pointer is valid
+     * @return true if the internal pointer is not {@code NULL}.
+     */
+    public abstract boolean isConst();
+
+    /**
      * Compares this {@code Pointer} to the specified object.
      *
      * @param other a {@code Pointer}
@@ -113,7 +115,7 @@ public abstract class Pointer implements
      *      equal. Returns false otherwise.
      */
     @Override
-    public boolean equals(Object other)
+    public final boolean equals(Object other)
     {
         if (other == null)
             return false;
@@ -134,7 +136,7 @@ public abstract class Pointer implements
      *         is less then, equal, or greater then the specified object.
      */
     @Override
-    public int compareTo(Pointer other)
+    public final int compareTo(Pointer other)
         throws ClassCastException
     {
         if (other == null)
@@ -152,24 +154,6 @@ public abstract class Pointer implements
     }
 
     /**
-     * Called by the garbage collector when the object is destroyed.
-     * The class will free internal resources allocated by the Operating system.
-     * @see Object#finalize()
-     * @throws Throwable the {@code Exception} raised by this method.
-     */
-    @Override
-    protected final void finalize()
-        throws Throwable
-    {
-        try {
-            if (!ISCONST)
-                free0(POINTER);
-        } finally {
-            POINTER = 0L;
-        }
-    }
-
-    /**
      * Free the allocated resource by the Operating system.
      * <p>
      * Note that {@code Object.finalize()} method will call
@@ -179,19 +163,8 @@ public abstract class Pointer implements
      * @see #finalize()
      * @throws Throwable the {@code Exception} raised by this method.
      */
-    public final void free()
-        throws Throwable
-    {
-        if (ISCONST)
-            throw new UnsupportedOperationException();
-        if (POINTER == 0L)
-            throw new NullPointerException();
-        try {
-            free0(POINTER);
-        } finally {
-            POINTER = 0L;
-        }
-    }
+    public abstract void free()
+        throws Throwable;
 
     /**
      * Returns a string representation of the Pointer.