CtrlBuf.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. ////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Copyright 2016 RWS Inc, All Rights Reserved
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of version 2 of the GNU General Public License as published by
  7. // the Free Software Foundation
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU General Public License along
  15. // with this program; if not, write to the Free Software Foundation, Inc.,
  16. // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. //
  18. // CtrlBuf.h
  19. // Project: Nostril (aka Postal)
  20. //
  21. // History:
  22. // 05/26/97 MJR Started.
  23. //
  24. // 06/15/97 MJR Added Reset().
  25. //
  26. ////////////////////////////////////////////////////////////////////////////////
  27. #ifndef CTRLBUF_H
  28. #define CTRLBUF_H
  29. #include "RSPiX.h"
  30. ////////////////////////////////////////////////////////////////////////////////
  31. //
  32. // CCtrlBuf impliments a specialized buffer designed for random access.
  33. // Also, when getting a series of values from the buffer, they must ALWAYS
  34. // be in a single, contiguous chunk of memory, which is not the case with a
  35. // traditional circular buffer.
  36. //
  37. // This is pretty crude right now, as it may result in a memmove() each time
  38. // a value is discarded from the buffer. One optimization might be to move
  39. // the front of the buffer forward until the back hits the end of the
  40. // available space, and only then do a memmove() of the remaining data back
  41. // to the beginning of the buffer. Alternately, maybe a traditional queue
  42. // should be used as the core, with an intermediate buffer to take care of the
  43. // special case where a chunk of data wraps around the end of the buffer.
  44. //
  45. ////////////////////////////////////////////////////////////////////////////////
  46. class CCtrlBuf
  47. {
  48. //------------------------------------------------------------------------------
  49. // Types, enums, etc.
  50. //------------------------------------------------------------------------------
  51. public:
  52. enum
  53. {
  54. MaxBufEntries = 256,
  55. InvalidEntry = 0xffffffff
  56. };
  57. //------------------------------------------------------------------------------
  58. // Variables
  59. //------------------------------------------------------------------------------
  60. private:
  61. long m_lNumCtrls;
  62. long m_lOldestSeq;
  63. long m_alBuf[MaxBufEntries];
  64. //------------------------------------------------------------------------------
  65. // Functions
  66. //------------------------------------------------------------------------------
  67. public:
  68. ////////////////////////////////////////////////////////////////////////////////
  69. // Constructor
  70. ////////////////////////////////////////////////////////////////////////////////
  71. CCtrlBuf()
  72. {
  73. Reset();
  74. }
  75. ////////////////////////////////////////////////////////////////////////////////
  76. // Destructor
  77. ////////////////////////////////////////////////////////////////////////////////
  78. ~CCtrlBuf()
  79. {
  80. }
  81. ////////////////////////////////////////////////////////////////////////////////
  82. // Reset
  83. ////////////////////////////////////////////////////////////////////////////////
  84. void Reset(void)
  85. {
  86. m_lNumCtrls = 0;
  87. m_lOldestSeq = 0;
  88. }
  89. ////////////////////////////////////////////////////////////////////////////////
  90. // Add new ctrl to buffer
  91. ////////////////////////////////////////////////////////////////////////////////
  92. short Add(
  93. long lSeq,
  94. long lNum,
  95. long* plCtrls,
  96. long* plNumAdded)
  97. {
  98. short sResult = 0;
  99. // This can and should be optimized!
  100. *plNumAdded = 0;
  101. for (long l = 0; l < lNum; l++)
  102. {
  103. sResult = Add(lSeq++, *plCtrls++);
  104. if (sResult == 0)
  105. (*plNumAdded)++;
  106. else
  107. break;
  108. }
  109. return sResult;
  110. }
  111. short Add(
  112. long lSeq,
  113. long lCtrl)
  114. {
  115. short sResult = 0;
  116. // This needs to deal with the fact that the ctrls don't always come
  117. // sequentially, and there will often be gaps between ctrl values that
  118. // get filled in later on.
  119. if (m_lNumCtrls == 0)
  120. {
  121. // If array is empty, set oldest seq to specified seq, add ctrl to
  122. // array, and adjust the number of ctrls.
  123. m_lOldestSeq = lSeq;
  124. m_alBuf[0] = lCtrl;
  125. m_lNumCtrls++;
  126. }
  127. else
  128. {
  129. // Calculate index in array based on sequence number
  130. long lIndex = lSeq - m_lOldestSeq;
  131. // Make sure it isn't older than our oldest value. This can happen if
  132. // we receive an older data packet. If it does happen, we quietly
  133. // ignore the value since it is apparently no longer needed.
  134. if (lIndex >= 0)
  135. {
  136. // Make sure it will fit in the buffer
  137. if (lIndex < MaxBufEntries)
  138. {
  139. // Add new ctrl to array
  140. m_alBuf[lIndex] = lCtrl;
  141. // Check if new ctrl went beyond the "current" number of entries
  142. if (lIndex >= m_lNumCtrls)
  143. {
  144. // Invalidate any unused ctrls (there may not be any)
  145. for (long l = m_lNumCtrls; l < lIndex; l++)
  146. m_alBuf[l] = InvalidEntry;
  147. // Set new number of entries (last index + 1)
  148. m_lNumCtrls = lIndex + 1;
  149. }
  150. }
  151. else
  152. {
  153. sResult = -1;
  154. TRACE("No room in buf!\n");
  155. }
  156. }
  157. }
  158. return sResult;
  159. }
  160. ////////////////////////////////////////////////////////////////////////////////
  161. // Get specified entry. If the returned value is 'InvalidEntry', then that
  162. // entry was not available.
  163. ////////////////////////////////////////////////////////////////////////////////
  164. long GetAt(
  165. long lSeq)
  166. {
  167. if (m_lNumCtrls > 0)
  168. {
  169. long lIndex = lSeq - m_lOldestSeq;
  170. if ((lIndex >= 0) && (lIndex < m_lNumCtrls))
  171. return m_alBuf[lIndex];
  172. }
  173. return InvalidEntry;
  174. }
  175. ////////////////////////////////////////////////////////////////////////////////
  176. // Get pointer to specified entry. If the returned value is 0 (NULL), then
  177. // that entry was not available.
  178. ////////////////////////////////////////////////////////////////////////////////
  179. long* GetPtrTo(
  180. long lSeq)
  181. {
  182. if (m_lNumCtrls > 0)
  183. {
  184. long lIndex = lSeq - m_lOldestSeq;
  185. if ((lIndex >= 0) && (lIndex < m_lNumCtrls))
  186. return &(m_alBuf[lIndex]);
  187. }
  188. return 0;
  189. }
  190. ////////////////////////////////////////////////////////////////////////////////
  191. // Discard all ctrls up to the specified sequence number
  192. ////////////////////////////////////////////////////////////////////////////////
  193. void DiscardThrough(
  194. long lSeq)
  195. {
  196. if (m_lNumCtrls > 0)
  197. {
  198. if (lSeq >= m_lOldestSeq)
  199. {
  200. // Calculate index of first ctrl that will be KEPT
  201. long lKeepIndex = (lSeq - m_lOldestSeq) + 1;
  202. // If index is less than number of ctrls, then there's something to be moved.
  203. // Otherwise, the entire array has been discarded.
  204. if (lKeepIndex < m_lNumCtrls)
  205. {
  206. // Move remaining ctrls down to start of array
  207. memmove(&(m_alBuf[0]), &(m_alBuf[lKeepIndex]), (m_lNumCtrls - lKeepIndex) * sizeof(m_alBuf[0]));
  208. m_lNumCtrls -= lKeepIndex;
  209. m_lOldestSeq = lSeq + 1;
  210. }
  211. else
  212. {
  213. // All entries were discarded
  214. m_lNumCtrls = 0;
  215. }
  216. }
  217. }
  218. }
  219. ////////////////////////////////////////////////////////////////////////////////
  220. // Determine whether buffer is empty
  221. ////////////////////////////////////////////////////////////////////////////////
  222. bool IsEmpty(void)
  223. {
  224. return m_lNumCtrls == 0;
  225. }
  226. ////////////////////////////////////////////////////////////////////////////////
  227. // Determine whether buffer is full
  228. ////////////////////////////////////////////////////////////////////////////////
  229. bool IsFull(void)
  230. {
  231. return m_lNumCtrls == MaxBufEntries;
  232. }
  233. ////////////////////////////////////////////////////////////////////////////////
  234. // Get oldest sequence (not valid if buffer is empty!)
  235. ////////////////////////////////////////////////////////////////////////////////
  236. long GetOldestSeq(void)
  237. {
  238. return m_lOldestSeq;
  239. }
  240. ////////////////////////////////////////////////////////////////////////////////
  241. // Get newest sequence (not valid if buffer is empty!)
  242. ////////////////////////////////////////////////////////////////////////////////
  243. long GetNewestSeq(void)
  244. {
  245. return m_lOldestSeq + (m_lNumCtrls - 1);
  246. }
  247. };
  248. #endif //CTRLBUF_H
  249. ////////////////////////////////////////////////////////////////////////////////
  250. // EOF
  251. ////////////////////////////////////////////////////////////////////////////////