Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.21 KB | None | 0 0
  1. /*****************************************************************************/
  2. /**
  3.  * @brief       Function initializes the circular queue structure with the
  4.  *              given buffer size of burrer and size of structure
  5.  *
  6.  * @note
  7.  *
  8.  * @param       *q -  structure pointer
  9.  * @param       *buf - buffer pointer
  10.  * @param       size - buffer size
  11.  * @param       structsize - size of structure
  12.  *
  13.  * @return      None.
  14.  *****************************************************************************/
  15. void cirqueue_init(cirqueue_t * q, const void * buf, int size, int structsize)
  16. {
  17.     q->head = 0;
  18.     q->tail = 0;
  19.     q->size = size;
  20.     q->mask = size - 1;
  21.     q->buf = (char*) buf;
  22.     q->structsize = structsize;
  23. }
  24. /*****************************************************************************/
  25. /**
  26.  * @brief       Reset queue of state
  27.  *
  28.  * @note
  29.  *
  30.  * @param       *q -  structure pointer
  31.  *
  32.  * @return      None
  33.  *****************************************************************************/
  34. void cirqueue_reset(cirqueue_t * q)
  35. {
  36.     q->tail = q->head = 0;
  37. }
  38. /*****************************************************************************/
  39. /**
  40.  * @brief       Check queue of state
  41.  *
  42.  * @note
  43.  *
  44.  * @param       *q -  structure pointer
  45.  *
  46.  * @return      0 - is empty / !0 - not
  47.  *****************************************************************************/
  48. int cirqueue_empty(cirqueue_t * q)
  49. {
  50.     return (q->tail == q->head) ? 0 : 1;
  51. }
  52. /*****************************************************************************/
  53. /**
  54.  * @brief       Check queue of overflow
  55.  *
  56.  * @note
  57.  *
  58.  * @param       *q -  structure pointer
  59.  *
  60.  * @return      0 - not / !0 - is overflow
  61.  *****************************************************************************/
  62. int cirqueue_overflow(cirqueue_t * q)
  63. {
  64.     return ((q->head - q->tail) >= q->size) ? 1 : 0;
  65. }
  66. /*****************************************************************************/
  67. /**
  68.  * @brief       Function push bytes of structure
  69.  *
  70.  * @note
  71.  *
  72.  * @param       *q -  structure pointer
  73.  * @param       *buf - buffer pointer of structure
  74.  *
  75.  * @return      The number of bytes written is returned.
  76.  *              If the head runs in to the tail, not all bytes are written
  77.  *****************************************************************************/
  78. int cirqueue_push(cirqueue_t * q, const void * buf)
  79. {
  80.     int i;
  81.     const char * p;
  82.     p = buf;
  83.  
  84.     for (i = 0; i < q->structsize; i++)
  85.     {
  86.         q->buf[q->head & q->mask] = *p++;
  87.         q->head++;  //increment the head
  88.     }
  89.     return q->structsize;
  90. }
  91. /*****************************************************************************/
  92. /**
  93.  * @brief       Function reads bytes of structure
  94.  *
  95.  * @note
  96.  *
  97.  * @param       *q -  structure pointer
  98.  * @param       *buf - buffer pointer
  99.  *
  100.  * @return      The number of bytes read is returned.
  101.  *****************************************************************************/
  102. int cirqueue_pop(cirqueue_t * q, void * buf)
  103. {
  104.     int i = 0;
  105.     char * p;
  106.     p = buf;
  107.     if (q->tail != q->head)
  108.     { //see if any data is available
  109.         for (i = 0; i < q->structsize; i++)
  110.         {
  111.             *p++ = q->buf[q->tail & q->mask]; //grab a byte from the circular buffer
  112.             q->tail++;  //increment the tail
  113.             if (q->tail == q->head)
  114.             {  //check for wrap-around
  115.                 return i; //number of bytes read
  116.             }
  117.         }
  118.     }
  119.  
  120.     return i;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement