2015/07/26 - Apache DirectMemory has been retired.

For more information, please explore the Attic.

public class

MergingByteBufferAllocator

extends AbstractByteBufferAllocator
java.lang.Object
   ↳ org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
     ↳ org.apache.directmemory.memory.allocator.MergingByteBufferAllocator

Class Overview

Allocator 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(long) and can be overridden.

Summary

[Expand]
Inherited Fields
From class org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
Public Constructors
MergingByteBufferAllocator(int number, int totalSize)
Constructor.
Public Methods
MemoryBuffer allocate(int size)
void clear()
void close()
void free(MemoryBuffer buffer)
int getCapacity()
void setMinSizeThreshold(int minSizeThreshold)
void setReturnNullWhenBufferIsFull(boolean returnNullWhenBufferIsFull)
void setSizeRatioThreshold(double sizeRatioThreshold)
Protected Methods
List<Integer> generateFreeSizesRange(long totalSize)
Generate free sizes' range.
void init()
Initialization function.
[Expand]
Inherited Methods
From class org.apache.directmemory.memory.allocator.AbstractByteBufferAllocator
From class java.lang.Object
From interface java.io.Closeable
From interface org.apache.directmemory.memory.allocator.Allocator

Public Constructors

public MergingByteBufferAllocator (int number, int totalSize)

Constructor.

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

Public Methods

public MemoryBuffer allocate (int size)

public void clear ()

public void close ()

Throws
IOException

public void free (MemoryBuffer buffer)

public int getCapacity ()

public void setMinSizeThreshold (int minSizeThreshold)

public void setReturnNullWhenBufferIsFull (boolean returnNullWhenBufferIsFull)

public void setSizeRatioThreshold (double sizeRatioThreshold)

Protected Methods

protected List<Integer> generateFreeSizesRange (long 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.

Returns
  • a list of all size's level used by the allocator.

protected void init ()

Initialization function. Create an initial free LinkedByteBuffer mapping the whole buffer.