Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*****************************************************************************/
- /**
- * @brief Function initializes the circular queue structure with the
- * given buffer size of burrer and size of structure
- *
- * @note
- *
- * @param *q - structure pointer
- * @param *buf - buffer pointer
- * @param size - buffer size
- * @param structsize - size of structure
- *
- * @return None.
- *****************************************************************************/
- void cirqueue_init(cirqueue_t * q, const void * buf, int size, int structsize)
- {
- q->head = 0;
- q->tail = 0;
- q->size = size;
- q->mask = size - 1;
- q->buf = (char*) buf;
- q->structsize = structsize;
- }
- /*****************************************************************************/
- /**
- * @brief Reset queue of state
- *
- * @note
- *
- * @param *q - structure pointer
- *
- * @return None
- *****************************************************************************/
- void cirqueue_reset(cirqueue_t * q)
- {
- q->tail = q->head = 0;
- }
- /*****************************************************************************/
- /**
- * @brief Check queue of state
- *
- * @note
- *
- * @param *q - structure pointer
- *
- * @return 0 - is empty / !0 - not
- *****************************************************************************/
- int cirqueue_empty(cirqueue_t * q)
- {
- return (q->tail == q->head) ? 0 : 1;
- }
- /*****************************************************************************/
- /**
- * @brief Check queue of overflow
- *
- * @note
- *
- * @param *q - structure pointer
- *
- * @return 0 - not / !0 - is overflow
- *****************************************************************************/
- int cirqueue_overflow(cirqueue_t * q)
- {
- return ((q->head - q->tail) >= q->size) ? 1 : 0;
- }
- /*****************************************************************************/
- /**
- * @brief Function push bytes of structure
- *
- * @note
- *
- * @param *q - structure pointer
- * @param *buf - buffer pointer of structure
- *
- * @return The number of bytes written is returned.
- * If the head runs in to the tail, not all bytes are written
- *****************************************************************************/
- int cirqueue_push(cirqueue_t * q, const void * buf)
- {
- int i;
- const char * p;
- p = buf;
- for (i = 0; i < q->structsize; i++)
- {
- q->buf[q->head & q->mask] = *p++;
- q->head++; //increment the head
- }
- return q->structsize;
- }
- /*****************************************************************************/
- /**
- * @brief Function reads bytes of structure
- *
- * @note
- *
- * @param *q - structure pointer
- * @param *buf - buffer pointer
- *
- * @return The number of bytes read is returned.
- *****************************************************************************/
- int cirqueue_pop(cirqueue_t * q, void * buf)
- {
- int i = 0;
- char * p;
- p = buf;
- if (q->tail != q->head)
- { //see if any data is available
- for (i = 0; i < q->structsize; i++)
- {
- *p++ = q->buf[q->tail & q->mask]; //grab a byte from the circular buffer
- q->tail++; //increment the tail
- if (q->tail == q->head)
- { //check for wrap-around
- return i; //number of bytes read
- }
- }
- }
- return i;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement