NLMISC::CBufFIFO Class Reference

#include <buf_fifo.h>


Detailed Description

This class is a dynamic size FIFO that contains variable size uint8 buffer. It's used in the layer1 network for storing temporary messages. You can resize the internal FIFO buffer if you know the average size of data you'll put in it. It have the same behavior as STL so if the buffer is full the size will be automatically increase by 2.
CBufFIFO fifo; fifo.resize(10000); vector<uint8> vec; vec.resize(rand()%256); memset (&(vec[0]), '-', vec.size()); // push the vector fifo.push(vec); // display the fifo fifo.display(); vector<uint8> vec2; // get the vector fifo.pop(vec2);
Author:
Vianney Lecroart

Nevrax France

Date:
2001

Definition at line 64 of file buf_fifo.h.

Public Member Functions

 CBufFIFO ()
void clear ()
 Erase the FIFO.

void display ()
 display the FIFO to stdout (used to debug the FIFO)

void displayStats (CLog *log=InfoLog)
 display the FIFO statistics (speed, nbcall, etc...) to stdout

bool empty ()
 Return true if the FIFO is empty.

void front (uint8 *&buffer, uint32 &size)
void front (NLMISC::CMemStream &buffer)
void front (std::vector< uint8 > &buffer)
 Get the buffer in the tail of the FIFO and put it in 'buffer'.

uint8 frontLast ()
void pop ()
 Pop the buffer in the tail of the FIFO.

void push (const std::vector< uint8 > &buffer1, const std::vector< uint8 > &buffer2)
 Concate and push 'buffer1' and buffer2 in the head of the FIFO. The goal is to avoid a copy.

void push (const uint8 *buffer, uint32 size)
void push (const NLMISC::CMemStream &buffer)
void push (const std::vector< uint8 > &buffer)
 Push 'buffer' in the head of the FIFO.

void resize (uint32 size)
 Set the size of the FIFO buffer in byte.

uint32 size ()
 Returns the size of the FIFO.

 ~CBufFIFO ()

Private Types

typedef uint32 TFifoSize

Private Member Functions

bool canFit (uint32 size)

Private Attributes

uint32 _BiggestBlock
uint32 _BiggestBuffer
uint8_Buffer
uint32 _BufferSize
bool _Empty
uint32 _Fronted
TTicks _FrontedTime
uint8_Head
uint32 _Pushed
TTicks _PushedTime
uint32 _Resized
TTicks _ResizedTime
uint8_Rewinder
uint32 _SmallestBlock
uint32 _SmallestBuffer
uint8_Tail


Member Typedef Documentation

typedef uint32 NLMISC::CBufFIFO::TFifoSize [private]
 

Definition at line 117 of file buf_fifo.h.

Referenced by front(), frontLast(), pop(), and push().


Constructor & Destructor Documentation

NLMISC::CBufFIFO::CBufFIFO  ) 
 

Definition at line 42 of file buf_fifo.cpp.

References _BiggestBlock, _BiggestBuffer, _Fronted, _FrontedTime, _Pushed, _PushedTime, _Resized, _ResizedTime, _SmallestBlock, and _SmallestBuffer.

00042                    : _Buffer(NULL), _BufferSize(0), _Empty(true), _Head(NULL), _Tail(NULL), _Rewinder(NULL)
00043 {
00044         // reset statistic
00045         _BiggestBlock = 0;
00046         _SmallestBlock = 999999999;
00047         _BiggestBuffer = 0;
00048         _SmallestBuffer = 999999999;
00049         _Pushed = 0;
00050         _Fronted = 0;
00051         _Resized = 0;
00052         _PushedTime = 0;
00053         _FrontedTime = 0;
00054         _ResizedTime = 0;
00055 }

NLMISC::CBufFIFO::~CBufFIFO  ) 
 

Definition at line 57 of file buf_fifo.cpp.

References nldebug.

00058 {
00059         if (_Buffer != NULL)
00060         {
00061                 delete []_Buffer;
00062 #if DEBUG_FIFO
00063                 nldebug("%p delete", this);
00064 #endif
00065         }
00066 }


Member Function Documentation

bool NLMISC::CBufFIFO::canFit uint32  size  )  [private]
 

Definition at line 606 of file buf_fifo.cpp.

References _BufferSize, _Head, _Tail, nldebug, and s.

Referenced by push().

00607 {
00608         if (_Tail == _Head)
00609         {
00610                 if (empty())
00611                 {
00612                         // is the buffer large enough?
00613                         if (_BufferSize >= s)
00614                         {
00615                                 // reset the pointer
00616 #if DEBUG_FIFO
00617                                 nldebug("%p reset tail and head", this);
00618 #endif
00619                                 _Head = _Tail = _Buffer;
00620                                 return true;
00621                         }
00622                         else
00623                         {
00624                                 // buffer not big enough
00625 #if DEBUG_FIFO
00626                                 nldebug("%p buffer full buffersize<size", this);
00627 #endif
00628                                 return false;
00629                         }
00630                 }
00631                 else
00632                 {
00633                         // buffer full
00634 #if DEBUG_FIFO
00635                         nldebug("%p buffer full h=t", this);
00636 #endif
00637                         return false;
00638                 }
00639         }
00640         else if (_Tail < _Head)
00641         {
00642                 if (_Buffer + _BufferSize - _Head >= (sint32) s)
00643                 {
00644                         // can fit after _Head
00645 #if DEBUG_FIFO
00646                         nldebug("%p fit after", this);
00647 #endif
00648                         return true;
00649                 }
00650                 else if (_Tail - _Buffer >= (sint32) s)
00651                 {
00652                         // can fit at the beginning
00653 #if DEBUG_FIFO
00654                         nldebug("%p fit at beginning", this);
00655 #endif
00656                         _Rewinder = _Head;
00657 #if DEBUG_FIFO
00658                         nldebug("%p set the rewinder", this);
00659 #endif
00660                         _Head = _Buffer;
00661                         return true;
00662                 }
00663                 else
00664                 {
00665                         // can't fit
00666 #if DEBUG_FIFO
00667                         nldebug("%p no room t<h", this);
00668 #endif
00669                         return false;
00670                 }
00671         }
00672         else // the last case is : if (_Tail > _Head)
00673         {
00674                 if (_Tail - _Head >= (sint32) s)
00675                 {
00676 #if DEBUG_FIFO
00677                         nldebug("%p fit t>h", this);
00678 #endif
00679                         return true;
00680                 }
00681                 else
00682                 {
00683 #if DEBUG_FIFO
00684                         nldebug("%p no room t>h", this);
00685 #endif
00686                         return false;
00687                 }
00688         }
00689         nlstop;
00690 }

void NLMISC::CBufFIFO::clear  ) 
 

Erase the FIFO.

Definition at line 403 of file buf_fifo.cpp.

References _Head, _Rewinder, and _Tail.

00404 {
00405         _Tail = _Head = _Buffer;
00406         _Rewinder = NULL;
00407         _Empty = true;
00408 }

void NLMISC::CBufFIFO::display  ) 
 

display the FIFO to stdout (used to debug the FIFO)

Definition at line 540 of file buf_fifo.cpp.

References _BufferSize, _Head, _Rewinder, _Tail, NLMISC::DebugLog, NLMISC::CLog::display(), empty(), s, sint32, NLMISC::smprintf(), uint32, and uint8.

Referenced by front(), pop(), push(), and resize().

00541 {
00542         int s = 64;
00543         int gran = s/30;
00544 
00545         char str[1024];
00546 
00547         smprintf(str, 1024, "%p %p (%5d %5d) %p %p %p ", this, _Buffer, _BufferSize, CBufFIFO::size(), _Rewinder, _Tail, _Head);
00548 
00549         int i;
00550         for (i = 0; i < (sint32) _BufferSize; i+= gran)
00551         {
00552                 uint8 *pos = _Buffer + i;
00553                 if (_Tail >= pos && _Tail < pos + gran)
00554                 {
00555                         if (_Head >= pos && _Head < pos + gran)
00556                         {
00557                                 if (_Rewinder != NULL && _Rewinder >= pos && _Rewinder < pos + gran)
00558                                 {
00559                                         strncat (str, "*", 1024);
00560                                 }
00561                                 else
00562                                 {
00563                                         strncat (str, "@", 1024);
                                }
                        }
                        else
                        {
                                strncat (str, "T", 1024);
00564                         }
00565                 }
00566                 else if (_Head >= pos && _Head < pos + gran)
00567                 {
00568                         strncat (str, "H", 1024);
00569                 }
00570                 else if (_Rewinder != NULL && _Rewinder >= pos && _Rewinder < pos + gran)
00571                 {
00572                         strncat (str, "R", 1024);
00573                 }
00574                 else
00575                 {
00576                         if (strlen(str) < 1023)
00577                         {
00578                                 uint32 p = strlen(str);
00579                                 if (isprint(*pos))
00580                                         str[p] = *pos;
00581                                 else
00582                                         str[p] = '$';
00583 
00584                                 str[p+1] = '\0';
00585                         }
00586                 }
00587         }
00588 
00589         for (; i < s; i+= gran)
00590         {
00591                 strncat (str, " ", 1024);
00592         }
00593 #ifdef NL_DEBUG
00594         strncat (str, "\n", 1024);
00595 #else
00596         strncat (str, "\r", 1024);
00597 #endif
00598         DebugLog->display (str);
00599 }
00600 

void NLMISC::CBufFIFO::displayStats CLog log = InfoLog  ) 
 

display the FIFO statistics (speed, nbcall, etc...) to stdout

Definition at line 528 of file buf_fifo.cpp.

References _BiggestBlock, _BiggestBuffer, _BufferSize, _Fronted, _FrontedTime, _Pushed, _PushedTime, _Resized, _ResizedTime, _SmallestBlock, _SmallestBuffer, NLMISC::CLog::displayNL(), NL_I64, sint64, and size().

Referenced by NLNET::CBufServer::displaySendQueueStat(), and NLNET::CBufClient::displaySendQueueStat().

00529 {
00530         log->displayNL ("%p CurrentQueueSize: %d, TotalQueueSize: %d", this, size(), _BufferSize);
00531         log->displayNL ("%p InQueue: %d", this, _Pushed - _Fronted);
00532 
00533         log->displayNL ("%p BiggestBlock: %d, SmallestBlock: %d", this, _BiggestBlock, _SmallestBlock);
00534         log->displayNL ("%p BiggestBuffer: %d, SmallestBuffer: %d", this, _BiggestBuffer, _SmallestBuffer);
00535         log->displayNL ("%p Pushed: %d, PushedTime: total %"NL_I64"d ticks, mean %f ticks", this, _Pushed, _PushedTime, (_Pushed>0?(double)(sint64)_PushedTime / (double)_Pushed:0.0));
00536         log->displayNL ("%p Fronted: %d, FrontedTime: total %"NL_I64"d ticks, mean %f ticks", this, _Fronted, _FrontedTime, (_Fronted>0?(double)(sint64)_FrontedTime / (double)_Fronted:0.0));
00537         log->displayNL ("%p Resized: %d, ResizedTime: total %"NL_I64"d ticks, mean %f ticks", this, _Resized, _ResizedTime, (_Resized>0?(double)(sint64)_ResizedTime / (double)_Resized:0.0));
00538 }

bool NLMISC::CBufFIFO::empty  )  [inline]
 

Return true if the FIFO is empty.

Definition at line 100 of file buf_fifo.h.

Referenced by display(), NLNET::CBufSock::flush(), front(), frontLast(), pop(), resize(), and size().

00100 { return _Empty; }

void NLMISC::CBufFIFO::front uint8 *&  buffer,
uint32 size
 

Definition at line 354 of file buf_fifo.cpp.

References _Fronted, _FrontedTime, _Rewinder, _Tail, buffer, display(), empty(), nldebug, nlwarning, s, TFifoSize, NLMISC::TTicks, uint32, and uint8.

00355 {
00356 #if STAT_FIFO
00357         TTicks before = CTime::getPerformanceTime ();
00358 #endif
00359 
00360         uint8   *tail = _Tail;
00361 
00362         if (empty ())
00363         {
00364                 nlwarning("BF: Try to get the front of an empty fifo!");
00365                 return;
00366         }
00367 
00368         _Fronted++;
00369         
00370         if (_Rewinder != NULL && tail == _Rewinder)
00371         {
00372 #if DEBUG_FIFO
00373                 nldebug("%p front rewind!", this);
00374 #endif
00375 
00376                 // need to rewind
00377                 tail = _Buffer;
00378         }
00379 
00380         s = *(TFifoSize *)tail;
00381 
00382 #if DEBUG_FIFO
00383         nldebug("%p front(%d)", this, s);
00384 #endif
00385 
00386         tail += sizeof (TFifoSize);
00387 
00388 #if STAT_FIFO
00389         // stat code
00390         TTicks after = CTime::getPerformanceTime ();
00391         _FrontedTime += after - before;
00392 #endif
00393 
00394 #if DEBUG_FIFO
00395         display ();
00396 #endif
00397 
00398         buffer = tail;
00399 }

void NLMISC::CBufFIFO::front NLMISC::CMemStream buffer  ) 
 

Definition at line 296 of file buf_fifo.cpp.

References buffer, front(), s, uint32, and uint8.

00297 {
00298         uint8 *tmpbuffer;
00299         uint32 s;
00300 
00301         buffer.clear ();
00302 
00303         front (tmpbuffer, s);
00304 
00305         buffer.fill (tmpbuffer, s);
00306         
00307         /*
00308         TTicks before = CTime::getPerformanceTime ();
00309 
00310         uint8   *tail = _Tail;
00311         
00312         buffer.clear ();
00313 
00314         if (empty ())
00315         {
00316                 nlwarning("Try to get the front of an empty fifo!");
00317                 return;
00318         }
00319 
00320         _Fronted++;
00321         
00322         if (_Rewinder != NULL && tail == _Rewinder)
00323         {
00324 #if DEBUG_FIFO
00325                 nldebug("%p front rewind!", this);
00326 #endif
00327 
00328                 // need to rewind
00329                 tail = _Buffer;
00330         }
00331 
00332         TFifoSize size = *(TFifoSize *)tail;
00333 
00334 #if DEBUG_FIFO
00335         nldebug("%p front(%d)", this, size);
00336 #endif
00337 
00338         tail += sizeof (TFifoSize);
00339 
00340         //buffer.resize (size);
00341         //CFastMem::memcpy (&(buffer[0]), tail, size);
00342 
00343         buffer.fill (tail, size);
00344 
00345         // stat code
00346         TTicks after = CTime::getPerformanceTime ();
00347         _FrontedTime += after - before;
00348 
00349 #if DEBUG_FIFO
00350         display ();
00351 #endif*/
00352 }

void NLMISC::CBufFIFO::front std::vector< uint8 > &  buffer  ) 
 

Get the buffer in the tail of the FIFO and put it in 'buffer'.

Definition at line 237 of file buf_fifo.cpp.

References buffer, s, uint32, and uint8.

Referenced by NLNET::CBufSock::flush(), and front().

00238 {
00239         uint8 *tmpbuffer;
00240         uint32 s;
00241 
00242         buffer.clear ();
00243 
00244         front (tmpbuffer, s);
00245         
00246         buffer.resize (s);
00247 
00248         CFastMem::memcpy (&(buffer[0]), tmpbuffer, s);
00249 
00250 /*      TTicks before = CTime::getPerformanceTime ();
00251 
00252         uint8   *tail = _Tail;
00253         
00254         buffer.clear ();
00255 
00256         if (empty ())
00257         {
00258                 nlwarning("Try to get the front of an empty fifo!");
00259                 return;
00260         }
00261 
00262         _Fronted++;
00263         
00264         if (_Rewinder != NULL && tail == _Rewinder)
00265         {
00266 #if DEBUG_FIFO
00267                 nldebug("%p front rewind!", this);
00268 #endif
00269 
00270                 // need to rewind
00271                 tail = _Buffer;
00272         }
00273 
00274         TFifoSize size = *(TFifoSize *)tail;
00275 
00276 #if DEBUG_FIFO
00277         nldebug("%p front(%d)", this, size);
00278 #endif
00279 
00280         tail += sizeof (TFifoSize);
00281 
00282         buffer.resize (size);
00283 
00284         CFastMem::memcpy (&(buffer[0]), tail, size);
00285 
00286         // stat code
00287         TTicks after = CTime::getPerformanceTime ();
00288         _FrontedTime += after - before;
00289 
00290 #if DEBUG_FIFO
00291         display ();
00292 #endif
00293 */}

uint8 NLMISC::CBufFIFO::frontLast  ) 
 

This function returns the last byte of the front message It is used by the network to know a value quickly without doing front()

Definition at line 207 of file buf_fifo.cpp.

References _Rewinder, _Tail, empty(), nldebug, nlwarning, s, size, TFifoSize, and uint8.

00208 {
00209         uint8   *tail = _Tail;
00210         
00211         if (empty ())
00212         {
00213                 nlwarning("BF: Try to get the front of an empty fifo!");
00214                 return 0;
00215         }
00216 
00217         if (_Rewinder != NULL && tail == _Rewinder)
00218         {
00219 #if DEBUG_FIFO
00220                 nldebug("%p front rewind!", this);
00221 #endif
00222 
00223                 // need to rewind
00224                 tail = _Buffer;
00225         }
00226 
00227         TFifoSize s = *(TFifoSize *)tail;
00228 
00229 #if DEBUG_FIFO
00230         nldebug("%p frontLast() returns %d ", this, s, *(tail+sizeof(TFifoSize)+size-1));
00231 #endif
00232 
00233         return *(tail+sizeof(TFifoSize)+s-1);
00234 }

void NLMISC::CBufFIFO::pop  ) 
 

Pop the buffer in the tail of the FIFO.

Definition at line 168 of file buf_fifo.cpp.

References _Head, _Rewinder, _Tail, display(), empty(), nldebug, nlwarning, s, and TFifoSize.

Referenced by NLNET::CBufSock::flush().

00169 {
00170         if (empty ())
00171         {
00172                 nlwarning("BF: Try to pop an empty fifo!");
00173                 return;
00174         }
00175 
00176         if (_Rewinder != NULL && _Tail == _Rewinder)
00177         {
00178 #if DEBUG_FIFO
00179                 nldebug("%p pop rewind!", this);
00180 #endif
00181 
00182                 // need to rewind
00183                 _Tail = _Buffer;
00184                 _Rewinder = NULL;
00185         }
00186 
00187         TFifoSize s = *(TFifoSize *)_Tail;
00188 
00189 #if DEBUG_FIFO
00190         nldebug("%p pop(%d)", this, s);
00191 #endif
00192 
00193 #ifdef NL_DEBUG
00194         // clear the message to be sure user doesn't use it anymore
00195         memset (_Tail, '-', s + sizeof (TFifoSize));
00196 #endif
00197 
00198         _Tail += s + sizeof (TFifoSize);
00199 
00200         if (_Tail == _Head) _Empty = true;
00201 
00202 #if DEBUG_FIFO
00203         display ();
00204 #endif
00205 }

void NLMISC::CBufFIFO::push const std::vector< uint8 > &  buffer1,
const std::vector< uint8 > &  buffer2
 

Concate and push 'buffer1' and buffer2 in the head of the FIFO. The goal is to avoid a copy.

Definition at line 113 of file buf_fifo.cpp.

References _BiggestBlock, _BufferSize, _Head, _Pushed, _PushedTime, _SmallestBlock, canFit(), display(), nlassert, nldebug, resize(), s, size(), TFifoSize, and NLMISC::TTicks.

00114 {
00115 #if STAT_FIFO
00116         TTicks before = CTime::getPerformanceTime();
00117 #endif
00118 
00119         TFifoSize s = buffer1.size() + buffer2.size();
00120 
00121 #if DEBUG_FIFO
00122         nldebug("%p push2(%d)", this, s);
00123 #endif
00124 
00125         nlassert((buffer1.size() + buffer2.size ()) > 0 && (buffer1.size() + buffer2.size ()) < pow(2, sizeof(TFifoSize)*8));
00126 
00127         // avoid too big fifo
00128         if (this->size() > 10000000)
00129         {
00130                 throw Exception ("CBufFIFO::push(): stack full (more than 10mb)");
00131         }
00132 
00133 
00134         // stat code
00135         if (s > _BiggestBlock) _BiggestBlock = s;
00136         if (s < _SmallestBlock) _SmallestBlock = s;
00137 
00138         _Pushed++;
00139 
00140         // resize while the buffer is enough big to accept the block
00141         while (!canFit (s + sizeof (TFifoSize)))
00142         {
00143                 resize(_BufferSize * 2);
00144         }
00145 
00146         // store the size of the block
00147         *(TFifoSize *)_Head = s;
00148         _Head += sizeof(TFifoSize);
00149 
00150         // store the block itself
00151         CFastMem::memcpy(_Head, &(buffer1[0]), buffer1.size());
00152         CFastMem::memcpy(_Head + buffer1.size(), &(buffer2[0]), buffer2.size());
00153         _Head += s;
00154 
00155         _Empty = false;
00156 
00157 #if STAT_FIFO
00158         // stat code
00159         TTicks after = CTime::getPerformanceTime();
00160         _PushedTime += after - before;
00161 #endif
00162 
00163 #if DEBUG_FIFO
00164         display ();
00165 #endif
00166 }

void NLMISC::CBufFIFO::push const uint8 buffer,
uint32  size
 

Definition at line 68 of file buf_fifo.cpp.

References _BiggestBlock, _BufferSize, _Head, _Pushed, _PushedTime, _SmallestBlock, buffer, canFit(), display(), nlassert, nldebug, resize(), s, TFifoSize, NLMISC::TTicks, uint32, and uint8.

00069 {
00070         // if the buffer is more than 1 meg, there s surely a problem, no?
00071 //      nlassert( buffer.size() < 1000000 ); // size check in debug mode
00072 
00073 #if STAT_FIFO
00074         TTicks before = CTime::getPerformanceTime();
00075 #endif
00076 
00077 #if DEBUG_FIFO
00078         nldebug("%p push(%d)", this, s);
00079 #endif
00080 
00081         nlassert(s > 0 && s < pow(2, sizeof(TFifoSize)*8));
00082 
00083         // stat code
00084         if (s > _BiggestBlock) _BiggestBlock = s;
00085         if (s < _SmallestBlock) _SmallestBlock = s;
00086         _Pushed++;
00087 
00088         while (!canFit (s + sizeof (TFifoSize)))
00089         {
00090                 resize(_BufferSize * 2);
00091         }
00092 
00093         *(TFifoSize *)_Head = s;
00094         _Head += sizeof(TFifoSize);
00095 
00096         CFastMem::memcpy(_Head, buffer, s);
00097 
00098         _Head += s;
00099 
00100         _Empty = false;
00101 
00102 #if STAT_FIFO
00103         // stat code
00104         TTicks after = CTime::getPerformanceTime();
00105         _PushedTime += after - before;
00106 #endif
00107 
00108 #if DEBUG_FIFO
00109         display ();
00110 #endif
00111 }

void NLMISC::CBufFIFO::push const NLMISC::CMemStream buffer  )  [inline]
 

Definition at line 74 of file buf_fifo.h.

References buffer, and push().

00074 { push (buffer.buffer(), buffer.length()); }

void NLMISC::CBufFIFO::push const std::vector< uint8 > &  buffer  )  [inline]
 

Push 'buffer' in the head of the FIFO.

Definition at line 72 of file buf_fifo.h.

References buffer.

Referenced by push(), and NLNET::CBufSock::pushBuffer().

00072 { push (&buffer[0], buffer.size()); }

void NLMISC::CBufFIFO::resize uint32  size  ) 
 

Set the size of the FIFO buffer in byte.

Definition at line 437 of file buf_fifo.cpp.

References _BiggestBuffer, _BufferSize, _Head, _Resized, _ResizedTime, _Rewinder, _SmallestBuffer, _Tail, display(), empty(), nlassert, nldebug, nlerror, nlwarning, s, size, NLMISC::TTicks, uint, uint32, and uint8.

Referenced by push().

00438 {
00439 #if STAT_FIFO
00440         TTicks before = CTime::getPerformanceTime();
00441 #endif
00442 
00443         if (s == 0) s = 100;
00444 
00445 #if DEBUG_FIFO
00446         nldebug("%p resize(%d)", this, s);
00447 #endif
00448 
00449         if (s > _BiggestBuffer) _BiggestBuffer = s;
00450         if (s < _SmallestBuffer) _SmallestBuffer = s;
00451 
00452         _Resized++;
00453 
00454         uint32 UsedSize = CBufFIFO::size();
00455 
00456         // creer un nouveau tableau et copie l ancien dans le nouveau.
00457         if (s < _BufferSize && UsedSize > s)
00458         {
00459                 // probleme, on a pas assez de place pour caser les datas => on fait pas
00460                 nlwarning("BF: Can't resize the FIFO because there's not enough room in the new wanted buffer (%d bytes needed at least)", UsedSize);
00461                 return;
00462         }
00463 
00464         uint8 *NewBuffer = new uint8[s];
00465         if (NewBuffer == NULL)
00466         {
00467                 nlerror("Not enough memory to resize the FIFO to %u bytes", s);
00468         }
00469 #ifdef NL_DEBUG
00470         // clear the message to be sure user doesn't use it anymore
00471         memset (NewBuffer, '-', s);
00472 #endif
00473 
00474 #if DEBUG_FIFO
00475         nldebug("%p new %d bytes", this, s);
00476 #endif
00477 
00478         // copy the old buffer to the new one
00479         // if _Tail == _Head => empty fifo, don't copy anything
00480         if (!empty())
00481         {
00482                 if (_Tail < _Head)
00483                 {
00484                         CFastMem::memcpy (NewBuffer, _Tail, UsedSize);
00485                 }
00486                 else if (_Tail >= _Head)
00487                 {
00488                         nlassert (_Rewinder != NULL);
00489 
00490                         uint size1 = _Rewinder - _Tail;
00491                         CFastMem::memcpy (NewBuffer, _Tail, size1);
00492                         uint size2 = _Head - _Buffer;
00493                         CFastMem::memcpy (NewBuffer + size1, _Buffer, size2);
00494 
00495                         nlassert (size1+size2==UsedSize);
00496                 }
00497         }
00498 
00499         // resync the circular pointer
00500         // Warning: don't invert these 2 lines position or it ll not work
00501         _Tail = NewBuffer;
00502         _Head = NewBuffer + UsedSize;
00503         _Rewinder = NULL;
00504 
00505         // delete old buffer if needed
00506         if (_Buffer != NULL)
00507         {
00508                 delete []_Buffer;
00509 #if DEBUG_FIFO
00510                 nldebug ("delete", this);
00511 #endif
00512         }
00513 
00514         // affect new buffer
00515         _Buffer = NewBuffer;
00516         _BufferSize = s;
00517 
00518 #if STAT_FIFO
00519         TTicks after = CTime::getPerformanceTime();
00520         _ResizedTime += after - before;
00521 #endif
00522 
00523 #if DEBUG_FIFO
00524         display ();
00525 #endif
00526 }

uint32 NLMISC::CBufFIFO::size  ) 
 

Returns the size of the FIFO.

Definition at line 410 of file buf_fifo.cpp.

References _BufferSize, _Head, _Rewinder, _Tail, empty(), nlassert, nlstop, and uint32.

Referenced by displayStats(), NLNET::CBufServer::getSendQueueSize(), NLNET::CBufClient::getSendQueueSize(), push(), and NLNET::CBufSock::update().

00411 {
00412         if (empty ())
00413         {
00414                 return 0;
00415         }
00416         else if (_Head == _Tail)
00417         {
00418                 // buffer is full
00419                 if (_Rewinder == NULL)
00420                         return _BufferSize;
00421                 else
00422                         return _Rewinder - _Buffer;
00423         }
00424         else if (_Head > _Tail)
00425         {
00426                 return _Head - _Tail;
00427         }
00428         else if (_Head < _Tail)
00429         {
00430                 nlassert (_Rewinder != NULL);
00431                 return (_Rewinder - _Tail) + (_Head - _Buffer);
00432         }
00433         nlstop;
00434         return 0;
00435 }


Field Documentation

uint32 NLMISC::CBufFIFO::_BiggestBlock [private]
 

Definition at line 140 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and push().

uint32 NLMISC::CBufFIFO::_BiggestBuffer [private]
 

Definition at line 142 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and resize().

uint8* NLMISC::CBufFIFO::_Buffer [private]
 

Definition at line 120 of file buf_fifo.h.

uint32 NLMISC::CBufFIFO::_BufferSize [private]
 

Definition at line 122 of file buf_fifo.h.

Referenced by canFit(), display(), displayStats(), push(), resize(), and size().

bool NLMISC::CBufFIFO::_Empty [private]
 

Definition at line 125 of file buf_fifo.h.

uint32 NLMISC::CBufFIFO::_Fronted [private]
 

Definition at line 145 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and front().

TTicks NLMISC::CBufFIFO::_FrontedTime [private]
 

Definition at line 148 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and front().

uint8* NLMISC::CBufFIFO::_Head [private]
 

Definition at line 128 of file buf_fifo.h.

Referenced by canFit(), clear(), display(), pop(), push(), resize(), and size().

uint32 NLMISC::CBufFIFO::_Pushed [private]
 

Definition at line 144 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and push().

TTicks NLMISC::CBufFIFO::_PushedTime [private]
 

Definition at line 147 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and push().

uint32 NLMISC::CBufFIFO::_Resized [private]
 

Definition at line 146 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and resize().

TTicks NLMISC::CBufFIFO::_ResizedTime [private]
 

Definition at line 149 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and resize().

uint8* NLMISC::CBufFIFO::_Rewinder [private]
 

Definition at line 132 of file buf_fifo.h.

Referenced by clear(), display(), front(), frontLast(), pop(), resize(), and size().

uint32 NLMISC::CBufFIFO::_SmallestBlock [private]
 

Definition at line 141 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and push().

uint32 NLMISC::CBufFIFO::_SmallestBuffer [private]
 

Definition at line 143 of file buf_fifo.h.

Referenced by CBufFIFO(), displayStats(), and resize().

uint8* NLMISC::CBufFIFO::_Tail [private]
 

Definition at line 130 of file buf_fifo.h.

Referenced by canFit(), clear(), display(), front(), frontLast(), pop(), resize(), and size().


The documentation for this class was generated from the following files:
Generated on Tue Mar 16 13:06:00 2004 for NeL by doxygen 1.3.6