org.jnetpcap.nio
Class JMemoryPool

java.lang.Object
  extended by org.jnetpcap.nio.JMemoryPool

public class JMemoryPool
extends java.lang.Object

Provides a mechanism for allocating memory to JMemory objects. This class is intended to be used when for example JPacket objects need to be kept around for longer periods of time than a single loop cycle. Since libpcap library utilizes a round-robin memory buffer for returning packet data buffers, this class provides a mechanism for copying that data into more permanent storage very efficiently.

The pool works by allocating a memory blocks which are given out to any JMemory class that requests a chunk. That memory is given out, out of the pool, until the block is completely exhausted, then a new block is allocated and continues to give out the memory. The memory blocks are released and deallocated when the last JMemory block that receive any of the memory is garbage collected. When that happens the original memory block is deallocated with a native C free() call. The user does not have to do anything special, the memory management is done completely behind the scene, very efficiently and automatically using.

Author:
Mark Bednarczyk, Sly Technologies, Inc.

Nested Class Summary
static class JMemoryPool.Block
          A block of native memory allocated with malloc.
 
Field Summary
static int DEFAULT_BLOCK_SIZE
          Default block size.
 
Constructor Summary
JMemoryPool()
          Uses default allocation size and strategy.
JMemoryPool(int defaultBlockSize)
          Allocates blocks in specified size
 
Method Summary
 void allocate(int size, JMemory memory)
          Allocates size bytes of memroy and initializes the supplied memory pointer class.
static JBuffer buffer(int size)
           
 JMemoryPool.Block getBlock(int size)
          Gets a block of memory that is big enough to hold at least size number of bytes.
static void malloc(int size, JMemory storage)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_BLOCK_SIZE

public static final int DEFAULT_BLOCK_SIZE
Default block size. JMemoryPool allocates memory in a large block which then further sub allocates per individual requests. The is the default size.

See Also:
Constant Field Values
Constructor Detail

JMemoryPool

public JMemoryPool()
Uses default allocation size and strategy.


JMemoryPool

public JMemoryPool(int defaultBlockSize)
Allocates blocks in specified size

Parameters:
defaultBlockSize - minimum memory block allocation size
Method Detail

getBlock

public JMemoryPool.Block getBlock(int size)
Gets a block of memory that is big enough to hold at least size number of bytes. The user must further request from the block JMemoryPool.Block.allocate(int) the size of memory needed. The block will then return an offset into the memory which has been reserved for this allocation

Parameters:
size - minimum available amount of memory in a block
Returns:
block big enough to hold size number of bytes
See Also:
JMemoryPool.Block.allocate(int)

allocate

public void allocate(int size,
                     JMemory memory)
Allocates size bytes of memroy and initializes the supplied memory pointer class.

Parameters:
size - number of bytes
memory - memory pointer

malloc

public static void malloc(int size,
                          JMemory storage)
Parameters:
size -
storage -

buffer

public static JBuffer buffer(int size)
Parameters:
size -
storage -