Author: bperroud
Date: Fri Mar  2 08:50:06 2012
New Revision: 1296090

URL: http://svn.apache.org/viewvc?rev=1296090&view=rev
Log:
DIRECTMEMORY-40, DIRECTMEMORY-70 : add javadoc and comments, put logger in 
abstract, add feature to close an allocator and release all the held buffers.

Modified:
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/AbstractByteBufferAllocator.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/ByteBufferAllocator.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/DirectByteBufferUtils.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImpl.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImpl.java
    
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImpl.java

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/MemoryManagerServiceImpl.java
 Fri Mar  2 08:50:06 2012
@@ -90,8 +90,8 @@ public class MemoryManagerServiceImpl<V>
     {
         final MergingByteBufferAllocatorImpl allocator = new 
MergingByteBufferAllocatorImpl( allocatorNumber, size );
         
-        // Hack to ensure the pointers are always splitted as it was the case 
before.
-        allocator.setMinSizeThreshold( 0.0 );
+        // Hack to ensure the pointers are always split to keep backward 
compatibility.
+        allocator.setMinSizeThreshold( 0 );
         allocator.setSizeRatioThreshold( 1.0 );
         
         return allocator;

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/AbstractByteBufferAllocator.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/AbstractByteBufferAllocator.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/AbstractByteBufferAllocator.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/AbstractByteBufferAllocator.java
 Fri Mar  2 08:50:06 2012
@@ -1,5 +1,10 @@
 package org.apache.directmemory.memory.allocator;
 
+import java.util.concurrent.atomic.AtomicBoolean;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
 /*
  * Licensed to the Apache Software Foundation (ASF) under one
  * or more contributor license agreements. See the NOTICE file
@@ -23,8 +28,12 @@ public abstract class AbstractByteBuffer
     implements ByteBufferAllocator
 {
 
+    protected final Logger logger = LoggerFactory.getLogger( this.getClass() );
+
     private final int number;
     
+    private final AtomicBoolean closed = new AtomicBoolean( false );
+    
     AbstractByteBufferAllocator( final int number )
     {
         this.number = number;
@@ -36,4 +45,19 @@ public abstract class AbstractByteBuffer
         return number;
     }
 
+    protected Logger getLogger()
+    {
+        return logger;
+    }
+    
+    protected boolean isClosed()
+    {
+        return closed.get();
+    }
+    
+    protected void setClosed( final boolean closed )
+    {
+        this.closed.set( closed );
+    }
+    
 }

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/ByteBufferAllocator.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/ByteBufferAllocator.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/ByteBufferAllocator.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/ByteBufferAllocator.java
 Fri Mar  2 08:50:06 2012
@@ -23,31 +23,41 @@ import java.io.Closeable;
 import java.nio.ByteBuffer;
 
 /**
+ * Interface defining interaction with {@link ByteBuffer}
  * 
- * 
- *
+ * @since 0.6
  */
 public interface ByteBufferAllocator
     extends Closeable
 {
     
     /**
-     * Return the given ByteBuffer making it available for a future usage 
-     * @param buffer
+     * Returns the given {@link ByteBuffer} making it available for a future 
usage. Returning twice a {@link ByteBuffer} won't throw an exception. 
+     * @param buffer : the {@link ByteBuffer} to return
      */
     void free( final ByteBuffer buffer );
     
     /**
-     * Allocate the given size off heap, or return null if the allocation 
failed.
-     * @param size
-     * @return
+     * Allocates and returns a {@link ByteBuffer} with {@link 
ByteBuffer#limit()} set to the given size.
+     * When the allocation fails, it returns either null or throws an {@link 
BufferOverflowException}, depending on the implementation. 
+     * @param size : the size in byte to allocate
+     * @return a {@link ByteBuffer} of the given size, or either return null 
or throw an {@link BufferOverflowException} when the allocation fails.
      */
     ByteBuffer allocate( final int size );
     
+    /**
+     * Clear all allocated {@link ByteBuffer}, resulting in a empty and ready 
to deserve {@link ByteBufferAllocator}
+     */
     void clear();
     
+    /**
+     * @return the internal total size that can be allocated 
+     */
     int getCapacity();
     
+    /**
+     * @return the internal identifier of the {@link ByteBufferAllocator}
+     */
     int getNumber();
     
 }

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/DirectByteBufferUtils.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/DirectByteBufferUtils.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/DirectByteBufferUtils.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/DirectByteBufferUtils.java
 Fri Mar  2 08:50:06 2012
@@ -1,14 +1,47 @@
 package org.apache.directmemory.memory.allocator;
 
+/*
+ * 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.
+ */
+
 import java.lang.reflect.InvocationTargetException;
 import java.lang.reflect.Method;
 import java.nio.ByteBuffer;
 
 import static com.google.common.base.Preconditions.checkArgument;
 
+/**
+ * Utility class around direct {@link ByteBuffer} 
+ *
+ */
 public class DirectByteBufferUtils
 {
 
+    /**
+     * Clear and release the internal content of a direct {@link ByteBuffer}.
+     * Clearing manually the content avoid waiting till the GC do his job.
+     * @param buffer : the buffer to clear
+     * @throws IllegalArgumentException
+     * @throws IllegalAccessException
+     * @throws InvocationTargetException
+     * @throws SecurityException
+     * @throws NoSuchMethodException
+     */
     public static void destroyDirectByteBuffer( final ByteBuffer buffer )
         throws IllegalArgumentException, IllegalAccessException, 
InvocationTargetException, SecurityException,
         NoSuchMethodException

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImpl.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImpl.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/FixedSizeByteBufferAllocatorImpl.java
 Fri Mar  2 08:50:06 2012
@@ -28,27 +28,23 @@ import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
 import java.util.concurrent.ConcurrentLinkedQueue;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.common.base.Preconditions;
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
 
 /**
  * {@link ByteBufferAllocator} implementation that instantiate {@link 
ByteBuffer}s of fixed size, called slices.
  * 
- * @author bperroud
+ * @since 0.6
  * 
  */
 public class FixedSizeByteBufferAllocatorImpl
     extends AbstractByteBufferAllocator
 {
 
-    protected static Logger logger = LoggerFactory.getLogger( 
FixedSizeByteBufferAllocatorImpl.class );
-
     // Collection that keeps track of the parent buffers (segments) where 
slices are allocated
     private final Set<ByteBuffer> segmentsBuffers = new HashSet<ByteBuffer>();
 
-    // Collection that owns all slice that can be used.
+    // Collection that owns all slices that can be used.
     private final Queue<ByteBuffer> freeBuffers = new 
ConcurrentLinkedQueue<ByteBuffer>();
 
     // Size of each slices dividing each segments of the slab
@@ -60,7 +56,7 @@ public class FixedSizeByteBufferAllocato
     // Tells if one need to keep track of borrowed buffers
     private boolean keepTrackOfUsedSliceBuffers = false;
     
-    // Tells if it returns null or throw an BufferOverflowExcpetion with the 
requested size is bigger than the size of the slices
+    // Tells if it returns null or throw an BufferOverflowException when the 
requested size is bigger than the size of the slices
     private boolean returnNullWhenOversizingSliceSize = true;
     
     // Tells if it returns null when no buffers are available
@@ -70,16 +66,13 @@ public class FixedSizeByteBufferAllocato
     private final Set<ByteBuffer> usedSliceBuffers = Collections
         .newSetFromMap( new ConcurrentHashMap<ByteBuffer, Boolean>() );
 
-    protected Logger getLogger()
-    {
-        return logger;
-    }
-
+    
     /**
      * Constructor.
+     * @param number : internal identifier of the allocator
      * @param totalSize : the internal buffer
      * @param sliceSize : arbitrary number of the buffer.
-     * @param numberOfSegments : 
+     * @param numberOfSegments : number of parent {@link ByteBuffer} to 
allocate.
      */
     FixedSizeByteBufferAllocatorImpl( final int number, final int totalSize, 
final int sliceSize, final int numberOfSegments )
     {
@@ -94,6 +87,8 @@ public class FixedSizeByteBufferAllocato
 
     protected void init( final int numberOfSegments )
     {
+        checkArgument( numberOfSegments > 0 );
+        
         // Compute the size of each segments
         int segmentSize = totalSize / numberOfSegments;
         // size is rounded down to a multiple of the slice size
@@ -118,6 +113,7 @@ public class FixedSizeByteBufferAllocato
 
     protected ByteBuffer findFreeBuffer( int capacity )
     {
+        // ensure the requested size is not bigger than the slices' size
         if ( capacity > sliceSize )
         {
             if (returnNullWhenOversizingSliceSize)
@@ -137,12 +133,15 @@ public class FixedSizeByteBufferAllocato
     public void free( final ByteBuffer byteBuffer )
     {
 
+        checkState( !isClosed() );
+        
         if ( keepTrackOfUsedSliceBuffers && !usedSliceBuffers.remove( 
byteBuffer ) )
         {
             return;
         }
 
-        Preconditions.checkArgument( byteBuffer.capacity() == sliceSize );
+        // Ensure the buffer belongs to this slab
+        checkArgument( byteBuffer.capacity() == sliceSize );
 
         freeBuffers.offer( byteBuffer );
 
@@ -152,6 +151,8 @@ public class FixedSizeByteBufferAllocato
     public ByteBuffer allocate( int size )
     {
 
+        checkState( !isClosed() );
+        
         ByteBuffer allocatedByteBuffer = findFreeBuffer( size );
 
         if ( allocatedByteBuffer == null )
@@ -166,6 +167,7 @@ public class FixedSizeByteBufferAllocato
             }
         }
 
+        // Reset buffer's state
         allocatedByteBuffer.clear();
         allocatedByteBuffer.limit( size );
 
@@ -186,7 +188,10 @@ public class FixedSizeByteBufferAllocato
     @Override
     public void clear()
     {
-        // Nothing to do.
+        if ( keepTrackOfUsedSliceBuffers )
+        {
+            usedSliceBuffers.clear();
+        }
     }
 
     @Override
@@ -198,6 +203,10 @@ public class FixedSizeByteBufferAllocato
     @Override
     public void close()
     {
+        checkState( !isClosed() );
+        
+        setClosed( true );
+        
         clear();
         
         for ( final ByteBuffer buffer : segmentsBuffers )
@@ -208,7 +217,7 @@ public class FixedSizeByteBufferAllocato
             }
             catch (Exception e)
             {
-                
+                getLogger().warn( "Exception thrown while closing the 
allocator", e );
             }
         }
     }

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImpl.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImpl.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/MergingByteBufferAllocatorImpl.java
 Fri Mar  2 08:50:06 2012
@@ -35,17 +35,20 @@ import java.util.concurrent.ConcurrentSk
 import java.util.concurrent.locks.Lock;
 import java.util.concurrent.locks.ReentrantLock;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-
 /**
- * {@link ByteBufferAllocator} implementation with {@link ByteBuffer} merging 
capabilities
- *
- * {@link TreeMap} can be safely used because synchronization is achieved 
through a {@link Lock}
- *
- * @author bperroud
- *
+ * {@link ByteBufferAllocator} implementation with {@link ByteBuffer} merging 
capabilities. 
+ * 
+ * {@link ByteBuffer}s are wrapped into an {@link LinkedByteBuffer}, and when 
a {@link ByteBuffer} is freed,
+ * lookup is done to the neighbor to check if they are also free, in which 
case they are merged.
+ * 
+ * {@link #setMinSizeThreshold(double)} gives the minimum buffer's size under 
which no splitting is done.
+ * {@link #setSizeRatioThreshold(double)} gives the size ratio (requested 
allocation / free buffer's size} under which no splitting is done
+ * 
+ * The free {@link ByteBuffer} are held into a {@link NavigableMap} with keys 
defining the size's range : 0 -> first key (included), first key -> second key 
(included), ...
+ * Instead of keeping a list of {@link ByteBuffer}s sorted by capacity, {@link 
ByteBuffer}s in the same size's range are held in the same collection.
+ * The size's range are computed by {@link #generateFreeSizesRange(Integer)} 
and can be overridden. 
+ * 
+ * @since 0.6
  */
 public class MergingByteBufferAllocatorImpl
     extends AbstractByteBufferAllocator
@@ -55,36 +58,31 @@ public class MergingByteBufferAllocatorI
 
     private static final int DEFAULT_MIN_SIZE_THRESHOLD = 128;
     
-    protected static Logger logger = LoggerFactory.getLogger( 
MergingByteBufferAllocatorImpl.class );
-
-    // List of free pointers, with several list of different size
+    // List of free pointers, with several lists of different size
     private final NavigableMap<Integer, Collection<LinkedByteBuffer>> 
freePointers = new ConcurrentSkipListMap<Integer, 
Collection<LinkedByteBuffer>>();
 
-    // Set of used pointers. The key is a hash of ByteBuffer.
+    // Set of used pointers. The key is #getHash(ByteBuffer).
     private final Map<Integer, LinkedByteBuffer> usedPointers = new 
ConcurrentHashMap<Integer, LinkedByteBuffer>();
 
     // Lock used instead of synchronized block to guarantee consistency when 
manipulating list of pointers.
-    private final Lock pointerManipulationLock = new ReentrantLock();
+    private final Lock linkedStructureManipulationLock = new ReentrantLock();
 
+    // The initial buffer, from which all the others are sliced
     private final ByteBuffer parentBuffer;
 
-    // Allowed size ratio of the returned pointer before splitting the pointer
+    // Allowed size ratio (requested size / buffer's size) of the returned 
buffer before splitting
     private double sizeRatioThreshold = DEFAULT_SIZE_RATIO_THRESHOLD;
     
-    //
-    private double minSizeThreshold = DEFAULT_MIN_SIZE_THRESHOLD;
+    // Min size of the returned buffer before splitting
+    private int minSizeThreshold = DEFAULT_MIN_SIZE_THRESHOLD;
 
+    // Tells if null is returned or an BufferOverflowException is thrown when 
the buffer is full
     private boolean returnNullWhenBufferIsFull = true;
     
-    protected Logger getLogger()
-    {
-        return logger;
-    }
-
     /**
      * Constructor.
-     * @param buffer : the internal buffer
-     * @param bufferNumber : arbitrary number of the buffer.
+     * @param number : the internal buffer identifier
+     * @param totalSize : total size of the parent buffer.
      */
     public MergingByteBufferAllocatorImpl( final int number, final int 
totalSize )
     {
@@ -101,7 +99,7 @@ public class MergingByteBufferAllocatorI
     {
         Integer totalSize = Integer.valueOf( parentBuffer.capacity() );
 
-        for ( Integer i : generateQueueSizes( totalSize ) )
+        for ( Integer i : generateFreeSizesRange( totalSize ) )
         {
             freePointers.put( Integer.valueOf( i ), new 
LinkedHashSet<LinkedByteBuffer>() );
         }
@@ -110,6 +108,9 @@ public class MergingByteBufferAllocatorI
 
     }
 
+    /**
+     * Create the first {@link LinkedByteBuffer}
+     */
     private void initFirstBuffer()
     {
         parentBuffer.clear();
@@ -120,16 +121,23 @@ public class MergingByteBufferAllocatorI
     }
     
     
-    protected List<Integer> generateQueueSizes( final Integer totalSize )
+    /**
+     * Generate free sizes' range. Sizes' range are used to try to allocate 
the best matching {@ByteBuffer} 
+     * regarding the requested size. Instead of using a sorted structure, 
arbitrary size's range are computed.
+     * 
+     * @param totalSize
+     * @return a list of all size's level used by the allocator.
+     */
+    protected List<Integer> generateFreeSizesRange( final Integer totalSize )
     {
         List<Integer> sizes = new ArrayList<Integer>();
 
-        for ( int i = 128; i <= totalSize; i *= 8 )
+        for ( int i = minSizeThreshold; i <= totalSize; i *= 8 )
         {
             sizes.add( Integer.valueOf( i ) );
         }
 
-        // If totalSize < 128 or totalSize is not a multiple of 128 
+        // If totalSize < minSizeThreshold or totalSize is not a multiple of 
minSizeThreshold 
         // we force adding an element to the map
         if ( sizes.isEmpty() || !sizes.contains( totalSize ) )
         {
@@ -153,7 +161,7 @@ public class MergingByteBufferAllocatorI
 
         try
         {
-            pointerManipulationLock.lock();
+            linkedStructureManipulationLock.lock();
 
             if ( returningLinkedBuffer.getBefore() != null )
             {
@@ -177,7 +185,7 @@ public class MergingByteBufferAllocatorI
         }
         finally
         {
-            pointerManipulationLock.unlock();
+            linkedStructureManipulationLock.unlock();
         }
     }
 
@@ -187,7 +195,7 @@ public class MergingByteBufferAllocatorI
 
         try
         {
-            pointerManipulationLock.lock();
+            linkedStructureManipulationLock.lock();
 
             final SortedMap<Integer, Collection<LinkedByteBuffer>> freeMap = 
freePointers
                 .tailMap( size - 1 );
@@ -270,7 +278,7 @@ public class MergingByteBufferAllocatorI
         }
         finally
         {
-            pointerManipulationLock.unlock();
+            linkedStructureManipulationLock.unlock();
         }
     }
 
@@ -336,7 +344,7 @@ public class MergingByteBufferAllocatorI
         this.sizeRatioThreshold = sizeRatioThreshold;
     }
     
-    public void setMinSizeThreshold( final double minSizeThreshold )
+    public void setMinSizeThreshold( final int minSizeThreshold )
     {
         this.minSizeThreshold = minSizeThreshold;
     }

Modified: 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImpl.java
URL: 
http://svn.apache.org/viewvc/incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImpl.java?rev=1296090&r1=1296089&r2=1296090&view=diff
==============================================================================
--- 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImpl.java
 (original)
+++ 
incubator/directmemory/trunk/directmemory-cache/src/main/java/org/apache/directmemory/memory/allocator/SlabByteBufferAllocatorImpl.java
 Fri Mar  2 08:50:06 2012
@@ -27,42 +27,34 @@ import java.util.Map;
 import java.util.NavigableMap;
 import java.util.concurrent.ConcurrentSkipListMap;
 
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
 
 /**
- * {@link ByteBufferAllocator} implementation that instantiate uses {@link 
FixedSizeByteBufferAllocatorImpl} of different size to allocate best matching's 
size {@link ByteBuffer}
+ * {@link ByteBufferAllocator} implementation that uses {@link 
FixedSizeByteBufferAllocatorImpl} 
+ * of different size to allocate best matching's size {@link ByteBuffer}
  * 
- * @author bperroud
+ * @since 0.6
  * 
  */
 public class SlabByteBufferAllocatorImpl
     extends AbstractByteBufferAllocator
 {
 
-    protected static Logger logger = LoggerFactory.getLogger( 
SlabByteBufferAllocatorImpl.class );
-
     // Tells if it returns null when no buffers are available
     private boolean returnNullWhenNoBufferAvailable = true;
     
     // Internal slabs sorted by sliceSize
     private final NavigableMap<Integer, FixedSizeByteBufferAllocatorImpl> 
slabs = new ConcurrentSkipListMap<Integer, FixedSizeByteBufferAllocatorImpl>();
     
+    // Tells if it is allowed to look in a bigger slab to perform the request.
     private final boolean allowAllocationToBiggerSlab;
 
-    protected Logger getLogger()
-    {
-        return logger;
-    }
-
     /**
      * Constructor.
-     * @param totalSize : the internal buffer
-     * @param sliceSize : arbitrary number of the buffer.
-     * @param numberOfSegments : 
+     * @param number : internal allocator identifier
+     * @param slabs : {@link FixedSizeByteBufferAllocatorImpl} to use for 
allocation
+     * @param allowAllocationToBiggerSlab : tells if it is allowed to look in 
a bigger slab to perform the request.
      */
-    SlabByteBufferAllocatorImpl( final int number, final 
Collection<FixedSizeByteBufferAllocatorImpl> slabs, final boolean 
allowAllocationToBiggerSlab )
+    public SlabByteBufferAllocatorImpl( final int number, final 
Collection<FixedSizeByteBufferAllocatorImpl> slabs, final boolean 
allowAllocationToBiggerSlab )
     {
         super( number );
         
@@ -76,6 +68,10 @@ public class SlabByteBufferAllocatorImpl
     }
     
 
+    /**
+     * @param size
+     * @return the slab that best match the given size
+     */
     private FixedSizeByteBufferAllocatorImpl getSlabThatMatchTheSize( final 
int size )
     {
         // Find the slab that can carry the wanted size. -1 is used because 
higherEntry returns a strictly higher entry.
@@ -109,7 +105,6 @@ public class SlabByteBufferAllocatorImpl
     @Override
     public ByteBuffer allocate( final int size )
     {
-
         
         final FixedSizeByteBufferAllocatorImpl slab = getSlabThatMatchTheSize( 
size );
 
@@ -189,7 +184,10 @@ public class SlabByteBufferAllocatorImpl
     @Override
     public void clear()
     {
-        // Nothing to do.
+        for (final Map.Entry<Integer, FixedSizeByteBufferAllocatorImpl> entry 
: slabs.entrySet())
+        {
+            entry.getValue().clear();
+        }
     }
 
     @Override


Reply via email to