vxwRngLib.h
上传用户:luoyougen
上传日期:2008-05-12
资源大小:23136k
文件大小:7k
源码类别:

VxWorks

开发平台:

C/C++

  1. // VXWRingBuf/vxwRngLib.h - ring buffer class
  2. // Copyright 1995-1999 Wind River Systems, Inc.
  3. // modification history
  4. // --------------------
  5. // 01b,23feb99,fle  doc : made it refgen compliant
  6. // 01d,21feb99,jdi  added library section, checked in documentation.
  7. // 01c,03oct95,rhp  documented.
  8. // 01b,05feb97,bss  fixed SPR #7821 by defining myValue() method.
  9. // 01a,15jun95,srh  written.
  10. // DESCRIPTION
  11. // The `VXWRingBuf' class provides routines for creating and using
  12. // ring buffers, which are first-in-first-out circular buffers.  The
  13. // routines simply manipulate the ring buffer data structure; no
  14. // kernel functions are invoked.  In particular, ring buffers by
  15. // themselves provide no task synchronization or mutual exclusion.
  16. //
  17. // However, the ring buffer pointers are manipulated in such a way
  18. // that a reader task (invoking VXWRingBuf::get()) and a writer task
  19. // (invoking VXWRingBuf::put()) can access a ring simultaneously
  20. // without requiring mutual exclusion.  This is because readers only
  21. // affect a <read> pointer and writers only affect a <write>
  22. // pointer in a ring buffer data structure.  However, access by
  23. // multiple readers or writers <must> be interlocked through a
  24. // mutual exclusion mechanism (for example, a mutual-exclusion
  25. // semaphore guarding a ring buffer).
  26. //
  27. // INCLUDE FILES: vxwRngLib.h
  28. //
  29. // SECTION: 1C
  30. //
  31. #ifndef vxwRngLib_h
  32. #define vxwRngLib_h
  33. #include "vxWorks.h"
  34. #include "rngLib.h"
  35. #include "vxwObject.h"
  36. #include "vxwErr.h"
  37. class VXWRingBuf : virtual public VXWIdObject
  38.     {
  39.   public:
  40. //_ VXWRingBuf Public Constructors
  41. ///////////////////////////////////////////////////////////////////////////////
  42. //
  43. // VXWRingBuf::VXWRingBuf - create an empty ring buffer
  44. //
  45. // This constructor creates a ring buffer of size <nbytes>, and initializes
  46. // it.  Memory for the buffer is allocated from the system memory partition.
  47. //
  48. // RETURNS: N/A.
  49.     VXWRingBuf (int nbytes)
  50. : rid_ (rngCreate (nbytes))
  51. {
  52. if (rid_ == 0)
  53.     vxwThrowErrno ();
  54. }
  55. ///////////////////////////////////////////////////////////////////////////////
  56. //
  57. // VXWRingBuf::VXWRingBuf - build ring-buffer object from existing ID
  58. // 
  59. // Use this constructor to build a ring-buffer object from an existing
  60. // ring buffer.  This permits you to use the C++ ring-buffer
  61. // interfaces even if the ring buffer itself was created by a routine
  62. // written in C.
  63. // 
  64. // RETURNS: N/A.
  65. // 
  66. // SEE ALSO: rngLib
  67.     VXWRingBuf (RING_ID aRingId)
  68. : rid_ (aRingId)
  69. {
  70. }
  71. ///////////////////////////////////////////////////////////////////////////////
  72. //
  73. // VXWRingBuf::~VXWRingBuf - delete ring buffer
  74. //
  75. // This destructor deletes a specified ring buffer.
  76. // Any data in the buffer at the time it is deleted is lost.
  77. //
  78. // RETURNS: N/A
  79.     ~VXWRingBuf ()
  80. {
  81. rngDelete (rid_);
  82. }
  83. //_ VXWRingBuf Public Member Functions
  84. ///////////////////////////////////////////////////////////////////////////////
  85. //
  86. // VXWRingBuf::get - get characters from ring buffer
  87. //
  88. // This routine copies bytes from the ring buffer into <buffer>.
  89. // It copies as many bytes as are available in the ring, up to <maxbytes>.
  90. // The bytes copied are then removed from the ring.
  91. //
  92. // RETURNS:
  93. // The number of bytes actually received from the ring buffer;
  94. // it may be zero if the ring buffer is empty at the time of the call.
  95.     int get (char * buffer, int maxbytes)
  96. {
  97. return rngBufGet (rid_, buffer, maxbytes);
  98. }
  99. ///////////////////////////////////////////////////////////////////////////////
  100. //
  101. // VXWRingBuf::put - put bytes into ring buffer
  102. //
  103. // This routine puts bytes from <buffer> into the ring buffer.  The
  104. // specified number of bytes is put into the ring, up to the number of
  105. // bytes available in the ring.
  106. //
  107. // RETURNS:
  108. // The number of bytes actually put into the ring buffer;
  109. // it may be less than number requested, even zero,
  110. // if there is insufficient room in the ring buffer at the time of the call.
  111.     int put (char * buffer, int nBytes)
  112. {
  113. return rngBufPut (rid_, buffer, nBytes);
  114. }
  115. ///////////////////////////////////////////////////////////////////////////////
  116. //
  117. // VXWRingBuf::flush - make ring buffer empty
  118. //
  119. // This routine initializes the ring buffer to be empty.
  120. // Any data in the buffer is lost.
  121. //
  122. // RETURNS: N/A
  123.     void flush ()
  124. {
  125. rngFlush (rid_);
  126. }
  127. ///////////////////////////////////////////////////////////////////////////////
  128. //
  129. // VXWRingBuf::freeBytes - determine the number of free bytes in ring buffer
  130. //
  131. // This routine determines the number of bytes currently unused in the
  132. // ring buffer.
  133. //
  134. // RETURNS: The number of unused bytes in the ring buffer.
  135.     int freeBytes () const
  136. {
  137. return rngFreeBytes (rid_);
  138. }
  139. ///////////////////////////////////////////////////////////////////////////////
  140. //
  141. // VXWRingBuf::isEmpty - test whether ring buffer is empty
  142. //
  143. // This routine reports on whether the ring buffer is empty.
  144. //
  145. // RETURNS:
  146. // TRUE if empty, FALSE if not.
  147.     BOOL isEmpty () const
  148. {
  149. return rngIsEmpty (rid_);
  150. }
  151. ///////////////////////////////////////////////////////////////////////////////
  152. //
  153. // VXWRingBuf::isFull - test whether ring buffer is full (no more room)
  154. //
  155. // This routine reports on whether the ring buffer is completely full.
  156. //
  157. // RETURNS:
  158. // TRUE if full, FALSE if not.
  159.     BOOL isFull () const
  160. {
  161. return rngIsFull (rid_);
  162. }
  163. ///////////////////////////////////////////////////////////////////////////////
  164. //
  165. // VXWRingBuf::moveAhead - advance ring pointer by <n> bytes
  166. // 
  167. // This routine advances the ring buffer input pointer by <n> bytes.
  168. // This makes <n> bytes available in the ring buffer, after having
  169. // been written ahead in the ring buffer with VXWRingBuf::putAhead().
  170. //
  171. // RETURNS: N/A
  172.     void moveAhead (int n)
  173. {
  174. rngMoveAhead (rid_, n);
  175. }
  176. ///////////////////////////////////////////////////////////////////////////////
  177. //
  178. // VXWRingBuf::nBytes - determine the number of bytes in ring buffer
  179. //
  180. // This routine determines the number of bytes currently in the
  181. // ring buffer.
  182. //
  183. // RETURNS: The number of bytes filled in the ring buffer.
  184.     int nBytes () const
  185. {
  186. return rngNBytes (rid_);
  187. }
  188. ///////////////////////////////////////////////////////////////////////////////
  189. //
  190. // VXWRingBuf::putAhead - put a byte ahead in a ring buffer without moving ring pointers
  191. // 
  192. // This routine writes a byte into the ring, but does not move the
  193. // ring buffer pointers.  Thus the byte is not yet be available to
  194. // VXWRingBuf::get() calls.  The byte is written <offset> bytes ahead
  195. // of the next input location in the ring.  Thus, an offset of 0 puts
  196. // the byte in the same position as VXWRingBuf::put() would put a
  197. // byte, except that the input pointer is not updated.
  198. // 
  199. // Bytes written ahead in the ring buffer with this routine can be
  200. // made available all at once by subsequently moving the ring buffer
  201. // pointers with the routine VXWRingBuf::moveAhead().
  202. //
  203. // Before calling VXWRingBuf::putAhead(), the caller must verify that at least
  204. // <offset> + 1 bytes are available in the ring buffer.
  205. //
  206. // RETURNS: N/A
  207.     void putAhead (char byte, int offset)
  208. {
  209. rngPutAhead (rid_, byte, offset);
  210. }
  211.   protected:
  212.     VXWRingBuf ()
  213. {
  214. }
  215.     VXWRingBuf (const VXWRingBuf &)
  216. {
  217. }
  218.     VXWRingBuf & operator = (const VXWRingBuf &)
  219. {
  220. return *this;
  221. }
  222.     // Added to fix SPR # 7821.
  223.     virtual void * myValue ();
  224.     RING_ID rid_;
  225.     };
  226. #endif /* ifndef vxwRngLib_h */