|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectorg.jnetpcap.nio.JMemoryPool
public class JMemoryPool
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.
| 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 |
|---|
public static final int DEFAULT_BLOCK_SIZE
| Constructor Detail |
|---|
public JMemoryPool()
public JMemoryPool(int defaultBlockSize)
defaultBlockSize - minimum memory block allocation size| Method Detail |
|---|
public static JBuffer buffer(int size)
size - allocation size in bytes
public static void malloc(int size,
JMemory storage)
size - the sizestorage - the storage
public void allocate(int size,
JMemory memory)
size - number of bytesmemory - memory pointerpublic JMemory allocateExclusive(int size)
size - amount of native memory to allocate in bytes
public int duplicate(JMemory src,
JMemory dst)
src - source memory to copy fromdst - destination object to peer with new memory containing copy of
memory pointed to by src
public int duplicate2(JMemory src1,
JMemory src2,
JMemory dst1,
JMemory dst2)
src1 - first src for duplicatesrc2 - second src for duplicate into the same memorydst1 - peered with new memory using src1 lengthdst2 - peered with same memory at src1.length offset using src2 length as
length of peer
public int duplicate2(JMemory src1,
java.nio.ByteBuffer src2,
JMemory dst1,
JMemory dst2)
src1 - first src for duplicatesrc2 - second src for duplicate into the same memorydst1 - peered with new memory using src1 lengthdst2 - peered with same memory at src1.length offset using src2 length as
length of peer
public int duplicate(java.nio.ByteBuffer src,
JMemory dst)
src - source memory to copy fromdst - destination object to peer with new memory containing copy of
memory pointed to by src
public JMemoryPool.Block getBlock(int size)
size - minimum available amount of memory in a 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. 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.JMemoryPool.Block.allocate(int)public static JMemoryPool defaultMemoryPool()
public static void shutdown()
public int getBlockSize()
public void setBlockSize(int blockSize)
blockSize - the blockSize to set
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||