directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r233110 - in /directory/network: branches/0.7/src/java/org/apache/mina/common/ branches/0.7/src/test/org/apache/mina/common/ trunk/src/java/org/apache/mina/common/ trunk/src/test/org/apache/mina/common/
Date Wed, 17 Aug 2005 03:00:19 GMT
Author: trustin
Date: Tue Aug 16 20:00:08 2005
New Revision: 233110

URL: http://svn.apache.org/viewcvs?rev=233110&view=rev
Log:
Fix for DIRMINA-78 - ByteBuffer.wrap((byte[],?) The element of least suprise:
* Added 'pooled' property to ByteBuffer
* Revised JavaDocs to guide users to use setPooled() method instead of additional acquire()
* Added a test case related with 'pooled' property.



Modified:
    directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java
    directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java
    directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
    directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java

Modified: directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBuffer.java Tue Aug
16 20:00:08 2005
@@ -90,33 +90,11 @@
  * <h2>Wrapping existing NIO buffers and arrays</h2>
  * <p>
  * This class provides a few <tt>wrap(...)</tt> methods that wraps
- * any NIO buffers and byte arrays.  But please be careful and use these
- * methods at your own risk.  Using those methods can cause memory leakage
- * if you keep wrapping new NIO buffers and arrays because even wrapped
- * MINA buffers are going to be pooled when they are released by MINA.
- * <p>
- * To resolve this issue, please do not keeping wrapping NIO buffers if
- * possible.  If you're working with any third party component that keeps
- * creating NIO buffers, please call {@link #acquire()} once more and
- * don't call {@link #release()} for it, then it will not be returned to
- * the pool and GC'd eventually.  Here's the example:
- * <pre>
- * import org.apache.mina.common.*;
- * import org.apache.mina.io.*;
+ * any NIO buffers and byte arrays.  Wrapped MINA buffers are not returned
+ * to the buffer pool by default to prevent unexpected memory leakage by default.
+ * In case you want to make it pooled, you can call {@link #setPooled(boolean)}
+ * with <tt>true</tt> flag to enable pooling.
  *
- * IoSession session = ...;
- * for (;;) {
- *     // readData() returns a newly allocate NIO buffer.
- *     java.nio.ByteBuffer newBuffer = otherApplication.readData();
- *     
- *     // Wrap it.
- *     ByteBuffer wrappedBuffer = ByteBuffer.wrap(newBuffer);
- *     // Acquire once and don't call release to prevent MINA from pooling it. 
- *     wrappedBuffer.acquire();
- *     session.write(wrappedBuffer, marker);
- * }
- * </pre>
- * 
  * <h2>AutoExpand</h2>
  * <p>
  * Writing variable-length data using NIO <tt>ByteBuffers</tt> is not really
@@ -259,6 +237,7 @@
     {
         DefaultByteBuffer buf = allocateContainer();
         buf.init( nioBuffer );
+        buf.setPooled( false );
         return buf;
     }
     
@@ -340,6 +319,27 @@
      */
     public abstract ByteBuffer setAutoExpand( boolean autoExpand );
     
+    /**
+     * Returns <tt>true</tt> if and only if this buffer is returned back
+     * to the buffer pool when released.
+     * <p>
+     * The default value of this property is <tt>true</tt> if and only if you
+     * allocated this buffer using {@link #allocate(int)} or {@link #allocate(int, boolean)},
+     * or <tt>false</tt> otherwise. (i.e. {@link #wrap(byte[])}, {@link #wrap(byte[],
int, int)},
+     * and {@link #wrap(java.nio.ByteBuffer)})
+     */
+    public abstract boolean isPooled();
+
+    /**
+     * Sets whether this buffer is returned back to the buffer pool when released.
+     * <p>
+     * The default value of this property is <tt>true</tt> if and only if you
+     * allocated this buffer using {@link #allocate(int)} or {@link #allocate(int, boolean)},
+     * or <tt>false</tt> otherwise. (i.e. {@link #wrap(byte[])}, {@link #wrap(byte[],
int, int)},
+     * and {@link #wrap(java.nio.ByteBuffer)})
+     */
+    public abstract void setPooled( boolean pooled );
+    
     public abstract int position();
 
     public abstract ByteBuffer position( int newPosition );
@@ -557,6 +557,7 @@
         private java.nio.ByteBuffer buf;
         private int refCount = 1;
         private boolean autoExpand;
+        private boolean pooled;
 
         protected DefaultByteBuffer()
         {
@@ -566,6 +567,7 @@
         {
             this.buf = buf;
             autoExpand = false;
+            pooled = true;
             refCount = 1;
         }
         
@@ -597,7 +599,11 @@
                 }
             }
 
-            release0( buf );
+            if( pooled )
+            {
+                release0( buf );
+            }
+
             synchronized( containerStack )
             {
                 containerStack.push( this );
@@ -628,6 +634,16 @@
         {
             this.autoExpand = autoExpand;
             return this;
+        }
+
+        public boolean isPooled()
+        {
+            return pooled;
+        }
+        
+        public void setPooled( boolean pooled )
+        {
+            this.pooled = pooled;
         }
 
         public int capacity()

Modified: directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/network/branches/0.7/src/java/org/apache/mina/common/ByteBufferProxy.java Tue
Aug 16 20:00:08 2005
@@ -506,4 +506,14 @@
         buf.setAutoExpand( autoExpand );
         return this;
     }
+
+    public boolean isPooled()
+    {
+        return buf.isPooled();
+    }
+    
+    public void setPooled( boolean pooled )
+    {
+        buf.setPooled( pooled );
+    }
 }

Modified: directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/branches/0.7/src/test/org/apache/mina/common/ByteBufferTest.java Tue
Aug 16 20:00:08 2005
@@ -147,6 +147,17 @@
         Assert.assertEquals( 4, buf.capacity() );
     }
     
+    public void testPooledProperty() throws Exception
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 16 );
+        java.nio.ByteBuffer nioBuf = buf.buf();
+        buf.release();
+        Assert.assertSame( nioBuf, ByteBuffer.allocate( 16 ).buf() );
+        buf.setPooled( false );
+        buf.release();
+        Assert.assertNotSame( nioBuf, ByteBuffer.allocate( 16 ).buf() );
+    }
+    
     public void testGetString() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBuffer.java Tue Aug 16 20:00:08
2005
@@ -91,32 +91,11 @@
  * <h2>Wrapping existing NIO buffers and arrays</h2>
  * <p>
  * This class provides a few <tt>wrap(...)</tt> methods that wraps
- * any NIO buffers and byte arrays.  But please be careful and use these
- * methods at your own risk.  Using those methods can cause memory leakage
- * if you keep wrapping new NIO buffers and arrays because even wrapped
- * MINA buffers are going to be pooled when they are released by MINA.
- * <p>
- * To resolve this issue, please do not keeping wrapping NIO buffers if
- * possible.  If you're working with any third party component that keeps
- * creating NIO buffers, please call {@link #acquire()} once more and
- * don't call {@link #release()} for it, then it will not be returned to
- * the pool and GC'd eventually.  Here's the example:
- * <pre>
- * import org.apache.mina.common.*;
- * 
- * IoSession session = ...;
- * for (;;) {
- *     // readData() returns a newly allocate NIO buffer.
- *     java.nio.ByteBuffer newBuffer = otherApplication.readData();
- *     
- *     // Wrap it.
- *     ByteBuffer wrappedBuffer = ByteBuffer.wrap(newBuffer);
- *     // Acquire once and don't call release to prevent MINA from pooling it. 
- *     wrappedBuffer.acquire();
- *     session.write(wrappedBuffer);
- * }
- * </pre>
- * 
+ * any NIO buffers and byte arrays.  Wrapped MINA buffers are not returned
+ * to the buffer pool by default to prevent unexpected memory leakage by default.
+ * In case you want to make it pooled, you can call {@link #setPooled(boolean)}
+ * with <tt>true</tt> flag to enable pooling.
+ *
  * <h2>AutoExpand</h2>
  * <p>
  * Writing variable-length data using NIO <tt>ByteBuffers</tt> is not really
@@ -257,6 +236,7 @@
     {
         DefaultByteBuffer buf = allocateContainer();
         buf.init( nioBuffer );
+        buf.setPooled( false );
         return buf;
     }
     
@@ -338,6 +318,27 @@
      */
     public abstract ByteBuffer setAutoExpand( boolean autoExpand );
     
+    /**
+     * Returns <tt>true</tt> if and only if this buffer is returned back
+     * to the buffer pool when released.
+     * <p>
+     * The default value of this property is <tt>true</tt> if and only if you
+     * allocated this buffer using {@link #allocate(int)} or {@link #allocate(int, boolean)},
+     * or <tt>false</tt> otherwise. (i.e. {@link #wrap(byte[])}, {@link #wrap(byte[],
int, int)},
+     * and {@link #wrap(java.nio.ByteBuffer)})
+     */
+    public abstract boolean isPooled();
+
+    /**
+     * Sets whether this buffer is returned back to the buffer pool when released.
+     * <p>
+     * The default value of this property is <tt>true</tt> if and only if you
+     * allocated this buffer using {@link #allocate(int)} or {@link #allocate(int, boolean)},
+     * or <tt>false</tt> otherwise. (i.e. {@link #wrap(byte[])}, {@link #wrap(byte[],
int, int)},
+     * and {@link #wrap(java.nio.ByteBuffer)})
+     */
+    public abstract void setPooled( boolean pooled );
+    
     public abstract int position();
 
     public abstract ByteBuffer position( int newPosition );
@@ -555,6 +556,7 @@
         private java.nio.ByteBuffer buf;
         private int refCount = 1;
         private boolean autoExpand;
+        private boolean pooled;
 
         protected DefaultByteBuffer()
         {
@@ -566,6 +568,7 @@
             buf.clear();
             buf.order( ByteOrder.BIG_ENDIAN );
             autoExpand = false;
+            pooled = true;
             refCount = 1;
         }
         
@@ -597,7 +600,11 @@
                 }
             }
 
-            release0( buf );
+            if( pooled )
+            {
+                release0( buf );
+            }
+
             synchronized( containerStack )
             {
                 containerStack.push( this );
@@ -628,6 +635,16 @@
         {
             this.autoExpand = autoExpand;
             return this;
+        }
+        
+        public boolean isPooled()
+        {
+            return pooled;
+        }
+        
+        public void setPooled( boolean pooled )
+        {
+            this.pooled = pooled;
         }
 
         public int capacity()

Modified: directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java (original)
+++ directory/network/trunk/src/java/org/apache/mina/common/ByteBufferProxy.java Tue Aug 16
20:00:08 2005
@@ -506,4 +506,14 @@
         buf.setAutoExpand( autoExpand );
         return this;
     }
+    
+    public boolean isPooled()
+    {
+        return buf.isPooled();
+    }
+    
+    public void setPooled( boolean pooled )
+    {
+        buf.setPooled( pooled );
+    }
 }

Modified: directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?rev=233110&r1=233109&r2=233110&view=diff
==============================================================================
--- directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Tue Aug 16
20:00:08 2005
@@ -147,6 +147,17 @@
         Assert.assertEquals( 4, buf.capacity() );
     }
     
+    public void testPooledProperty() throws Exception
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 16 );
+        java.nio.ByteBuffer nioBuf = buf.buf();
+        buf.release();
+        Assert.assertSame( nioBuf, ByteBuffer.allocate( 16 ).buf() );
+        buf.setPooled( false );
+        buf.release();
+        Assert.assertNotSame( nioBuf, ByteBuffer.allocate( 16 ).buf() );
+    }
+    
     public void testGetString() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 16 );



Mime
View raw message