directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trus...@apache.org
Subject svn commit: r157370 - in directory/network/mina/trunk/src: java/org/apache/mina/common/ java/org/apache/mina/io/datagram/ java/org/apache/mina/io/filter/ java/org/apache/mina/io/socket/ java/org/apache/mina/protocol/codec/ java/org/apache/mina/protocol/io/ java/org/apache/mina/util/ test/org/apache/mina/common/
Date Mon, 14 Mar 2005 03:11:41 GMT
Author: trustin
Date: Sun Mar 13 19:11:37 2005
New Revision: 157370

URL: http://svn.apache.org/viewcvs?view=rev&rev=157370
Log:
* Changed static ByteBuffer.release() method to instance method.
* Added ByteBuffer.acquire() method to let users have control of releasing buffers.
* IoHandlerFilterManager and IoThreadPoolFilter utilizes new acquire() and release() now.

Modified:
    directory/network/mina/trunk/src/java/org/apache/mina/common/ByteBuffer.java
    directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
    directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
    directory/network/mina/trunk/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
    directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
    directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketSession.java
    directory/network/mina/trunk/src/java/org/apache/mina/protocol/codec/NettyEncoder.java
    directory/network/mina/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java
    directory/network/mina/trunk/src/java/org/apache/mina/util/IoHandlerFilterManager.java
    directory/network/mina/trunk/src/test/org/apache/mina/common/ByteBufferTest.java

Modified: directory/network/mina/trunk/src/java/org/apache/mina/common/ByteBuffer.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/common/ByteBuffer.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/common/ByteBuffer.java (original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/common/ByteBuffer.java Sun Mar 13
19:11:37 2005
@@ -98,43 +98,14 @@
         }
 
         buf.clear();
-        // Check leaked or dangling ByteBuffer.
-        if( buf.inUse )
+        synchronized( buf )
         {
-            throw new IllegalStateException(
-                    "Already allocated buffer. Did you release the buffer more than once?"
);
-        }
-        else
-        {
-            buf.inUse = true;
+            buf.refCount = 1;
         }
 
         return buf;
     }
 
-    /**
-     * Returns the specified buffer to buffer pool.
-     */
-    public static void release( ByteBuffer buf )
-    {
-        Stack stack = bufferStacks[ getBufferStackIndex( buf.capacity() ) ];
-        synchronized( stack )
-        {
-            // Check leaked or dangling ByteBuffer.
-            if( !buf.inUse )
-            {
-                throw new IllegalStateException(
-                        "Already released buffer.  Did you release the buffer more than once?"
);
-            }
-            else
-            {
-                buf.inUse = false; // clear the flag
-            }
-
-            stack.push( buf );
-        }
-    }
-
     private static int getBufferStackIndex( int size )
     {
         int targetSize = 16;
@@ -155,11 +126,58 @@
 
     private final java.nio.ByteBuffer buf;
 
-    private boolean inUse;
+    private int refCount = 1;
 
     private ByteBuffer( java.nio.ByteBuffer buf )
     {
         this.buf = buf;
+    }
+    
+    /**
+     * Increases the internal reference count of this buffer to defer
+     * automatic release.  You have to invoke {@link #release()} as many
+     * as you invoked this method to release this buffer.
+     * 
+     * @throws IllegalStateException if you attempt to acquire already
+     *                               released buffer.
+     */
+    public synchronized void acquire()
+    {
+        if( refCount <= 0 )
+        {
+            throw new IllegalStateException( "Already released buffer." );
+        }
+
+        refCount ++;
+    }
+
+    /**
+     * Releases the specified buffer to buffer pool.
+     * 
+     * @throws IllegalStateException if you attempt to release already
+     *                               released buffer.
+     */
+    public synchronized void release()
+    {
+        if( refCount <= 0 )
+        {
+            refCount = 0;
+            throw new IllegalStateException(
+                    "Already released buffer.  You released the buffer too many times." );
+        }
+
+        refCount --;
+        if( refCount > 0)
+        {
+            return;
+        }
+
+        Stack stack = bufferStacks[ getBufferStackIndex( buf.capacity() ) ];
+        synchronized( stack )
+        {
+            // push back
+            stack.push( this );
+        }
     }
 
     /**

Modified: directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramAcceptor.java
Sun Mar 13 19:11:37 2005
@@ -282,7 +282,7 @@
         }
         finally
         {
-            ByteBuffer.release( readBuf );
+            readBuf.release();
         }
     }
 
@@ -345,7 +345,7 @@
 
                 try
                 {
-                    ByteBuffer.release( buf );
+                    buf.release();
                 }
                 catch( IllegalStateException e )
                 {

Modified: directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/io/datagram/DatagramConnector.java
Sun Mar 13 19:11:37 2005
@@ -259,7 +259,7 @@
         }
         finally
         {
-            ByteBuffer.release( readBuf );
+            readBuf.release();
         }
     }
 
@@ -322,7 +322,7 @@
 
                 try
                 {
-                    ByteBuffer.release( buf );
+                    buf.release();
                 }
                 catch( IllegalStateException e )
                 {

Modified: directory/network/mina/trunk/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/io/filter/IoThreadPoolFilter.java
Sun Mar 13 19:11:37 2005
@@ -222,6 +222,8 @@
     public void dataRead( IoHandler nextHandler, IoSession session,
                          ByteBuffer buf )
     {
+        // MINA will release the buffer if this method returns.
+        buf.acquire();
         fireEvent( nextHandler, session, EventType.READ, buf );
     }
 
@@ -421,7 +423,9 @@
         {
             if( type == EventType.READ )
             {
-                nextHandler.dataRead( session, ( ByteBuffer ) data );
+                ByteBuffer buf = ( ByteBuffer ) data;
+                nextHandler.dataRead( session, buf );
+                buf.release();
             }
             else if( type == EventType.WRITTEN )
             {

Modified: directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketIoProcessor.java
Sun Mar 13 19:11:37 2005
@@ -420,7 +420,7 @@
                 }
                 try
                 {
-                    ByteBuffer.release( buf );
+                    buf.release();
                 }
                 catch( IllegalStateException e )
                 {

Modified: directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketSession.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketSession.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketSession.java (original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/io/socket/SocketSession.java Sun
Mar 13 19:11:37 2005
@@ -126,7 +126,7 @@
             return;
         }
         disposed = true;
-        ByteBuffer.release( readBuf );
+        readBuf.release();
     }
 
     public IoHandler getHandler()

Modified: directory/network/mina/trunk/src/java/org/apache/mina/protocol/codec/NettyEncoder.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/protocol/codec/NettyEncoder.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/protocol/codec/NettyEncoder.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/protocol/codec/NettyEncoder.java
Sun Mar 13 19:11:37 2005
@@ -56,7 +56,7 @@
                 }
                 else
                 {
-                    ByteBuffer.release( buf );
+                    buf.release();
                 }
             }
         }

Modified: directory/network/mina/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java (original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/protocol/io/IoAdapter.java Sun Mar
13 19:11:37 2005
@@ -55,8 +55,6 @@
  */
 public class IoAdapter
 {
-    public static final Class IO_HANDLER_TYPE = SessionHandlerAdapter.class;
-
     private final ProtocolHandlerFilterManager filterManager = new ProtocolHandlerFilterManager();
 
     IoAdapter()
@@ -193,7 +191,7 @@
             {
                 try
                 {
-                    ByteBuffer.release( in );
+                    in.release();
                 }
                 catch( IllegalStateException e )
                 {
@@ -454,7 +452,7 @@
                 }
 
                 newBuf.put( buf );
-                ByteBuffer.release( buf );
+                buf.release();
             }
             
             // Push the new buffer finally.

Modified: directory/network/mina/trunk/src/java/org/apache/mina/util/IoHandlerFilterManager.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/java/org/apache/mina/util/IoHandlerFilterManager.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/java/org/apache/mina/util/IoHandlerFilterManager.java
(original)
+++ directory/network/mina/trunk/src/java/org/apache/mina/util/IoHandlerFilterManager.java
Sun Mar 13 19:11:37 2005
@@ -26,7 +26,6 @@
 import org.apache.mina.io.IoHandler;
 import org.apache.mina.io.IoHandlerFilter;
 import org.apache.mina.io.IoSession;
-import org.apache.mina.protocol.io.IoAdapter;
 
 /**
  * Manages the list of {@link IoHandlerFilter}s.
@@ -66,11 +65,7 @@
         {
             IoHandler handler = session.getHandler();
             handler.dataRead( session, buf );
-            if( !IoAdapter.IO_HANDLER_TYPE.isAssignableFrom( handler
-                    .getClass() ) )
-            {
-                ByteBuffer.release( buf );
-            }
+            buf.release();
         }
 
         public void dataWritten( IoHandler nextHandler, IoSession session,
@@ -259,7 +254,7 @@
             {
                 // Original buffer is replaced with new filtered buffer;
                 // let's release the old one.
-                ByteBuffer.release( buf );
+                buf.release();
             }
             else if( newBuf == null )
             {

Modified: directory/network/mina/trunk/src/test/org/apache/mina/common/ByteBufferTest.java
URL: http://svn.apache.org/viewcvs/directory/network/mina/trunk/src/test/org/apache/mina/common/ByteBufferTest.java?view=diff&r1=157369&r2=157370
==============================================================================
--- directory/network/mina/trunk/src/test/org/apache/mina/common/ByteBufferTest.java (original)
+++ directory/network/mina/trunk/src/test/org/apache/mina/common/ByteBufferTest.java Sun Mar
13 19:11:37 2005
@@ -49,22 +49,42 @@
             Assert.assertEquals( buf.capacity(), buf.remaining() );
             Assert.assertTrue( buf.capacity() >= i );
             Assert.assertTrue( buf.capacity() < i * 2 );
-            ByteBuffer.release( buf );
+            buf.release();
         }
     }
 
     public void testLeakageDetection() throws Exception
     {
         ByteBuffer buf = ByteBuffer.allocate( 1024 );
-        ByteBuffer.release( buf );
+        buf.release();
         try
         {
-            ByteBuffer.release( buf );
+            buf.release();
             Assert.fail( "Releasing a buffer twice should fail." );
         }
         catch( IllegalStateException e )
         {
 
+        }
+    }
+    
+    public void testAcquireRelease() throws Exception
+    {
+        ByteBuffer buf = ByteBuffer.allocate( 1024 );
+        buf.acquire();
+        buf.release();
+        buf.acquire();
+        buf.acquire();
+        buf.release();
+        buf.release();
+        buf.release();
+        try
+        {
+            buf.release();
+            Assert.fail( "Releasing a buffer twice should fail." );
+        }
+        catch( IllegalStateException e )
+        {
         }
     }
 }



Mime
View raw message