org.apache.directmemory.memory.allocator
Class MergingByteBufferAllocatorImpl

java.lang.Object
  extended by org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
      extended by org.apache.directmemory.memory.allocator.MergingByteBufferAllocatorImpl
All Implemented Interfaces:
Closeable, ByteBufferAllocator

public class MergingByteBufferAllocatorImpl
extends AbstractByteBufferAllocator

ByteBufferAllocator implementation with ByteBuffer merging capabilities.

ByteBuffers are wrapped into an LinkedByteBuffer, and when a ByteBuffer is freed, lookup is done to the neighbor to check if they are also free, in which case they are merged.

setMinSizeThreshold(int) gives the minimum buffer's size under which no splitting is done. setSizeRatioThreshold(double) gives the size ratio (requested allocation / free buffer's size} under which no splitting is done

The free ByteBuffer are held into a NavigableMap with keys defining the size's range : 0 -> first key (included), first key -> second key (included), ... Instead of keeping a list of ByteBuffers sorted by capacity, ByteBuffers in the same size's range are held in the same collection. The size's range are computed by generateFreeSizesRange(Integer) and can be overridden.

Since:
0.6

Field Summary
 
Fields inherited from class org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
logger
 
Constructor Summary
MergingByteBufferAllocatorImpl(int number, int totalSize)
          Constructor.
 
Method Summary
 ByteBuffer allocate(int size)
          Allocates and returns a ByteBuffer with Buffer.limit() set to the given size.
 void clear()
          Clear all allocated ByteBuffer, resulting in a empty and ready to deserve ByteBufferAllocator
 void close()
           
 void free(ByteBuffer buffer)
          Returns the given ByteBuffer making it available for a future usage.
protected  List<Integer> generateFreeSizesRange(Integer totalSize)
          Generate free sizes' range.
 int getCapacity()
           
protected  void init()
          Initialization function.
 void setMinSizeThreshold(int minSizeThreshold)
           
 void setReturnNullWhenBufferIsFull(boolean returnNullWhenBufferIsFull)
           
 void setSizeRatioThreshold(double sizeRatioThreshold)
           
 
Methods inherited from class org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
getHash, getLogger, getNumber, isClosed, setClosed
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

MergingByteBufferAllocatorImpl

public MergingByteBufferAllocatorImpl(int number,
                                      int totalSize)
Constructor.

Parameters:
number - : the internal buffer identifier
totalSize - : total size of the parent buffer.
Method Detail

init

protected void init()
Initialization function. Create an initial free Pointer mapping the whole buffer.


generateFreeSizesRange

protected List<Integer> generateFreeSizesRange(Integer totalSize)
Generate free sizes' range. Sizes' range are used to try to allocate the best matching regarding the requested size. Instead of using a sorted structure, arbitrary size's range are computed.

Parameters:
totalSize -
Returns:
a list of all size's level used by the allocator.

free

public void free(ByteBuffer buffer)
Description copied from interface: ByteBufferAllocator
Returns the given ByteBuffer making it available for a future usage. Returning twice a ByteBuffer won't throw an exception.

Parameters:
buffer - : the ByteBuffer to return

allocate

public ByteBuffer allocate(int size)
Description copied from interface: ByteBufferAllocator
Allocates and returns a ByteBuffer with Buffer.limit() set to the given size. When the allocation fails, it returns either null or throws an BufferOverflowException, depending on the implementation.

Parameters:
size - : the size in byte to allocate
Returns:
a ByteBuffer of the given size, or either return null or throw an BufferOverflowException when the allocation fails.

clear

public void clear()
Description copied from interface: ByteBufferAllocator
Clear all allocated ByteBuffer, resulting in a empty and ready to deserve ByteBufferAllocator


setSizeRatioThreshold

public void setSizeRatioThreshold(double sizeRatioThreshold)

setMinSizeThreshold

public void setMinSizeThreshold(int minSizeThreshold)

setReturnNullWhenBufferIsFull

public void setReturnNullWhenBufferIsFull(boolean returnNullWhenBufferIsFull)

getCapacity

public int getCapacity()
Returns:
the internal total size that can be allocated

close

public void close()
           throws IOException
Throws:
IOException


Copyright © 2011-2012 The Apache Software Foundation. All Rights Reserved.