1 /* $Id: HBFifo.cpp,v 1.10 2003/08/24 13:27:41 titer Exp $ */
8 HBBuffer::HBBuffer( int size )
13 fData = (uint8_t*) malloc( size );
17 Log( "HBBuffer::HBBuffer() : malloc() failed, gonna crash soon" );
26 void HBBuffer::ReAlloc( int size )
28 realloc( fData, size );
32 Log( "HBBuffer::ReAlloc() : realloc() failed, gonna crash soon" );
39 HBFifo::HBFifo( int capacity )
45 fBuffers = (HBBuffer**) malloc( ( fCapacity + 1 ) * sizeof( void* ) );
46 fLocker = new BLocker();
55 while( fWhereToPush != fWhereToPop )
57 HBBuffer * buffer = fBuffers[fWhereToPop];
59 fWhereToPop %= ( fCapacity + 1 );
71 while( fWhereToPush != fWhereToPop )
73 HBBuffer * buffer = fBuffers[fWhereToPop];
75 fWhereToPop %= ( fCapacity + 1 );
84 /* Size() : returns how much the fifo is currently filled */
87 return ( fCapacity + 1 + fWhereToPush - fWhereToPop ) %
91 /* Capacity() : simply returns the fifo capacity... */
92 int HBFifo::Capacity()
97 /* Push() : add a packet to the fifo. If the fifo is full, it blocks
98 until the packet can be added. Returns true when it is successful,
99 or false if the fifo has been destroyed before we could add it */
100 bool HBFifo::Push( HBBuffer * buffer )
102 bool success = false;
107 if( Size() < fCapacity )
109 fBuffers[fWhereToPush] = buffer;
111 fWhereToPush %= ( fCapacity + 1 );
128 /* Pop() : get the first packet if the fifo. If the fifo is empty, it
129 blocks until a packet comes. Returns true when it is successful,
130 or false if the fifo has been destroyed before we could get a packet */
131 HBBuffer * HBFifo::Pop()
136 if( fWhereToPush != fWhereToPop )
138 HBBuffer * buffer = fBuffers[fWhereToPop];
140 fWhereToPop %= ( fCapacity + 1 );
151 /* Lock() : private function */
157 /* Unlock() : private function */
158 void HBFifo::Unlock()