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 java's garbage collection mechanism.

Author:
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 memory and initializes the supplied memory pointer class.
 JMemory allocateExclusive(int size)
          Allocates an exclusive block of native memory that once returned is not referenced by JMemoryPool.
static JBuffer buffer(int size)
          Allocates requested size of memory from the global memory pool.
static JMemoryPool defaultMemoryPool()
          Gets the global default memory pool.
 int duplicate(java.nio.ByteBuffer src, JMemory dst)
          Transfers contents from src to newly allocated memory and peers dst with that the new memory.
 int duplicate(JMemory src, JMemory dst)
          Transfers contents from src to newly allocated memory and peers dst with that the new memory.
 int duplicate2(JMemory src1, java.nio.ByteBuffer src2, JMemory dst1, JMemory dst2)
          Transfers contents from src1 and src2 to a contigues block of new memory, then peers dst1 and dst2 with the new memory, using the same sizes as src1 and src2 respectively.
 int duplicate2(JMemory src1, JMemory src2, JMemory dst1, JMemory dst2)
          Transfers contents from src1 and src2 to a contiguous block of new memory, then peers dst1 and dst2 with the new memory, using the same sizes as src1 and src2 respectively.
 JMemoryPool.Block getBlock(int size)
          Gets a block of memory that is big enough to hold at least size number of bytes.
 int getBlockSize()
          Gets the current default block size when creating new memory blocks.
static void malloc(int size, JMemory storage)
          Malloc.
 void setBlockSize(int blockSize)
          Sets the current default block size when creating new memory blocks.
static void shutdown()
          Shutdown.
 
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

buffer

public static JBuffer buffer(int size)
Allocates requested size of memory from the global memory pool.

Parameters:
size - allocation size in bytes
Returns:
buffer which references the allocated memory

malloc

public static void malloc(int size,
                          JMemory storage)
Malloc.

Parameters:
size - the size
storage - the storage

allocate

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

Parameters:
size - number of bytes
memory - memory pointer

allocateExclusive

public JMemory allocateExclusive(int size)
Allocates an exclusive block of native memory that once returned is not referenced by JMemoryPool.

Parameters:
size - amount of native memory to allocate in bytes
Returns:
object which is the owner of the allocated memory

duplicate

public int duplicate(JMemory src,
                     JMemory dst)
Transfers contents from src to newly allocated memory and peers dst with that the new memory. Any previously held resources by dst are freed.

Parameters:
src - source memory to copy from
dst - destination object to peer with new memory containing copy of memory pointed to by src
Returns:
number of bytes duplicated

duplicate2

public int duplicate2(JMemory src1,
                      JMemory src2,
                      JMemory dst1,
                      JMemory dst2)
Transfers contents from src1 and src2 to a contiguous block of new memory, then peers dst1 and dst2 with the new memory, using the same sizes as src1 and src2 respectively. This operation combines memory allocation, transferTo call on src1 and src2 and then peering of dst1 and dst2 with new memory in a single step.

Parameters:
src1 - first src for duplicate
src2 - second src for duplicate into the same memory
dst1 - peered with new memory using src1 length
dst2 - peered with same memory at src1.length offset using src2 length as length of peer
Returns:
total number of bytes duplicated

duplicate2

public int duplicate2(JMemory src1,
                      java.nio.ByteBuffer src2,
                      JMemory dst1,
                      JMemory dst2)
Transfers contents from src1 and src2 to a contigues block of new memory, then peers dst1 and dst2 with the new memory, using the same sizes as src1 and src2 respectively. This operation combines memory allocation, transferTo call on src1 and src2 and then peering of dst1 and dst2 with new memory in a single step.

Parameters:
src1 - first src for duplicate
src2 - second src for duplicate into the same memory
dst1 - peered with new memory using src1 length
dst2 - peered with same memory at src1.length offset using src2 length as length of peer
Returns:
total number of bytes duplicated

duplicate

public int duplicate(java.nio.ByteBuffer src,
                     JMemory dst)
Transfers contents from src to newly allocated memory and peers dst with that the new memory. Any previously held resources by dst are freed.

Parameters:
src - source memory to copy from
dst - destination object to peer with new memory containing copy of memory pointed to by src
Returns:
number of bytes duplicated

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

Parameters:
size - minimum available amount of memory in a block
Returns:
block big enough to hold size number of bytes 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. The pool of used blocks with potential of some available memory in them is maintained using a WeakReference. This allows the blocks to be GCed when no references to them exist, even if there is still a bit of available memory left in them.
See Also:
JMemoryPool.Block.allocate(int)

defaultMemoryPool

public static JMemoryPool defaultMemoryPool()
Gets the global default memory pool.

Returns:
the default pool

shutdown

public static void shutdown()
Shutdown.


getBlockSize

public int getBlockSize()
Gets the current default block size when creating new memory blocks.

Returns:
the blockSize

setBlockSize

public void setBlockSize(int blockSize)
Sets the current default block size when creating new memory blocks.

Parameters:
blockSize - the blockSize to set