Pool buffers in Fifos

Discussion to talk about software related topics only.
Post Reply
sswope
Posts: 7
Joined: Thu Jan 22, 2009 10:10 am

Pool buffers in Fifos

Post by sswope »

I have a question about pool buffer management.

Let's say that a function creates, initializes, and then registers a fifo for a UDP port.
The function then waits for incoming packets. Several packets arrive and are placed in
the fifo. The function removes one that it needed, with other packets still remaining
in the fifo. If the function then returns, without emptying the fifo, what happens to
the pool buffers containing the rest of the packets that were still linked in the fifo list
when the fifo goes away ?
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Pool buffers in Fifos

Post by rnixon »

Are you unregistering the fifo before you exit?
sswope
Posts: 7
Joined: Thu Jan 22, 2009 10:10 am

Re: Pool buffers in Fifos

Post by sswope »

Are you unregistering the fifo before you exit?


Yes. But unregistering doesn't empty the fifo. It only breaks the
association with a port.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Pool buffers in Fifos

Post by rnixon »

It's a c++ object, so maybe the fifo is freed when the destructor is called?
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: Pool buffers in Fifos

Post by tod »

Are you talking about OS_FIFIO? I'm pretty sure that's written in C. If you are allocating structures inside your function on the stack, then when your function exits your structures are released. If you're allocating your structures on the heap then you have to release them yourself. If you don't, then I would suspect that when your function exits you leak the memory.
sswope
Posts: 7
Joined: Thu Jan 22, 2009 10:10 am

Re: Pool buffers in Fifos

Post by sswope »

It's a c++ object, so maybe the fifo is freed when the destructor is called?

The netburner code shows fifos as a struct. Also OSFifoInit must be called explicitly
(instead of being included in a constructor), so I think it's a C object.

Are you talking about OS_FIFIO? I'm pretty sure that's written in C. If you are allocating structures inside your function on the stack, then when your function exits your structures are released.

Yes it is allocated on the stack just like several of netburner's examples. When the
function exits, the fifo is freed. However, notice what is freed:
typedef struct os_fifo {
OS_FIFO_EL *pHead;
OS_FIFO_EL *pTail;
BYTE OSFifoGrp;
BYTE OSFifoTbl[8];
} OS_FIFO;
Assuming that there is no smart destructor (and I cannot find a constructor nor a
destructor in the netburner code) then the fifo elements which are pool buffers in
this case (UDP packets) are lost.
In most of the netburner code, there is no concern about any (additional) packets
that may have arrived and were placed in the fifo but not used (removed from the
fifo). The only exception that I've seen is the code for the TFTP client (GetTFTP and
SendTFTP), where the fifo is explicitly emptied after unregistering the fifo.
It makes one wonder if this kind of code should always be included between
unregistering the fifo and freeing the fifo.
User avatar
tod
Posts: 587
Joined: Sat Apr 26, 2008 8:27 am
Location: Southern California
Contact:

Re: Pool buffers in Fifos

Post by tod »

Maybe if you showed your actual code this would make more sense to me. When I deal with ethernet buffers the first thing I do is read the data in a buffer that I own (a side-effect is the buffer is immediately freed for resuse). I'm not sure what the pool buffers even have to do with this question unless you're somehow using the underlying pool buffer pointers directly. Can you post the function?
User avatar
pbreed
Posts: 1088
Joined: Thu Apr 24, 2008 3:58 pm

Re: Pool buffers in Fifos

Post by pbreed »

You must empty the fifo or it is a buffer leak that will eventually kill the system

After you unregister the fifo (To make sure no new packets sneak in while you are unregistering)


Then

do
{
PoolPtr pp= (PoolPtr) OSFifoPendNoWait( &TheFifo);
if(pp!=NULL) FreeBuffer(pp);
}
while (pp!=NULL);
Post Reply