ringbuffer_slide.i 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /////////////////////////////////////////////////////////////
  2. // Function name : RingBuffer_Slide::GetMessageHead
  3. // Description : This will get a pointer to the fist undelivered data in buffer
  4. // Return type : char *
  5. // Argument : void
  6. //////////////////////////////////////////////////////////
  7. inline char * RingBuffer_Slide::GetMessageHead(void)
  8. {
  9. return _Buffer+_StartPos;
  10. }
  11. /////////////////////////////////////////////////////////////
  12. // Function name : RingBuffer_Slide::GetBufferOpen
  13. // Description : This will get the first writabe section of the buffer space
  14. // Return type :
  15. // Argument : void
  16. //////////////////////////////////////////////////////////
  17. inline char * RingBuffer_Slide::GetBufferOpen(void)
  18. {
  19. return _Buffer+_EndPos;
  20. }
  21. /////////////////////////////////////////////////////////////
  22. // Function name : RingBuffer_Slide::ForceWindowSlide
  23. // Description : Will force a compression of data // shift left to start position
  24. // Return type : inline void
  25. // Argument : void
  26. //////////////////////////////////////////////////////////
  27. inline void RingBuffer_Slide::ForceWindowSlide(void)
  28. {
  29. size_t len = AmountBuffered();
  30. if(len > 0 && _StartPos != 0) // basic flush left..
  31. {
  32. memmove(_Buffer,GetMessageHead(),len);
  33. _StartPos = 0;
  34. _EndPos = len;
  35. }
  36. }
  37. /////////////////////////////////////////////////////////////
  38. // Function name : RingBuffer_Slide::AmountBuffered
  39. // Description : Will report the number of unread chars in buffer
  40. // Return type : int
  41. // Argument : void
  42. //////////////////////////////////////////////////////////
  43. inline size_t RingBuffer_Slide::AmountBuffered(void)
  44. {
  45. return _EndPos - _StartPos;
  46. }
  47. /////////////////////////////////////////////////////////////
  48. // Function name : RingBuffer_Slide::BufferAvailabe
  49. // Description : Will report amount of data that is contiguas that can be writen at
  50. // the location returned by GetBufferOpen
  51. // Return type : inline int
  52. // Argument : void
  53. //////////////////////////////////////////////////////////
  54. inline size_t RingBuffer_Slide::BufferAvailabe(void)
  55. {
  56. return GetBufferSize() - _EndPos;
  57. }
  58. /////////////////////////////////////////////////////////////
  59. // Function name : RingBuffer_Slide::ResetContent
  60. // Description : Throw away all inread information
  61. // Return type : void
  62. // Argument : void
  63. //////////////////////////////////////////////////////////
  64. void RingBuffer_Slide::ResetContent(void)
  65. {
  66. _StartPos = 0;
  67. _EndPos = 0;
  68. }
  69. /////////////////////////////////////////////////////////////
  70. // Function name : RingBuffer_Slide::RingBuffer_Slide
  71. // Description :
  72. // Return type : inline
  73. // Argument : int in_size
  74. //////////////////////////////////////////////////////////
  75. inline RingBuffer_Slide::RingBuffer_Slide(size_t in_size) : MemBuffer(in_size)
  76. {
  77. _EndPos = 0;
  78. _StartPos = 0;
  79. }
  80. /////////////////////////////////////////////////////////////
  81. // Function name : RingBuffer_Slide::FullCompress
  82. // Description : Force a compress of the data
  83. // Return type : inline void
  84. // Argument : void
  85. //////////////////////////////////////////////////////////
  86. inline void RingBuffer_Slide::FullCompress(void)
  87. {
  88. if(_StartPos == _EndPos)
  89. {
  90. _StartPos = 0;
  91. _EndPos = 0;
  92. }
  93. else
  94. {
  95. ForceWindowSlide();
  96. }
  97. }
  98. /////////////////////////////////////////////////////////////
  99. // Function name : RingBuffer_Slide::Compress
  100. // Description : Try and do a intelegent compress of the data space
  101. // the algorithem is really stupid right know.. just say if i have
  102. // read past 1/2 my space do a compress...Im open for sugestions
  103. //
  104. //
  105. // Return type : inline void
  106. // Argument : void
  107. //////////////////////////////////////////////////////////
  108. inline void RingBuffer_Slide::Compress(void)
  109. {
  110. if(_StartPos == _EndPos)
  111. {
  112. _StartPos = 0;
  113. _EndPos = 0;
  114. }
  115. else if(_StartPos >= GetBufferSize() / 2)
  116. {
  117. ForceWindowSlide();
  118. }
  119. }
  120. /////////////////////////////////////////////////////////////
  121. // Function name : RingBuffer_Slide::Put
  122. // Description : Adds Data to a ring Buffer
  123. // Will do a compress if needed so pointers suplied by Get Call are no longer valide
  124. //
  125. // Return type : inline bool
  126. // Argument : char * data
  127. // Argument : int len
  128. //////////////////////////////////////////////////////////
  129. inline bool RingBuffer_Slide::Put(const char * data, size_t len)
  130. {
  131. bool answer = false;
  132. if(len > BufferAvailabe() )
  133. Compress();
  134. if(len <= BufferAvailabe() )
  135. {
  136. memcpy(GetBufferOpen(),data,len);
  137. _EndPos += len;
  138. answer = true;
  139. }
  140. return answer;
  141. }
  142. ////////////////////////////////////////////////////////////////////
  143. // Function name : RingBuffer_Slide::PutFast
  144. // Description :
  145. //
  146. // Return type : inline bool
  147. // Argument : const char * data
  148. // Argument : int len
  149. ////////////////////////////////////////////////////////////////////
  150. inline bool RingBuffer_Slide::PutFast(const char * data, size_t len)
  151. {
  152. // no checking be carefull
  153. memcpy(GetBufferOpen(),data,len); // should i be using memcopy..
  154. _EndPos += len;
  155. return true;
  156. }
  157. /////////////////////////////////////////////////////////////
  158. // Function name : RingBuffer_Slide::Get
  159. // Description : will copy the data ..
  160. // false indicates not enogh data to read .. sorry...
  161. //
  162. // Return type : inline bool
  163. // Argument : char * data
  164. // Argument : int len
  165. //////////////////////////////////////////////////////////
  166. inline bool RingBuffer_Slide::Get(char * data, size_t len)
  167. {
  168. bool answer = false;
  169. if(len <= AmountBuffered() )
  170. {
  171. memcpy(data,GetMessageHead(),len);
  172. _StartPos += len;
  173. Compress();
  174. answer = true;
  175. }
  176. return answer;
  177. }