JMemory.toDebugString()

Printer-friendly

Its a method that allows you to see who the real owner of the memory your object referring to is. Packet state, header and buffer's get peered to each other it is very possible to loose track of who the owner is.

This debug method JMemory.toDebugString() prints out the following information:

JMemory@b3f004a: size=1506, owner=Pcap.class
JMemory@b3f004a: size=1506, owner=Pcap.class
JMemory@b3f004a: size=886, owner=Pcap.class
JMemory@af7afb0: size=1506, owner=nio.JMemoryPool$Block.class
JMemory@b3f004a: size=86, owner=Pcap.class
JMemory@b3f004a: size=886, owner=Pcap.class
JMemory@b3f004a: size=86, owner=Pcap.class
JMemory@af7e788: size=1506, owner=nio.JMemoryPool$Block.class
JMemory@af7ed7a: size=886, owner=nio.JMemoryPool$Block.class
JMemory@af7f100: size=86, owner=nio.JMemoryPool$Block.class
JMemory@af7f166: size=1506, owner=nio.JMemoryPool$Block.class
JMemory@af7f758: size=1506, owner=nio.JMemoryPool$Block.class
JMemory@af7ffc0: size=886, owner=nio.JMemoryPool$Block.class
JMemory@af80346: size=86, owner=nio.JMemoryPool$Block.class
[truncated....]

This output is from iterating over a capture file and all its packets. The JMemory.toDebugString() was called in 2 places. First on the original JBuffer that supplied with JBufferHander.nextPacket method and the second time after a new PcapPacket was created with the libpcap buffer copied into it.

You can see 2 owners. Pcap.class and JMemoryPool$Block.class. Pcap class represents memory owned and allocated by libpcap library itself. While the Block.class is the owner using jNetPcap's default allocation scheme while allocates memory in chunks and hands it out for packet copies.

Interesting thing to note is the physical address on all the memory objects where Pcap.class is the owner. They are all the same. That means that libpcap is handing out packets stored in the same memory location. If you wrap a packet around this memory and put it on a queue then each packet on that queue would reference the exact same memory location, with the last packet read by libpcap having its contents there.

This debug information demonstrates the absolute need to copy packet buffer as dispatched by libpcap to newly allocated memory before storing on the queue.

For this reason, I have changed PcapPacket(PcapHeader, JBuffer) constructor to copy the data and not peer with it. It is still possible to create empty packet objects and peer them with libpcap header and buffer, but its now not the default behavior of the constructor.